Skip to content

Commit dca6a48

Browse files
committed
feat(twoslash): introduce disableTriggers, close #780
1 parent 644a244 commit dca6a48

File tree

4 files changed

+81
-2
lines changed

4 files changed

+81
-2
lines changed

packages/twoslash/src/core.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export function createTransformerFactory(
3838
},
3939
twoslasher = defaultTwoslasher,
4040
explicitTrigger = false,
41+
disableTriggers = ['notwoslash', 'no-twoslash'],
4142
renderer = defaultRenderer,
4243
throws = true,
4344
includesMap = new Map(),
@@ -67,7 +68,13 @@ export function createTransformerFactory(
6768

6869
const map = new WeakMap<ShikiTransformerContextMeta, TwoslashShikiReturn>()
6970

70-
const filter = options.filter || ((lang, _, options) => langs.includes(lang) && (!explicitTrigger || trigger.test(options.meta?.__raw || '')))
71+
const {
72+
filter = (lang, _, options) => {
73+
return langs.includes(lang)
74+
&& (!explicitTrigger || trigger.test(options.meta?.__raw || ''))
75+
&& !disableTriggers.some(i => typeof i === 'string' ? options.meta?.__raw?.includes(i) : i.test(options.meta?.__raw || ''))
76+
},
77+
} = options
7178

7279
const includes = new TwoslashIncludesManager(includesMap)
7380

packages/twoslash/src/types.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,19 @@ export interface TransformerTwoslashOptions {
3838
* @default false
3939
*/
4040
explicitTrigger?: boolean | RegExp
41+
/**
42+
* Triggers that skip Twoslash transformation on the code block meta
43+
*
44+
* @default ['notwoslash', 'no-twoslash']
45+
*/
46+
disableTriggers?: (string | RegExp)[]
4147
/**
4248
* Mapping from language alias to language name
4349
*/
4450
langAlias?: Record<string, string>
4551
/**
4652
* Custom filter function to apply this transformer to
47-
* When specified, `langs` and `explicitTrigger` will be ignored
53+
* When specified, `langs`, `explicitTrigger`, and `disableTriggers` will be ignored
4854
*/
4955
filter?: (lang: string, code: string, options: CodeToHastOptions) => boolean
5056
/**

packages/twoslash/test/markdown-it.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,44 @@ const v = 123
102102

103103
expect(styleTag + html).toMatchFileSnapshot('./out/markdown-it/highlight-lines.html')
104104
})
105+
106+
it('with disable triggers', async () => {
107+
const md = MarkdownIt()
108+
109+
md.use(await Shiki({
110+
langs: ['ts'],
111+
themes: {
112+
light: 'vitesse-light',
113+
dark: 'vitesse-dark',
114+
},
115+
defaultColor: false,
116+
transformers: [
117+
transformerMetaHighlight(),
118+
transformerTwoslash({
119+
explicitTrigger: false,
120+
renderer: rendererRich(),
121+
}),
122+
],
123+
}))
124+
125+
const html = md.render(`
126+
# Hello
127+
128+
\`\`\`ts {1,3}
129+
const a = 123
130+
const b = 123
131+
const v = 123
132+
// ^?
133+
\`\`\`
134+
135+
\`\`\`ts no-twoslash {2}
136+
const a = 123
137+
const b = 123
138+
const v = 123
139+
// ^?
140+
\`\`\`
141+
`.trim())
142+
143+
expect(styleTag + html).toMatchFileSnapshot('./out/markdown-it/highlight-disable-triggers.html')
144+
})
105145
})

packages/twoslash/test/out/markdown-it/highlight-disable-triggers.html

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)