refactor: optimize snippet markdown plugin code (#2580)

pull/2581/head
烽宁 1 year ago committed by GitHub
parent 32d65d40c5
commit d5ccc52048
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -145,12 +145,21 @@ export const snippetPlugin = (md: MarkdownIt, srcDir: string) => {
// @ts-ignore
const [src, regionName] = token.src ?? []
if (src) {
if (!src) return fence(...args)
if (loader) {
loader.addDependency(src)
}
const isAFile = fs.lstatSync(src).isFile()
if (fs.existsSync(src) && isAFile) {
if (!fs.existsSync(src) || !isAFile) {
token.content = isAFile
? `Code snippet path not found: ${src}`
: `Invalid code snippet option`
token.info = ''
return fence(...args)
}
let content = fs.readFileSync(src, 'utf8')
if (regionName) {
@ -161,20 +170,13 @@ export const snippetPlugin = (md: MarkdownIt, srcDir: string) => {
content = dedent(
lines
.slice(region.start, region.end)
.filter((line: string) => !region.regexp.test(line.trim()))
.filter((line) => !region.regexp.test(line.trim()))
.join('\n')
)
}
}
token.content = content
} else {
token.content = isAFile
? `Code snippet path not found: ${src}`
: `Invalid code snippet option`
token.info = ''
}
}
return fence(...args)
}

Loading…
Cancel
Save