简体中文
shiki-twoslash
@vuepress/plugin-shiki 的 Twoslash 集成。
Twoslash 会用 TypeScript 编译器执行代码示例,从而在构建时得知其类型、错误与补全信息。该包把 Twoslash 的结果转换为代码示例的标记,并把信息渲染为读者悬停代码时出现的弹窗。它是 @vuepress/plugin-shiki 在其 twoslash 选项启用时加载的可选依赖。
使用
npm i @vuepress/shiki-twoslash@next只有 @vuepress/plugin-shiki 使用它,为控制站点体积,该插件默认不安装它。安装后在插件中启用该功能:
import { shikiPlugin } from '@vuepress/plugin-shiki'
export default {
plugins: [
shikiPlugin({
twoslash: true,
}),
],
}twoslash 选项也可以接受下文选项构成的对象,需要检查的代码块必须在信息串中带上 twoslash 标记。见 Shiki > Twoslash 支持。
指南
运行 Twoslash
代码块的信息串中带有 twoslash 标记时才会被检查,默认的 explicitTrigger 选项匹配的正是该标记:
```ts twoslash
const a = 1
const b = 23
console.log(a + b)
```转换器只处理 ts、tsx、js、jsx、json 与 vue 代码块。Vue 代码块会先由 twoslash-vue 编译,因此单文件组件中的脚本同样可以被检查。
转换器会关闭这些代码块的 v-pre 处理,因为弹窗的标记需要由 Vue 编译,所以不要为 Twoslash 代码块添加 :v-pre 标记。生成的标记中的花括号会被转义,以免 Vue 把它们当作插值。
解析类型
Twoslash 需要站点的 TypeScript 编译选项。转换器会从 Twoslash 的默认选项开始,加入当前工作目录的 baseUrl,并把工作目录中 tsconfig.json 的 paths 与 twoslashOptions 选项的 paths 合并,从而让站点的别名在代码示例中生效:
import { shikiPlugin } from '@vuepress/plugin-shiki'
export default {
plugins: [
shikiPlugin({
twoslash: {
twoslashOptions: {
compilerOptions: {
lib: ['es2015', 'dom'],
},
},
},
}),
],
}Twoslash 出错时,CI 与生产构建会失败,而开发服务器只记录错误。throws 选项可以强制指定其中一种行为。
缓存类型
Twoslash 较慢,因此可以把每个代码示例的结果缓存到文件系统,在示例变化之前一直复用。createFileSystemTypesCache() 会把示例的结果写入以其代码哈希命名的 JSON 文件:
import { shikiPlugin } from '@vuepress/plugin-shiki'
import { createFileSystemTypesCache } from '@vuepress/shiki-twoslash'
import { path } from 'vuepress/utils'
export default {
plugins: [
shikiPlugin({
twoslash: {
typesCache: createFileSystemTypesCache({
dir: path.resolve(import.meta.dirname, '.cache/twoslash'),
}),
},
}),
],
}缓存目录会在缓存初始化时创建,因此无需事先存在。记得在版本控制中忽略它。
渲染弹窗
弹窗使用 FloatingVue 菜单渲染,因此插件的客户端配置需要注册组件,并导入 FloatingVue 与该包的样式:
import { enhanceTwoslash } from '@vuepress/shiki-twoslash/client'
import { defineClientConfig } from 'vuepress/client'
import '@vuepress/shiki-twoslash/twoslash.css'
export default defineClientConfig({
enhance({ app }) {
enhanceTwoslash(app)
},
})enhanceTwoslash() 会以渲染器使用的主题注册 FloatingVue:twoslash 用于悬停与查询弹窗,twoslash-completion 用于补全列表。由于弹窗在打开时才定位,切换代码选项卡时弹窗会重新计算位置。
Node.js 端 API
createTwoslashTransformer
options
ShikiTwoslashOptions转换器的选项,见选项。
它返回该包的 ShikiTransformer,名称为 vuepress:twoslash,它会在被触发的代码块上运行 Twoslash,并用 rendererFloatingVue() 渲染结果。它是异步函数,因为创建转换器前需要读取工作目录的 tsconfig.json。
@vuepress/plugin-shiki 会自行加载它,只有在你编写自己的高亮集成时才需要直接使用。
rendererFloatingVue
options
TwoslashFloatingVueRendererOptions渲染器选项,即 Twoslash 的 RendererRichOptions 与下文的 floatingVue 选项。
它返回该包的渲染器,即一个 rendererRich,把每个弹窗渲染为使用 enhanceTwoslash() 所注册主题名的 FloatingVue 菜单。代码示例的错误默认渲染在其所在行上。
createFileSystemTypesCache
dir必填
string存放缓存结果的目录。
它返回 TwoslashTypesCache,把代码示例的结果读写为目录下的 <hash>.json。目录由缓存的 init() 钩子创建。
resolveTypeScriptPaths
它读取工作目录中 tsconfig.json 的 compilerOptions.paths,并按 baseUrl 解析。文件不存在或无法解析时返回 null,否则返回 TypeScriptPaths,即 Record<string, string[]>。
客户端 API
enhanceTwoslash
app必填
App站点的 Vue 应用实例。
它把该包的 FloatingVue 组件与主题注册到应用,并导入 FloatingVue 的样式。需要在插件的客户端配置中调用一次,同时导入 @vuepress/shiki-twoslash/twoslash.css。
选项
ShikiTwoslashOptions
explicitTrigger
RegExp | boolean默认值
trueTwoslash 是否只运行在带有标记的代码块上。true 匹配 twoslash 标记,RegExp 替换该标记,false 会在所有支持的代码块上运行 Twoslash。
twoslashOptions
TransformerTwoslashOptions['twoslashOptions'] & VueSpecificOptionsTwoslash 自身的选项,在其默认值之后合并。其中的 compilerOptions 会与站点解析出的 paths 合并。
typesCache
TwoslashTypesCache解析类型的缓存,例如 createFileSystemTypesCache() 返回的缓存。
throws
booleanTwoslash 出错时是否让构建失败。它的默认值来自 process.env.CI 或生产环境,因此出错会在 CI 与生产构建中导致失败。
errorRendering
'line' | 'hover'默认值
'line'代码示例的错误渲染位置,继承自 RendererRichOptions。'line' 标记对应行,'hover' 只在弹窗中展示。
该选项还扩展了 @shikijs/twoslash 的 TransformerTwoslashOptions 与其富文本渲染器的 RendererRichOptions。
TwoslashFloatingVueOptions
classCopyIgnore
string默认值
'vp-copy-ignore'复制代码按钮需要忽略的元素的类名。
classFloatingPanel
string默认值
'twoslash-floating'弹窗浮动面板的类名。
classCode
string默认值
'vp-code'弹窗中代码元素的类名。
classMarkdown
string默认值
''弹窗中 Markdown 元素的类名。
attrMarkdown
string默认值
'vp-content'标记弹窗中 Markdown 内容的属性名。
floatingVueTheme
string默认值
'twoslash'悬停与查询弹窗的 FloatingVue 主题。
floatingVueThemeQuery
string默认值
'twoslash-query'查询弹窗的 FloatingVue 主题。
floatingVueThemeCompletion
string默认值
'twoslash-completion'补全弹窗的 FloatingVue 主题。
它们作为 ShikiTwoslashOptions 与 TwoslashFloatingVueRendererOptions 的 floatingVue 选项传入。
