Skip to content

Commit e883303

Browse files
committed
feat(html): add mergeWhitespaces option, thanks to @dominikg
1 parent 8f7b0d7 commit e883303

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

packages/shikiji/src/core/renderer-html.ts

+32
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ const defaultElements: ElementsOptions = {
1717
}
1818

1919
export function renderToHtml(lines: ThemedToken[][], options: HtmlRendererOptions = {}) {
20+
const {
21+
mergeWhitespaces = true,
22+
} = options
23+
24+
if (mergeWhitespaces)
25+
lines = mergeWhitespaceTokens(lines)
26+
2027
const bg = options.bg || '#fff'
2128
const fg = options.bg || '#000'
2229
const optionsByLineNumber = groupBy(options.lineOptions ?? [], option => option.line)
@@ -127,3 +134,28 @@ export function groupBy<TElement, TKey>(
127134
}
128135
return map
129136
}
137+
138+
function mergeWhitespaceTokens(tokens: ThemedToken[][]) {
139+
return tokens.map((line) => {
140+
const newLine: ThemedToken[] = []
141+
let carryOnContent = ''
142+
line.forEach((token, idx) => {
143+
if (token.content.match(/^\s+$/) && line[idx + 1]) {
144+
carryOnContent += token.content
145+
}
146+
else {
147+
if (carryOnContent) {
148+
newLine.push({
149+
...token,
150+
content: carryOnContent + token.content,
151+
})
152+
carryOnContent = ''
153+
}
154+
else {
155+
newLine.push(token)
156+
}
157+
}
158+
})
159+
return newLine
160+
})
161+
}

packages/shikiji/src/core/themedTokenizer.ts

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import type { ThemedToken, ThemedTokenExplanation, ThemedTokenScopeExplanation }
99
import type { FontStyle } from './stackElementMetadata'
1010
import { StackElementMetadata } from './stackElementMetadata'
1111

12+
export interface TokenizeWithThemeOptions {
13+
includeExplanation?: boolean
14+
}
15+
1216
export function tokenizeWithTheme(
1317
fileContents: string,
1418
grammar: IGrammar,

packages/shikiji/src/types.ts

+7
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,13 @@ export interface HtmlRendererOptions {
175175
* When specified, `fg` and `bg` will be ignored.
176176
*/
177177
rootStyle?: string
178+
/**
179+
* Merge token with only whitespace to the next token,
180+
* Saving a few extra `<span>`
181+
*
182+
* @default true
183+
*/
184+
mergeWhitespaces?: boolean
178185
}
179186

180187
export interface ElementsOptions {

packages/shikiji/test/core.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ describe('should', () => {
5555
`)
5656

5757
expect(shiki.codeToHtml('print 1', { lang: 'python', theme: 'vitesse-light' }))
58-
.toMatchInlineSnapshot('"<pre class=\\"shiki vitesse-light\\" style=\\"background-color:#ffffff;color:#ffffff\\" tabindex=\\"0\\"><code><span class=\\"line\\"><span style=\\"color:#998418\\">print</span><span style=\\"color:#393A34\\"> </span><span style=\\"color:#2F798A\\">1</span></span></code></pre>"')
58+
.toMatchInlineSnapshot('"<pre class=\\"shiki vitesse-light\\" style=\\"background-color:#ffffff;color:#ffffff\\" tabindex=\\"0\\"><code><span class=\\"line\\"><span style=\\"color:#998418\\">print</span><span style=\\"color:#2F798A\\"> 1</span></span></code></pre>"')
5959
})
6060

6161
it('requires nested lang', async () => {

0 commit comments

Comments
 (0)