Skip to content

Commit

Permalink
fix: support relative path at demo
Browse files Browse the repository at this point in the history
  • Loading branch information
zuofenghua committed Aug 18, 2020
1 parent 7c267e0 commit 2383519
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/node/markdown/plugins/demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ export const demoPlugin = (md: MarkdownIt) => {
const componentName = `demo${index++}`
const src = (content.match(/src=("|')(\S+)('|")/) || [])[2] ?? ''
let language = (content.match(/language=("|')(.*)('|")/) || [])[2] ?? ''
const srcPath = path.resolve(process.cwd(), src)
const srcPath = path.resolve((global as any).fileRoot, src)
if (!src || !fs.existsSync(srcPath)) {
const warningMsg = `${srcPath} is not exist!`
const warningMsg = `${srcPath} does not exist!`
console.warn(`[vitepress]: ${warningMsg}`)
return `<demo src="${src}" >
<p>${warningMsg}</p>`
Expand All @@ -37,7 +37,7 @@ export const demoPlugin = (md: MarkdownIt) => {
}

console.log(`srcPath=${srcPath}`)
const codeStr = fs.readFileSync(src).toString()
const codeStr = fs.readFileSync(srcPath).toString()
const htmlStr = encodeURIComponent(highlight(codeStr, language))

hoistedTags.script!.unshift(`import ${componentName} from '${src}' \n`)
Expand Down
6 changes: 4 additions & 2 deletions src/node/markdownToVue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export function createMarkdownToVueRenderFn(
injectData = true
) => {
file = path.relative(root, file)
// TODO
;(global as any).fileRoot = path.resolve(file, '../')
const cached = cache.get(src)
if (cached) {
debug(`[cache hit] ${file}`)
Expand Down Expand Up @@ -65,8 +67,8 @@ export function createMarkdownToVueRenderFn(
debug(`[render] ${file} in ${Date.now() - start}ms.`)

const result = { vueSrc, pageData }
console.log(file + '\n')
console.log(vueSrc)
// console.log(file + '\n')
// console.log(vueSrc)
cache.set(src, result)
return result
}
Expand Down

0 comments on commit 2383519

Please sign in to comment.