Skip to content

Commit

Permalink
feat(markdown): keep meta from fenced code block (#1800)
Browse files Browse the repository at this point in the history
Co-authored-by: nobkd <44443899+nobkd@users.noreply.github.com>
  • Loading branch information
Qwertovsky and nobkd committed Jan 12, 2023
1 parent 1eace98 commit 0aa922c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
15 changes: 13 additions & 2 deletions docs/content/4.api/1.components/7.prose.md
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
Expand All @@ -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')
}
Expand All @@ -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`
Expand Down
4 changes: 4 additions & 0 deletions src/runtime/components/Prose/ProseCode.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export default defineComponent({
highlights: {
type: Array as () => number[],
default: () => []
},
meta: {
type: String,
default: null
}
}
})
Expand Down
1 change: 1 addition & 0 deletions src/runtime/markdown-parser/handler/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)])])]
Expand Down
25 changes: 25 additions & 0 deletions test/features/parser-markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 0aa922c

Please sign in to comment.