createShikiPlugin()
创建一个基于 Shiki 的语法高亮插件。这是一个异步函数,会初始化 Shiki 高亮器并返回一个带有 HAST 转换的 UnifastPlugin。
import { createShikiPlugin } from "@unifast/shiki";签名
async function createShikiPlugin(
options?: ShikiTransformerOptions,
): Promise<UnifastPlugin>参数
options?
Shiki 配置(主题、语言)
| 属性 | 类型 | 默认值 | 描述 |
|---|---|---|---|
themes | BundledTheme[] | ["github-dark"] | 要加载的 Shiki 主题 |
defaultTheme | BundledTheme | themes 中的第一个主题 | 渲染时使用的默认主题 |
langs | BundledLanguage[] | [] | 要加载的语言。只有已加载的语言才会被高亮。 |
用法
import { compile } from "@unifast/node";
import { createShikiPlugin } from "@unifast/shiki";
const shiki = await createShikiPlugin({
themes: ["github-dark", "github-light"],
defaultTheme: "github-dark",
langs: ["typescript", "rust", "json", "html", "css"],
});
const result = compile(md, {
plugins: [shiki],
});
console.log(result.output);
// Code blocks are highlighted with Shiki-generated <span> elements示例
单一主题
import { compile } from "@unifast/node";
import { createShikiPlugin } from "@unifast/shiki";
const shiki = await createShikiPlugin({
themes: ["github-dark"],
langs: ["typescript"],
});
const result = compile("```ts\nconst x = 1;\n```", {
plugins: [shiki],
});
console.log(result.output);
// <pre> with Shiki-highlighted <span> elements多主题
import { createShikiPlugin } from "@unifast/shiki";
const shiki = await createShikiPlugin({
themes: ["github-dark", "github-light", "dracula"],
defaultTheme: "github-dark",
langs: ["typescript", "rust", "python"],
});
// Use with compile() — defaultTheme is applied to code blocks与其他插件搭配使用
import { compile, gfm, frontmatter } from "@unifast/node";
import { createShikiPlugin } from "@unifast/shiki";
const shiki = await createShikiPlugin({
themes: ["github-dark"],
langs: ["typescript"],
});
const result = compile(md, {
plugins: [gfm(), frontmatter(), shiki],
});