From cbce38a6346d8871e4a9d8c6e1ba172fa33e59c5 Mon Sep 17 00:00:00 2001 From: Valery Qwertovsky <5704043+Qwertovsky@users.noreply.github.com> Date: Fri, 6 Jan 2023 15:39:20 +0300 Subject: [PATCH 1/2] feat(markdown): keep meta from fenced code block --- docs/content/4.api/1.components/7.prose.md | 15 +++++++++++-- src/runtime/components/Prose/ProseCode.vue | 4 ++++ src/runtime/markdown-parser/handler/code.ts | 1 + test/features/parser-markdown.ts | 25 +++++++++++++++++++++ 4 files changed, 43 insertions(+), 2 deletions(-) diff --git a/docs/content/4.api/1.components/7.prose.md b/docs/content/4.api/1.components/7.prose.md index bd53d72f7..983501b90 100755 --- a/docs/content/4.api/1.components/7.prose.md +++ b/docs/content/4.api/1.components/7.prose.md @@ -45,7 +45,7 @@ To overwrite a prose component, create a component with the same name in your pr ::code-group ```markdown [Code] - ```javascript + ```javascript[file.js]{4-6,7} meta-info=val export default () => { console.log('Code block') } @@ -54,7 +54,7 @@ To overwrite a prose component, create a component with the same name in your pr ::code-block{label="Preview"} - ```javascript + ```javascript[file.js]{4-6,7} export default () => { console.log('Code block') } @@ -64,6 +64,17 @@ To overwrite a prose component, create a component with the same name in your pr :: +Component properties will be: +```json +{ + code: "export default () => {\n console.log('Code block')\n}" + language: "javascript" + filename: "file.js" + highlights: [4, 5, 6, 7] + meta: "meta-info=val" +} +``` + Check out the [highlight options](/api/configuration#highlight) for more about the syntax highlighting. ## `ProseCodeInline` diff --git a/src/runtime/components/Prose/ProseCode.vue b/src/runtime/components/Prose/ProseCode.vue index db1087d61..3c7982e8f 100644 --- a/src/runtime/components/Prose/ProseCode.vue +++ b/src/runtime/components/Prose/ProseCode.vue @@ -18,6 +18,10 @@ export default defineComponent({ highlights: { type: Array as () => number[], default: () => [] + }, + meta: { + type: String, + default: null } } }) diff --git a/src/runtime/markdown-parser/handler/code.ts b/src/runtime/markdown-parser/handler/code.ts index c30412f4b..0a53e3ded 100644 --- a/src/runtime/markdown-parser/handler/code.ts +++ b/src/runtime/markdown-parser/handler/code.ts @@ -15,6 +15,7 @@ export default (h: H, node: any) => { language, filename, highlights, + meta: node.meta, code }, [h(node, 'pre', {}, [h(node, 'code', { __ignoreMap: '' }, [u('text', code)])])] diff --git a/test/features/parser-markdown.ts b/test/features/parser-markdown.ts index 00fb09009..3b99284db 100644 --- a/test/features/parser-markdown.ts +++ b/test/features/parser-markdown.ts @@ -39,6 +39,31 @@ export const testMarkdownParser = () => { expect(parsed.body).toHaveProperty('children[0].children[0].tag', 'code-inline') }) + test('Keep meta from fenced code block', async () => { + const parsed = await $fetch('/api/parse', { + method: 'POST', + body: { + id: 'content:index.md', + content: [ + '``` ts [file.ts]{4-6,7} other code block info', + 'let code = undefined;', + 'return code;', + '```' + ].join('\n') + } + }) + + expect(parsed).toHaveProperty('body') + expect(parsed.body).toHaveProperty('children[0].tag', 'code') + expect(parsed.body).toHaveProperty('children[0].props') + const props = parsed.body.children[0].props + expect(props).toHaveProperty('meta') + expect(props.meta).toBe('[file.ts]{4-6,7} other code block info') + expect(props.language).toBe('ts') + expect(props.filename).toBe('file.ts') + expect(props.highlights).toEqual([4, 5, 6, 7]) + }) + test('comment', async () => { const parsed = await $fetch('/api/parse', { method: 'POST', From 5a4a35dd8f46ffa84753e678ac2c4e422f177eb1 Mon Sep 17 00:00:00 2001 From: Valery Qwertovsky <5704043+Qwertovsky@users.noreply.github.com> Date: Wed, 11 Jan 2023 23:56:44 +0300 Subject: [PATCH 2/2] Update test/features/parser-markdown.ts Co-authored-by: nobkd <44443899+nobkd@users.noreply.github.com> --- test/features/parser-markdown.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/features/parser-markdown.ts b/test/features/parser-markdown.ts index 3b99284db..09f5e7c2b 100644 --- a/test/features/parser-markdown.ts +++ b/test/features/parser-markdown.ts @@ -45,10 +45,10 @@ export const testMarkdownParser = () => { body: { id: 'content:index.md', content: [ - '``` ts [file.ts]{4-6,7} other code block info', - 'let code = undefined;', - 'return code;', - '```' + '```ts [file.ts]{4-6,7} other code block info', + 'let code = undefined;', + 'return code;', + '```' ].join('\n') } })