createShikiTransformer()
Cria um transformer Shiki standalone para um controle de mais baixo nível. Diferente do createShikiPlugin(), retorna um objeto transformer bruto que você pode aplicar manualmente em árvores HAST.
import { createShikiTransformer } from "@unifast/shiki";Assinatura
async function createShikiTransformer(
options?: ShikiTransformerOptions,
): Promise<ShikiTransformer>Parâmetros
options?
Configuração do Shiki (temas, linguagens)
| Propriedade | Tipo | Padrão | Descrição |
|---|---|---|---|
themes | BundledTheme[] | ["github-dark"] | Temas Shiki a carregar |
defaultTheme | BundledTheme | Primeiro tema em themes | Tema padrão para renderização |
langs | BundledLanguage[] | [] | Linguagens a carregar. Apenas linguagens carregadas terão highlighting. |
Uso
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 blocksExemplos
Transformação manual de HAST
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> elementsCom renderização React
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 });