react-shiki
Version:
Syntax highlighter component for react using shiki
64 lines (63 loc) • 2.46 kB
JavaScript
import { i as useHighlight, n as isInlineCode, r as rehypeInlineCodeProperty, t as createShikiHighlighterComponent } from "./component-CYq7SnJ0.mjs";
import { createHighlighterCore } from "shiki/core";
import { createOnigurumaEngine } from "shiki/engine/oniguruma";
import { createJavaScriptRawEngine, createJavaScriptRegexEngine } from "shiki/engine/javascript";
//#region src/bundles/core.ts
/**
* Validates that a highlighter is provided for the core bundle.
* The core bundle requires users to provide their own highlighter instance.
*/
function validateCoreHighlighter(highlighter) {
if (!highlighter) throw new Error("react-shiki/core requires a custom highlighter. Use createHighlighterCore() from react-shiki or switch to react-shiki for plug-and-play usage.");
return highlighter;
}
//#endregion
//#region src/core.ts
/**
* Highlight code with shiki (core bundle)
*
* @param code - Code to highlight
* @param lang - Language (bundled or custom)
* @param theme - Theme (bundled, multi-theme, or custom)
* @param options - react-shiki options + shiki options
* @returns Highlighted code as React elements or HTML string
*
* @example
* ```tsx
* import { createHighlighterCore, createOnigurumaEngine } from 'react-shiki/core';
*
*
* const highlighter = await createHighlighterCore({
* themes: [import('@shikijs/themes/github-light'), import('@shikijs/themes/github-dark')],
* langs: [import('@shikijs/langs/typescript')],
* engine: createOnigurumaEngine(import('shiki/wasm'))
* });
*
* const highlighted = useShikiHighlighter(
* 'const x = 1;',
* 'typescript',
* {
* light: 'github-light',
* dark: 'github-dark'
* },
* { highlighter }
* );
* ```
*
* Core bundle (minimal). For plug-and-play: `react-shiki` or `react-shiki/web`
*/
const useShikiHighlighter = (code, lang, themeInput, options = {}) => {
const highlighter = validateCoreHighlighter(options.highlighter);
return useHighlight(code, lang, themeInput, {
...options,
highlighter
}, async () => highlighter);
};
/**
* ShikiHighlighter component using a custom highlighter.
* Requires a highlighter to be provided.
*/
const ShikiHighlighter = createShikiHighlighterComponent(useShikiHighlighter);
//#endregion
export { ShikiHighlighter, ShikiHighlighter as default, createHighlighterCore, createJavaScriptRawEngine, createJavaScriptRegexEngine, createOnigurumaEngine, isInlineCode, rehypeInlineCodeProperty, useShikiHighlighter };
//# sourceMappingURL=core.mjs.map