Skip to content

Commit baf9aee

Browse files
khushthecoderantfu
andauthored
feat: support rootStyle: false option (#1184)
Co-authored-by: Anthony Fu <[email protected]>
1 parent 6ad1657 commit baf9aee

File tree

4 files changed

+52
-18
lines changed

4 files changed

+52
-18
lines changed

packages/core/src/highlight/code-to-hast.ts

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ export function codeToHast(
7676
fg,
7777
bg,
7878
themeName,
79-
rootStyle,
79+
rootStyle: options.rootStyle === false
80+
? false
81+
: options.rootStyle ?? rootStyle,
8082
},
8183
contextSource,
8284
grammarState,
@@ -102,24 +104,29 @@ export function tokensToHast(
102104
tabindex = '0',
103105
} = options
104106

107+
const properties: Element['properties'] = {
108+
class: `shiki ${options.themeName || ''}`,
109+
}
110+
111+
if (options.rootStyle !== false) {
112+
if (options.rootStyle != null)
113+
properties.style = options.rootStyle
114+
else
115+
properties.style = `background-color:${options.bg};color:${options.fg}`
116+
}
117+
118+
if (tabindex !== false && tabindex != null)
119+
properties.tabindex = tabindex.toString()
120+
121+
for (const [key, value] of Object.entries(options.meta || {})) {
122+
if (!key.startsWith('_'))
123+
properties[key] = value
124+
}
125+
105126
let preNode: Element = {
106127
type: 'element',
107128
tagName: 'pre',
108-
properties: {
109-
class: `shiki ${options.themeName || ''}`,
110-
style: options.rootStyle || `background-color:${options.bg};color:${options.fg}`,
111-
...(tabindex !== false && tabindex != null)
112-
? {
113-
tabindex: tabindex.toString(),
114-
}
115-
: {},
116-
...Object.fromEntries(
117-
Array.from(
118-
Object.entries(options.meta || {}),
119-
)
120-
.filter(([key]) => !key.startsWith('_')),
121-
),
122-
},
129+
properties,
123130
children: [],
124131
}
125132

packages/core/test/core.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,24 @@ describe('should', () => {
160160
const code3 = shiki.codeToHtml(ansiCode, { lang: 'lang3', theme: 'nord' })
161161
expect(code3).toBe(original2)
162162
})
163+
164+
it('custom root style', async () => {
165+
using shiki = await createHighlighterCore({
166+
themes: [nord],
167+
langs: [js],
168+
engine: createJavaScriptRegexEngine(),
169+
})
170+
171+
// Override root style
172+
const code1 = shiki.codeToHtml('console.log("Hi")', { lang: 'javascript', theme: 'nord', rootStyle: 'custom-style:true;' })
173+
expect(code1)
174+
.toMatchInlineSnapshot(`"<pre class="shiki nord" style="custom-style:true;" tabindex="0"><code><span class="line"><span style="color:#D8DEE9">console</span><span style="color:#ECEFF4">.</span><span style="color:#88C0D0">log</span><span style="color:#D8DEE9FF">(</span><span style="color:#ECEFF4">"</span><span style="color:#A3BE8C">Hi</span><span style="color:#ECEFF4">"</span><span style="color:#D8DEE9FF">)</span></span></code></pre>"`)
175+
176+
// Disable root style
177+
const code2 = shiki.codeToHtml('console.log("Hi")', { lang: 'javascript', theme: 'nord', rootStyle: false })
178+
expect(code2)
179+
.toMatchInlineSnapshot(`"<pre class="shiki nord" tabindex="0"><code><span class="line"><span style="color:#D8DEE9">console</span><span style="color:#ECEFF4">.</span><span style="color:#88C0D0">log</span><span style="color:#D8DEE9FF">(</span><span style="color:#ECEFF4">"</span><span style="color:#A3BE8C">Hi</span><span style="color:#ECEFF4">"</span><span style="color:#D8DEE9FF">)</span></span></code></pre>"`)
180+
})
163181
})
164182

165183
describe('errors', () => {

packages/types/src/options.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,18 @@ export interface CodeToHastOptionsCommon<Languages extends string = string>
143143
TransformerOptions,
144144
DecorationOptions,
145145
Pick<TokenizeWithThemeOptions, 'colorReplacements' | 'tokenizeMaxLineLength' | 'tokenizeTimeLimit' | 'grammarState' | 'grammarContextCode'> {
146-
146+
/**
147+
* The grammar name for the code.
148+
*/
147149
lang: StringLiteralUnion<Languages | SpecialLanguage>
148150

151+
/**
152+
* Custom style string to be applied to the root `<pre>` element.
153+
*
154+
* When set to `false`, no style will be applied.
155+
*/
156+
rootStyle?: string | false
157+
149158
/**
150159
* Merge whitespace tokens to saving extra `<span>`.
151160
*

packages/types/src/tokens.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ export interface TokensResult {
242242
* Custom style string to be applied to the root `<pre>` element.
243243
* When specified, `fg` and `bg` will be ignored.
244244
*/
245-
rootStyle?: string
245+
rootStyle?: string | false
246246

247247
/**
248248
* The last grammar state of the code snippet.

0 commit comments

Comments
 (0)