createShikiTransformer()
Create a standalone Shiki transformer for lower-level control. Unlike createShikiPlugin(), this returns a raw transformer object that you can apply manually to HAST trees.
import { createShikiTransformer } from "@unifast/shiki";Signature
async function createShikiTransformer(
options?: ShikiTransformerOptions,
): Promise<ShikiTransformer>Parameters
options?
Shiki configuration (themes, languages)
| Property | Type | Default | Description |
|---|---|---|---|
themes | BundledTheme[] | ["github-dark"] | Shiki themes to load |
defaultTheme | BundledTheme | First theme in themes | Default theme for rendering |
langs | BundledLanguage[] | [] | Languages to load. Only loaded languages will be highlighted. |
Usage
import { compile } from "@unifast/node";
import { createShikiTransformer } from "@unifast/shiki";
import type { HastRoot } from "@unifast/core";
const transformer = await createShikiTransformer({
themes: ["github-dark", "github-light"],
defaultTheme: "github-dark",
langs: ["typescript", "rust", "json"],
});
const result = compile(md, { outputKind: "hast" });
const hast: HastRoot = JSON.parse(result.output as string);
const highlighted = transformer.transform(hast);
console.log(highlighted);
// HAST tree with Shiki-highlighted code blocksExamples
Manual HAST transformation
import { compile } from "@unifast/node";
import { createShikiTransformer } from "@unifast/shiki";
import { hastToHtml } from "@unifast/core";
import type { HastRoot } from "@unifast/core";
const transformer = await createShikiTransformer({
themes: ["github-dark"],
langs: ["typescript"],
});
const result = compile("```ts\nconst x = 1;\n```", { outputKind: "hast" });
const hast: HastRoot = JSON.parse(result.output as string);
const highlighted = transformer.transform(hast);
const html = hastToHtml(highlighted);
console.log(html);
// <pre> with Shiki-highlighted <span> elementsWith React rendering
import { createElement, Fragment } from "react";
import { compile } from "@unifast/node";
import { createShikiTransformer } from "@unifast/shiki";
import { hastToReact } from "@unifast/react";
import type { HastRoot } from "@unifast/core";
const transformer = await createShikiTransformer({
themes: ["github-dark"],
langs: ["typescript"],
});
const result = compile(md, { outputKind: "hast" });
const hast: HastRoot = JSON.parse(result.output as string);
const highlighted = transformer.transform(hast);
const element = hastToReact(highlighted, { createElement, Fragment });