From 7cbe123fe8714d99b109ae858141770c83bfbcd1 Mon Sep 17 00:00:00 2001 From: Farnabaz Date: Wed, 6 Sep 2023 16:31:19 +0200 Subject: [PATCH] test: test highlighted lines in code blocks --- .../code-block-highlighted-lines.test.ts | 24 +++++++++++++++++++ test/utils/parser.ts | 4 ++-- 2 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 test/markdown/code-block-highlighted-lines.test.ts diff --git a/test/markdown/code-block-highlighted-lines.test.ts b/test/markdown/code-block-highlighted-lines.test.ts new file mode 100644 index 00000000..42f0ada8 --- /dev/null +++ b/test/markdown/code-block-highlighted-lines.test.ts @@ -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') +}) \ No newline at end of file diff --git a/test/utils/parser.ts b/test/utils/parser.ts index c4f03f54..3d10756b 100644 --- a/test/utils/parser.ts +++ b/test/utils/parser.ts @@ -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,