Skip to content

Commit a6a4402

Browse files
feat(transformers): add classActiveCode option to notation transformers (#1171)
1 parent ac7f5e9 commit a6a4402

File tree

6 files changed

+140
-0
lines changed

6 files changed

+140
-0
lines changed

packages/transformers/src/transformers/notation-diff.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ export interface TransformerNotationDiffOptions extends MatchAlgorithmOptions {
1515
* Class added to the <pre> element when the current code has diff
1616
*/
1717
classActivePre?: string
18+
/**
19+
* Class added to the <code> element when the current code has diff
20+
*/
21+
classActiveCode?: string
1822
}
1923

2024
/**
@@ -27,6 +31,7 @@ export function transformerNotationDiff(
2731
classLineAdd = 'diff add',
2832
classLineRemove = 'diff remove',
2933
classActivePre = 'has-diff',
34+
classActiveCode,
3035
} = options
3136

3237
return transformerNotationMap(
@@ -36,6 +41,7 @@ export function transformerNotationDiff(
3641
'--': classLineRemove,
3742
},
3843
classActivePre,
44+
classActiveCode,
3945
matchAlgorithm: options.matchAlgorithm,
4046
},
4147
'@shikijs/transformers:notation-diff',

packages/transformers/src/transformers/notation-error-level.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ export interface TransformerNotationErrorLevelOptions extends MatchAlgorithmOpti
88
* Class added to the <pre> element when the current code has diff
99
*/
1010
classActivePre?: string
11+
/**
12+
* Class added to the <code> element when the current code has diff
13+
*/
14+
classActiveCode?: string
1115
}
1216

1317
/**
@@ -22,12 +26,14 @@ export function transformerNotationErrorLevel(
2226
warning: ['highlighted', 'warning'],
2327
},
2428
classActivePre = 'has-highlighted',
29+
classActiveCode,
2530
} = options
2631

2732
return transformerNotationMap(
2833
{
2934
classMap,
3035
classActivePre,
36+
classActiveCode,
3137
matchAlgorithm: options.matchAlgorithm,
3238
},
3339
'@shikijs/transformers:notation-error-level',

packages/transformers/src/transformers/notation-focus.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ export interface TransformerNotationFocusOptions extends MatchAlgorithmOptions {
1111
* Class added to the root element when the code has focused lines
1212
*/
1313
classActivePre?: string
14+
/**
15+
* Class added to the <code> element when the code has focused lines
16+
*/
17+
classActiveCode?: string
1418
}
1519

1620
/**
@@ -22,6 +26,7 @@ export function transformerNotationFocus(
2226
const {
2327
classActiveLine = 'focused',
2428
classActivePre = 'has-focused',
29+
classActiveCode,
2530
} = options
2631

2732
return transformerNotationMap(
@@ -30,6 +35,7 @@ export function transformerNotationFocus(
3035
focus: classActiveLine,
3136
},
3237
classActivePre,
38+
classActiveCode,
3339
matchAlgorithm: options.matchAlgorithm,
3440
},
3541
'@shikijs/transformers:notation-focus',

packages/transformers/src/transformers/notation-highlight.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ export interface TransformerNotationHighlightOptions extends MatchAlgorithmOptio
1111
* Class added to the root element when the code has highlighted lines
1212
*/
1313
classActivePre?: string
14+
/**
15+
* Class added to the <code> element when the code has highlighted lines
16+
*/
17+
classActiveCode?: string
1418
}
1519

1620
/**
@@ -22,6 +26,7 @@ export function transformerNotationHighlight(
2226
const {
2327
classActiveLine = 'highlighted',
2428
classActivePre = 'has-highlighted',
29+
classActiveCode,
2530
} = options
2631

2732
return transformerNotationMap(
@@ -31,6 +36,7 @@ export function transformerNotationHighlight(
3136
hl: classActiveLine,
3237
},
3338
classActivePre,
39+
classActiveCode,
3440
matchAlgorithm: options.matchAlgorithm,
3541
},
3642
'@shikijs/transformers:notation-highlight',

packages/transformers/src/transformers/notation-map.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ export interface TransformerNotationMapOptions extends MatchAlgorithmOptions {
88
* Class added to the <pre> element when the current code has diff
99
*/
1010
classActivePre?: string
11+
/**
12+
* Class added to the <code> element when the current code has diff
13+
*/
14+
classActiveCode?: string
1115
}
1216

1317
function escapeRegExp(str: string): string {
@@ -21,6 +25,7 @@ export function transformerNotationMap(
2125
const {
2226
classMap = {},
2327
classActivePre = undefined,
28+
classActiveCode = undefined,
2429
} = options
2530

2631
return createCommentNotationTransformer(
@@ -35,6 +40,8 @@ export function transformerNotationMap(
3540

3641
if (classActivePre)
3742
this.addClassToHast(this.pre, classActivePre)
43+
if (classActiveCode)
44+
this.addClassToHast(this.code, classActiveCode)
3845
return true
3946
},
4047
options.matchAlgorithm,
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import { codeToHtml } from 'shiki'
2+
import { describe, expect, it } from 'vitest'
3+
import {
4+
transformerNotationDiff,
5+
transformerNotationErrorLevel,
6+
transformerNotationFocus,
7+
transformerNotationHighlight,
8+
} from '../src'
9+
10+
describe('classActiveCode option', () => {
11+
it('transformerNotationDiff adds class to code element', async () => {
12+
const code = `console.log('hello') // [!code ++]`
13+
14+
const html = await codeToHtml(code, {
15+
lang: 'js',
16+
theme: 'github-dark',
17+
transformers: [
18+
transformerNotationDiff({
19+
classActiveCode: 'has-diff-code',
20+
}),
21+
],
22+
})
23+
24+
expect(html).toContain('class="has-diff-code"')
25+
expect(html).toContain('<code class="has-diff-code">')
26+
})
27+
28+
it('transformerNotationFocus adds class to code element', async () => {
29+
const code = `console.log('hello') // [!code focus]`
30+
31+
const html = await codeToHtml(code, {
32+
lang: 'js',
33+
theme: 'github-dark',
34+
transformers: [
35+
transformerNotationFocus({
36+
classActiveCode: 'has-focus-code',
37+
}),
38+
],
39+
})
40+
41+
expect(html).toContain('<code class="has-focus-code">')
42+
})
43+
44+
it('transformerNotationHighlight adds class to code element', async () => {
45+
const code = `console.log('hello') // [!code highlight]`
46+
47+
const html = await codeToHtml(code, {
48+
lang: 'js',
49+
theme: 'github-dark',
50+
transformers: [
51+
transformerNotationHighlight({
52+
classActiveCode: 'has-highlight-code',
53+
}),
54+
],
55+
})
56+
57+
expect(html).toContain('<code class="has-highlight-code">')
58+
})
59+
60+
it('transformerNotationErrorLevel adds class to code element', async () => {
61+
const code = `console.log('hello') // [!code error]`
62+
63+
const html = await codeToHtml(code, {
64+
lang: 'js',
65+
theme: 'github-dark',
66+
transformers: [
67+
transformerNotationErrorLevel({
68+
classActiveCode: 'has-error-code',
69+
}),
70+
],
71+
})
72+
73+
expect(html).toContain('<code class="has-error-code">')
74+
})
75+
76+
it('works together with classActivePre', async () => {
77+
const code = `console.log('hello') // [!code ++]`
78+
79+
const html = await codeToHtml(code, {
80+
lang: 'js',
81+
theme: 'github-dark',
82+
transformers: [
83+
transformerNotationDiff({
84+
classActivePre: 'has-diff-pre',
85+
classActiveCode: 'has-diff-code',
86+
}),
87+
],
88+
})
89+
90+
expect(html).toContain('class="shiki github-dark has-diff-pre"')
91+
expect(html).toContain('<code class="has-diff-code">')
92+
})
93+
94+
it('does not add class when no notation is present', async () => {
95+
const code = `console.log('hello')`
96+
97+
const html = await codeToHtml(code, {
98+
lang: 'js',
99+
theme: 'github-dark',
100+
transformers: [
101+
transformerNotationDiff({
102+
classActiveCode: 'has-diff-code',
103+
}),
104+
],
105+
})
106+
107+
expect(html).not.toContain('has-diff-code')
108+
})
109+
})

0 commit comments

Comments
 (0)