Skip to content

Commit 38a9fdc

Browse files
committed
feat(twoslash): allow lossy custom twoslash implementation
1 parent 6b17733 commit 38a9fdc

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

packages/twoslash/src/core.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
* This file is the core of the @shikijs/twoslash package,
33
* Decoupled from twoslash's implementation and allowing to introduce custom implementation or cache system.
44
*/
5-
import type { TwoslashExecuteOptions, TwoslashReturn } from 'twoslash'
5+
import type { TwoslashExecuteOptions } from 'twoslash'
66
import type { ShikiTransformer } from '@shikijs/core'
77
import type { Element, ElementContent, Text } from 'hast'
88

99
import { splitTokens } from '@shikijs/core'
10-
import type { TransformerTwoslashOptions, TwoslashRenderer } from './types'
10+
import type { TransformerTwoslashOptions, TwoslashRenderer, TwoslashShikiReturn } from './types'
1111
import { ShikiTwoslashError } from './error'
1212

1313
export * from './types'
@@ -22,7 +22,7 @@ export function defaultTwoslashOptions(): TwoslashExecuteOptions {
2222
}
2323
}
2424

25-
export type TwoslashFunction = (code: string, lang?: string, options?: TwoslashExecuteOptions) => TwoslashReturn
25+
export type TwoslashFunction = (code: string, lang?: string, options?: TwoslashExecuteOptions) => TwoslashShikiReturn
2626

2727
export function createTransformerFactory(
2828
defaultTwoslasher: TwoslashFunction,
@@ -60,7 +60,7 @@ export function createTransformerFactory(
6060
if (filter(lang, code, this.options)) {
6161
const twoslash = twoslasher(code, lang, twoslashOptions)
6262
this.meta.twoslash = twoslash
63-
this.options.lang = twoslash.meta.extension || lang
63+
this.options.lang = twoslash.meta?.extension || lang
6464
return twoslash.code
6565
}
6666
},

packages/twoslash/src/types.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@ import type { NodeCompletion, NodeError, NodeHighlight, NodeHover, NodeQuery, No
22
import type { CodeToHastOptions, ShikiTransformerContext } from '@shikijs/core'
33
import type { Element, ElementContent, Text } from 'hast'
44

5+
// We only pick necessary types to Shiki, making passing custom twoslash implementation easier
6+
export type TwoslashShikiReturn =
7+
Pick<TwoslashReturn, 'nodes' | 'code'> & {
8+
meta?: Partial<Pick<TwoslashReturn['meta'], 'extension'>>
9+
}
10+
511
declare module '@shikijs/core' {
612
interface ShikiTransformerContextMeta {
7-
twoslash?: TwoslashReturn
13+
twoslash?: TwoslashShikiReturn
814
}
915
}
1016

0 commit comments

Comments
 (0)