Skip to content

Commit

Permalink
feat(twoslash): introduce disableTriggers, close #780
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Sep 19, 2024
1 parent 644a244 commit dca6a48
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 2 deletions.
9 changes: 8 additions & 1 deletion packages/twoslash/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export function createTransformerFactory(
},
twoslasher = defaultTwoslasher,
explicitTrigger = false,
disableTriggers = ['notwoslash', 'no-twoslash'],
renderer = defaultRenderer,
throws = true,
includesMap = new Map(),
Expand Down Expand Up @@ -67,7 +68,13 @@ export function createTransformerFactory(

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

const filter = options.filter || ((lang, _, options) => langs.includes(lang) && (!explicitTrigger || trigger.test(options.meta?.__raw || '')))
const {
filter = (lang, _, options) => {
return langs.includes(lang)
&& (!explicitTrigger || trigger.test(options.meta?.__raw || ''))
&& !disableTriggers.some(i => typeof i === 'string' ? options.meta?.__raw?.includes(i) : i.test(options.meta?.__raw || ''))
},
} = options

const includes = new TwoslashIncludesManager(includesMap)

Expand Down
8 changes: 7 additions & 1 deletion packages/twoslash/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,19 @@ export interface TransformerTwoslashOptions {
* @default false
*/
explicitTrigger?: boolean | RegExp
/**
* Triggers that skip Twoslash transformation on the code block meta
*
* @default ['notwoslash', 'no-twoslash']
*/
disableTriggers?: (string | RegExp)[]
/**
* Mapping from language alias to language name
*/
langAlias?: Record<string, string>
/**
* Custom filter function to apply this transformer to
* When specified, `langs` and `explicitTrigger` will be ignored
* When specified, `langs`, `explicitTrigger`, and `disableTriggers` will be ignored
*/
filter?: (lang: string, code: string, options: CodeToHastOptions) => boolean
/**
Expand Down
40 changes: 40 additions & 0 deletions packages/twoslash/test/markdown-it.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,44 @@ const v = 123

expect(styleTag + html).toMatchFileSnapshot('./out/markdown-it/highlight-lines.html')
})

it('with disable triggers', async () => {
const md = MarkdownIt()

md.use(await Shiki({
langs: ['ts'],
themes: {
light: 'vitesse-light',
dark: 'vitesse-dark',
},
defaultColor: false,
transformers: [
transformerMetaHighlight(),
transformerTwoslash({
explicitTrigger: false,
renderer: rendererRich(),
}),
],
}))

const html = md.render(`
# Hello
\`\`\`ts {1,3}
const a = 123
const b = 123
const v = 123
// ^?
\`\`\`
\`\`\`ts no-twoslash {2}
const a = 123
const b = 123
const v = 123
// ^?
\`\`\`
`.trim())

expect(styleTag + html).toMatchFileSnapshot('./out/markdown-it/highlight-disable-triggers.html')
})
})

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit dca6a48

Please sign in to comment.