Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(markdown): keep meta from fenced code block #1800

Merged
merged 2 commits into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;',
'```'
Qwertovsky marked this conversation as resolved.
Show resolved Hide resolved
].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