From 80b34c7c49219bbced3eb3471e09c6625090e02a Mon Sep 17 00:00:00 2001 From: Divyansh Singh <40380293+brc-dd@users.noreply.github.com> Date: Fri, 8 Jul 2022 14:42:56 +0530 Subject: [PATCH] fix: don't throw error on including non existent md file --- docs/guide/markdown.md | 43 +++++++++++++++++++-------------------- src/node/markdownToVue.ts | 14 ++++++++----- 2 files changed, 30 insertions(+), 27 deletions(-) diff --git a/docs/guide/markdown.md b/docs/guide/markdown.md index f3496948..342904e5 100644 --- a/docs/guide/markdown.md +++ b/docs/guide/markdown.md @@ -388,48 +388,47 @@ You can also use a [VS Code region](https://code.visualstudio.com/docs/editor/co -## Import & Embed markdown's content +## Markdown File Inclusion -When you need to combine multiple markdown content over a single markdown file, e.g. +You can include a markdown file in another markdown file like this: -```vue - +**Input** +```md # Docs ## Basics -{{ basics }} + +``` -## API Reference +**Part file** (`parts/basics.md`) -{{ reference }} -``` +```md +Some getting started stuff. -Replace curly brackets with `@include`, -the pathname with the extension, -surrounded by `` sintax +### Configuration -```vue - +Can be created using `.foorc.json`. +``` + +**Equivalent code** +```md # Docs ## Basics -< !--@include:./parts/basics.md--> +Some getting started stuff. -## API Reference +### Configuration -< !--@include:./parts/reference.md--> +Can be created using `.foorc.json`. ``` +::: warning +Note that this does not throw errors if your file is not present. Hence, when using this feature make sure that the contents are being rendered as expected. +::: ## Advanced Configuration diff --git a/src/node/markdownToVue.ts b/src/node/markdownToVue.ts index 86a07750..eeb567a3 100644 --- a/src/node/markdownToVue.ts +++ b/src/node/markdownToVue.ts @@ -54,11 +54,15 @@ export async function createMarkdownToVueRenderFn( // resolve includes let includes: string[] = [] - src = src.replace(includesRE, (_, m1) => { - const includePath = path.join(dir, m1) - const content = fs.readFileSync(includePath, 'utf-8') - includes.push(slash(includePath)) - return content + src = src.replace(includesRE, (m, m1) => { + try { + const includePath = path.join(dir, m1) + const content = fs.readFileSync(includePath, 'utf-8') + includes.push(slash(includePath)) + return content + } catch (error) { + return m // silently ignore error if file is not present + } }) const { content, data: frontmatter } = matter(src)