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)

PropertyTypeDefaultDescription
themesBundledTheme[]["github-dark"]Shiki themes to load
defaultThemeBundledThemeFirst theme in themesDefault theme for rendering
langsBundledLanguage[][]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 blocks

Examples

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> elements

With 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 });