1- import {
2- createTransformerFactory ,
3- rendererRich ,
4- transformerTwoslash ,
5- type TransformerTwoslashOptions ,
6- } from '@shikijs/twoslash' ;
1+ import { transformerTwoslash } from '@shikijs/twoslash' ;
72import type { Element , Root } from 'hast' ;
83import { toString } from 'hast-util-to-string' ;
94import type { MdxJsxFlowElementHast , MdxJsxTextElementHast } from 'mdast-util-mdx-jsx' ;
105import { createHighlighter , type Highlighter } from 'shiki' ;
11- import { createTwoslashFromCDN } from 'twoslash-cdn' ;
12- import ts from 'typescript' ;
136import type { Plugin } from 'unified' ;
147import { visit } from 'unist-util-visit' ;
158
@@ -27,30 +20,14 @@ import {
2720 DEFAULT_LANGS ,
2821 SHIKI_TRANSFORMERS ,
2922} from './shiki-constants.js' ;
23+ import {
24+ cdnTransformerTwoslash ,
25+ cdnTwoslash ,
26+ getTwoslashOptions ,
27+ parseLineComment ,
28+ } from './twoslash/config.js' ;
3029import { getLanguage } from './utils.js' ;
3130
32- const twoslashCompilerOptions = {
33- target : ts . ScriptTarget . ESNext ,
34- lib : [ 'ESNext' , 'DOM' , 'esnext' , 'dom' , 'es2020' ] ,
35- } ;
36-
37- const twoslashOptions : TransformerTwoslashOptions = {
38- onTwoslashError ( err , code , lang ) {
39- console . error ( JSON . stringify ( { err, code, lang } ) ) ;
40- } ,
41- onShikiError ( err , code , lang ) {
42- console . error ( JSON . stringify ( { err, code, lang } ) ) ;
43- } ,
44- renderer : rendererRich ( ) ,
45- langs : [ 'ts' , 'typescript' , 'js' , 'javascript' , 'tsx' , 'jsx' ] ,
46- explicitTrigger : / m i n t - t w o s l a s h / ,
47- twoslashOptions : { compilerOptions : twoslashCompilerOptions } ,
48- } ;
49-
50- const cdnTwoslash = createTwoslashFromCDN ( { compilerOptions : twoslashCompilerOptions } ) ;
51-
52- const cdnTransformerTwoslash = createTransformerFactory ( cdnTwoslash . runSync ) ;
53-
5431export type RehypeSyntaxHighlightingOptions = {
5532 theme ?: ShikiTheme ;
5633 themes ?: Record < 'light' | 'dark' , ShikiTheme > ;
@@ -145,7 +122,7 @@ const traverseNode = (
145122 options : RehypeSyntaxHighlightingOptions
146123) => {
147124 try {
148- const code = toString ( node ) ;
125+ let code = toString ( node ) ;
149126
150127 const meta = node . data ?. meta ?. split ( ' ' ) ?? [ ] ;
151128 const twoslashIndex = meta . findIndex ( ( str ) => str . toLowerCase ( ) === 'mint-twoslash' ) ;
@@ -156,6 +133,20 @@ const traverseNode = (
156133 node . data . meta = meta . join ( ' ' ) . trim ( ) || undefined ;
157134 }
158135
136+ const linkMap : Map < string , string > = new Map ( ) ;
137+ const splitCode = code . split ( '\n' ) ;
138+ for ( const [ i , line ] of splitCode . entries ( ) ) {
139+ const parsedLineComment = parseLineComment ( line ) ;
140+ if ( ! parsedLineComment ) continue ;
141+ const { word, href } = parsedLineComment ;
142+ linkMap . set ( word , href ) ;
143+ splitCode . splice ( i , 1 ) ;
144+ }
145+
146+ code = splitCode . join ( '\n' ) ;
147+
148+ const twoslashOptions = getTwoslashOptions ( { linkMap } ) ;
149+
159150 const hast = highlighter . codeToHast ( code , {
160151 lang : lang ?? DEFAULT_LANG ,
161152 meta : shouldUseTwoslash ? { __raw : 'mint-twoslash' } : undefined ,
0 commit comments