Skip to content

Commit f14f239

Browse files
authored
feat: add handling for FontStyle.Strikethrough (#976)
1 parent f8ef446 commit f14f239

File tree

6 files changed

+48
-20
lines changed

6 files changed

+48
-20
lines changed

docs/.vitepress/theme/transformers.css

+22
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,25 @@
2222
margin: -1px -3px;
2323
border-radius: 4px;
2424
}
25+
26+
.vp-doc [class*='language-'] pre {
27+
background-color: var(--shiki-light-bg, inherit);
28+
}
29+
30+
.dark .vp-doc [class*='language-'] pre {
31+
background-color: var(--shiki-dark-bg, inherit);
32+
}
33+
34+
.vp-code,
35+
.vp-code span:not(.line.highlighted):not(.line.highlighted *) {
36+
background-color: var(--shiki-light-bg, inherit);
37+
text-decoration: var(--shiki-light-text-decoration, inherit);
38+
font-weight: var(--shiki-light-font-weight, inherit);
39+
}
40+
41+
.dark .vp-code,
42+
.dark .vp-code span:not(.line.highlighted):not(.line.highlighted *) {
43+
background-color: var(--shiki-dark-bg, inherit);
44+
text-decoration: var(--shiki-dark-text-decoration, inherit);
45+
font-weight: var(--shiki-dark-font-weight, inherit);
46+
}

docs/languages.md

+10-17
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { codeToHtml } from 'shiki'
2121
2222
const html = codeToHtml('console.log("Hello World")', {
2323
lang: 'text', // [!code hl]
24-
theme: 'vitesse-light',
24+
theme: 'vitesse-light',
2525
})
2626
```
2727

@@ -32,22 +32,15 @@ const html = codeToHtml('console.log("Hello World")', {
3232
A special processed language `ansi` is provided to highlight terminal outputs. For example:
3333

3434
```ansi
35-
┌ Welcome to VitePress!
36-
│
37-
◇ Where should VitePress initialize the config?
38-
│ ./docs
39-
│
40-
◇ Site title:
41-
│ My Awesome Project
42-
│
43-
◇ Site description:
44-
│ A VitePress Site
45-
│
46-
◆ Theme:
47-
│ ● Default Theme (Out of the box, good-looking docs)
48-
│ ○ Default Theme + Customization
49-
│ ○ Custom Theme
50-
└
35+
colored foreground
36+
colored background
37+
38+
bold text
39+
dimmed text
40+
underlined text
41+
reversed text
42+
strikethrough text
43+
underlined + strikethrough text
5144
```
5245

5346
Check the [raw markdown of code snippet above](https://github.com/shikijs/shiki/blob/main/docs/languages.md?plain=1#L35).

packages/cli/src/code-to-ansi.ts

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ export async function codeToANSI(code: string, lang: BundledLanguage, theme: Bun
2828
text = c.italic(text)
2929
if (token.fontStyle & FontStyle.Underline)
3030
text = c.underline(text)
31+
if (token.fontStyle & FontStyle.Strikethrough)
32+
text = c.strikethrough(text)
3133
}
3234
output += text
3335
}

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,11 @@ function mergeWhitespaceTokens(tokens: ThemedToken[][]): ThemedToken[][] {
232232
let carryOnContent = ''
233233
let firstOffset = 0
234234
line.forEach((token, idx) => {
235-
const isUnderline = token.fontStyle && token.fontStyle & FontStyle.Underline
236-
const couldMerge = !isUnderline
235+
const isDecorated = token.fontStyle && (
236+
(token.fontStyle & FontStyle.Underline)
237+
|| (token.fontStyle & FontStyle.Strikethrough)
238+
)
239+
const couldMerge = !isDecorated
237240
if (couldMerge && token.content.match(/^\s+$/) && line[idx + 1]) {
238241
if (!firstOffset)
239242
firstOffset = token.offset

packages/core/src/highlight/code-to-tokens-ansi.ts

+3
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ export function tokenizeAnsiWithTheme(
5757
if (token.decorations.has('underline'))
5858
fontStyle |= FontStyle.Underline
5959

60+
if (token.decorations.has('strikethrough'))
61+
fontStyle |= FontStyle.Strikethrough
62+
6063
return {
6164
content: token.value,
6265
offset: line[1], // TODO: more accurate offset? might need to fork ansi-sequence-parser

packages/core/src/utils/tokens.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,13 @@ export function getTokenStyleObject(token: TokenStyles): Record<string, string>
116116
styles['font-style'] = 'italic'
117117
if (token.fontStyle & FontStyle.Bold)
118118
styles['font-weight'] = 'bold'
119+
const decorations = []
119120
if (token.fontStyle & FontStyle.Underline)
120-
styles['text-decoration'] = 'underline'
121+
decorations.push('underline')
122+
if (token.fontStyle & FontStyle.Strikethrough)
123+
decorations.push('line-through')
124+
if (decorations.length)
125+
styles['text-decoration'] = decorations.join(' ')
121126
}
122127
return styles
123128
}

0 commit comments

Comments
 (0)