Skip to content

Commit

Permalink
test: test highlighted lines in code blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
farnabaz committed Sep 6, 2023
1 parent 5e8f4f7 commit 7cbe123
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
24 changes: 24 additions & 0 deletions test/markdown/code-block-highlighted-lines.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { expect, it } from 'vitest'
import { parseMarkdown } from '../utils/parser'

const md = `
\`\`\`ts {2,3}
class C {
private name: string = "foo"
private age: number = 18
}
\`\`\`
`.trim()

it('Highlighted code block', async () => {
const { body } = await parseMarkdown(md, {
highlight: {
theme: 'github-dark'
}
})
expect(body).toHaveProperty('type', 'root')
expect(body.children).toHaveLength(2)
expect((body.children[0] as any).props?.highlights).toEqual([2,3])
expect((body.children[0] as any).children[0].children[1].props).haveOwnProperty('class', 'line highlight')
expect((body.children[0] as any).children[0].children[2].props).haveOwnProperty('class', 'line highlight')
})
4 changes: 2 additions & 2 deletions test/utils/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ export const parseMarkdown = (md: string, options: MDCParseOptions = {}) => {
options.highlight = options.highlight || {}
options.highlight.theme = options.highlight.theme || 'github-light'

options.highlight.highlighter = async (code: string, lang: string, theme: Theme) => {
options.highlight.highlighter = async (code: string, lang: string, theme: Theme, highlights) => {
const shikiHighlighter = useShikiHighlighter({})

const styleMap: TokenStyleMap = {}

const { tree, className } = await shikiHighlighter.getHighlightedAST(code as string, lang as any, theme as Theme, { styleMap })
const { tree, className } = await shikiHighlighter.getHighlightedAST(code as string, lang as any, theme as Theme, { styleMap, highlights })

return {
tree,
Expand Down

0 comments on commit 7cbe123

Please sign in to comment.