Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/core/src/highlight/code-to-hast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export function tokensToHast(
tagName: 'pre',
properties,
children: [],
data: options.data as any,
}

let codeNode: Element = {
Expand Down
19 changes: 19 additions & 0 deletions packages/core/test/hast.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,23 @@ describe('merge same style', () => {

expect(html).toMatchInlineSnapshot(`"<pre class="shiki min-light" style="background-color:#ffffff;color:#24292eff" tabindex="0"><code><span class="line"><span style="color:#D32F2F" class="highlighted-word">name</span><span style="color:#D32F2F">:</span><span style="color:#22863A"> CI</span></span></code></pre>"`)
})

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"',
})
})
})
5 changes: 5 additions & 0 deletions packages/types/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ export interface CodeToHastOptionsCommon<Languages extends string = string>
DecorationOptions,
Pick<TokenizeWithThemeOptions, 'colorReplacements' | 'tokenizeMaxLineLength' | 'tokenizeTimeLimit' | 'grammarState' | 'grammarContextCode' | 'includeExplanation'> {

/**
* Data to be added to the root `<pre>` element.
*/
data?: Record<string, unknown>

/**
* The grammar name for the code.
*/
Expand Down