createShikiPlugin()

Create a Shiki-powered syntax highlighting plugin. An async function that initializes the Shiki highlighter and returns a UnifastPlugin with a HAST transform.

import { createShikiPlugin } from "@unifast/shiki";

Signature

async function createShikiPlugin(
  options?: ShikiTransformerOptions,
): Promise<UnifastPlugin>

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

Examples

Single theme

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

Multiple themes

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

With other plugins

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],
});