createShikiTransformer()

lower-level नियंत्रण के लिए एक standalone Shiki transformer बनाएँ। createShikiPlugin() के विपरीत, यह एक raw transformer object return करता है जिसे आप HAST trees पर manually लागू कर सकते हैं।

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

Signature

async function createShikiTransformer(
  options?: ShikiTransformerOptions,
): Promise<ShikiTransformer>

Parameters

options?

Shiki configuration (themes, languages)

PropertyTypeDefaultविवरण
themesBundledTheme[]["github-dark"]load करने के लिए Shiki themes
defaultThemeBundledThemethemes में पहला themerendering के लिए Default theme
langsBundledLanguage[][]load करने के लिए भाषाएँ। केवल loaded भाषाएँ ही highlight होंगी।

उपयोग

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);
// Shiki-highlighted code blocks के साथ HAST tree

उदाहरण

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);
// Shiki-highlighted <span> elements वाला <pre>

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