From 2409996fddf10cd07c0b6441be1b887d6de4ac38 Mon Sep 17 00:00:00 2001 From: pheralb Date: Thu, 28 Aug 2025 18:12:38 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=A0=EF=B8=8F=20Create=20shiki=20utilit?= =?UTF-8?q?y=20with=20highlighter=20&=20rehype=20options?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/shiki.ts | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/utils/shiki.ts diff --git a/src/utils/shiki.ts b/src/utils/shiki.ts new file mode 100644 index 0000000..78eacaf --- /dev/null +++ b/src/utils/shiki.ts @@ -0,0 +1,44 @@ +import type { RehypeShikiOptions } from "@shikijs/rehype"; +import { + type HighlighterCore, + type RegexEngine, + createHighlighterCore, +} from "shiki/core"; +import { createJavaScriptRegexEngine } from "shiki/engine/javascript"; + +// Themes: +import githubLight from "@shikijs/themes/github-light"; +import githubDark from "@shikijs/themes/github-dark"; + +// Languages: +import ts from "@shikijs/langs/ts"; +import bash from "@shikijs/langs/bash"; + +let jsEngine: RegexEngine | null = null; +let highlighter: Promise | null = null; + +// Engine: +const getShikiEngine = (): RegexEngine => { + jsEngine ??= createJavaScriptRegexEngine(); + return jsEngine; +}; + +// Rehype options for Shiki: +const rehypeShikiOptions: RehypeShikiOptions = { + themes: { + light: "github-light", + dark: "github-dark", + }, + langs: [bash, ts], +}; + +const shikiHighlighter = async (): Promise => { + highlighter ??= createHighlighterCore({ + themes: [githubLight, githubDark], + langs: [bash, ts], + engine: getShikiEngine(), + }); + return highlighter; +}; + +export { shikiHighlighter, rehypeShikiOptions };