Skip to content

Commit

Permalink
fix(code-block): meta property
Browse files Browse the repository at this point in the history
  • Loading branch information
farnabaz committed May 16, 2023
1 parent 7cd771c commit 97005cf
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/runtime/markdown-parser/handler/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type Node = MdastContent & {

export default (h: H, node: Node) => {
const lang = (node.lang || '') + ' ' + (node.meta || '')
const { language, highlights, filename } = parseThematicBlock(lang)
const { language, highlights, filename, meta } = parseThematicBlock(lang)
const code = node.value ? detab(node.value + '\n') : ''

return h(
Expand All @@ -22,7 +22,7 @@ export default (h: H, node: Node) => {
language,
filename,
highlights,
meta: node.meta,
meta,
code,
className: [`language-${language}`]
},
Expand Down
4 changes: 3 additions & 1 deletion src/runtime/markdown-parser/handler/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ export function parseThematicBlock (lang: string) {
const language = lang.replace(/[{|[](.+)/, '').match(/^[^ \t]+(?=[ \t]|$)/)
const highlightTokens = lang.match(/{([^}]+)}/)
const filenameTokens = lang.match(/\[(.+)\]/)
const meta = lang.replace(/^\w+\s+(\[[^\]]+\])?\s*(\{[^}]+\})?\s*/g, '')

return {
language: language ? language[0] : undefined,
highlights: parseHighlightedLines(highlightTokens && highlightTokens[1]),
filename: Array.isArray(filenameTokens) ? filenameTokens[1] : undefined
filename: Array.isArray(filenameTokens) ? filenameTokens[1] : undefined,
meta
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/features/parser-markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export const testMarkdownParser = () => {
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.meta).toBe('other code block info')
expect(props.language).toBe('ts')
expect(props.filename).toBe('file.ts')
expect(props.highlights).toEqual([4, 5, 6, 7])
Expand Down

0 comments on commit 97005cf

Please sign in to comment.