44
55import assert from 'node:assert/strict'
66import fs from 'node:fs/promises'
7+ import { fileURLToPath } from 'node:url'
78import { unified } from 'unified'
89import remarkParse from 'remark-parse'
910import remarkGfm from 'remark-gfm'
@@ -15,6 +16,8 @@ import rehypeAutolinkHeadings from 'rehype-autolink-headings'
1516import sourceGitignore from '@wooorm/starry-night/source.gitignore'
1617import { common } from '@wooorm/starry-night'
1718import rehypeStarryNight from 'rehype-starry-night'
19+ import rehypeTwoslash from 'rehype-twoslash'
20+ import typescript from 'typescript'
1821import rehypeLink from '../plugin/rehype-link.js'
1922import rehypeRewriteUrls from '../plugin/rehype-rewrite-urls.js'
2023import rehypeAbbreviate from '../plugin/rehype-abbreviate.js'
@@ -26,6 +29,28 @@ const packageJson = JSON.parse(packageValue)
2629const origin = packageJson . homepage
2730assert ( typeof origin === 'string' )
2831
32+ const configPath = typescript . findConfigFile (
33+ fileURLToPath ( import . meta. url ) ,
34+ typescript . sys . fileExists ,
35+ 'tsconfig.json'
36+ )
37+ assert ( configPath )
38+ const commandLine = typescript . getParsedCommandLineOfConfigFile (
39+ configPath ,
40+ undefined ,
41+ {
42+ fileExists : typescript . sys . fileExists ,
43+ getCurrentDirectory : typescript . sys . getCurrentDirectory ,
44+ onUnRecoverableConfigFileDiagnostic ( x ) {
45+ console . warn ( 'Unrecoverable diagnostic' , x )
46+ } ,
47+ readDirectory : typescript . sys . readDirectory ,
48+ readFile : typescript . sys . readFile ,
49+ useCaseSensitiveFileNames : typescript . sys . useCaseSensitiveFileNames
50+ }
51+ )
52+ assert ( commandLine )
53+
2954export const article = unified ( )
3055 . use ( remarkParse )
3156 . use ( remarkGfm )
@@ -36,6 +61,7 @@ export const article = unified()
3661 grammars : [ ...common , sourceGitignore ] ,
3762 plainText : [ 'txt' ]
3863 } )
64+ . use ( rehypeTwoslash , { twoslash : { compilerOptions : commandLine . options } } )
3965 . use ( rehypeSlug )
4066 . use ( rehypeAutolinkHeadings , {
4167 behavior : 'prepend' ,
0 commit comments