diff --git a/packages/core/src/highlight/code-to-hast.ts b/packages/core/src/highlight/code-to-hast.ts index 8740b2348..f2a02facb 100644 --- a/packages/core/src/highlight/code-to-hast.ts +++ b/packages/core/src/highlight/code-to-hast.ts @@ -128,6 +128,7 @@ export function tokensToHast( tagName: 'pre', properties, children: [], + data: options.data as any, } let codeNode: Element = { diff --git a/packages/core/test/hast.test.ts b/packages/core/test/hast.test.ts index b16ba0801..1f4bbe4d8 100644 --- a/packages/core/test/hast.test.ts +++ b/packages/core/test/hast.test.ts @@ -166,4 +166,23 @@ describe('merge same style', () => { expect(html).toMatchInlineSnapshot(`"
name: CI
"`) }) + + it('supports data', async () => { + using shiki = await createHighlighter({ + themes: ['vitesse-light'], + langs: ['javascript'], + }) + + const hast = shiki.codeToHast('console.log', { + lang: 'js', + theme: 'vitesse-light', + data: { + meta: 'foo="bar"', + }, + }) + + expect((hast.children[0] as any).data).toEqual({ + meta: 'foo="bar"', + }) + }) }) diff --git a/packages/types/src/options.ts b/packages/types/src/options.ts index 38f25186c..46cec949b 100644 --- a/packages/types/src/options.ts +++ b/packages/types/src/options.ts @@ -144,6 +144,11 @@ export interface CodeToHastOptionsCommon DecorationOptions, Pick { + /** + * Data to be added to the root `
` element.
+   */
+  data?: Record
+
   /**
    * The grammar name for the code.
    */