aboutsummaryrefslogtreecommitdiffhomepage
path: root/frontend/app/highlight.ts
blob: b17a2c9a8273f0eb01da869577cedb0fe9724fce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import { toJsxRuntime } from "hast-util-to-jsx-runtime";
import { Fragment, type JSX } from "react";
import { jsx, jsxs } from "react/jsx-runtime";
import { type BundledLanguage, codeToHast } from "./shiki.bundle";

export type { BundledLanguage };

// https://shiki.matsu.io/packages/next
export async function highlight(code: string, lang: BundledLanguage) {
	const out = await codeToHast(code.trimEnd(), {
		lang: lang === "swift" ? "text" : lang,
		theme: "github-light",
	});

	return toJsxRuntime(out, {
		Fragment,
		jsx,
		jsxs,
	}) as JSX.Element;
}