-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
57 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import shikiThemeCatppuccinLatte from '@shikijs/themes/catppuccin-latte' | ||
import shikiThemeCatppuccinMocha from '@shikijs/themes/catppuccin-mocha' | ||
import shikiLangJson from '@shikijs/langs/json' | ||
import { createHighlighterCore, type HighlighterCore } from "shiki/core"; | ||
import { createOnigurumaEngine } from 'shiki/engine/oniguruma' | ||
import shikiHighlighter from 'shiki/wasm' | ||
import { toJsxRuntime } from 'hast-util-to-jsx-runtime'; | ||
import * as jsxRuntime from 'preact/jsx-runtime'; | ||
|
||
export async function highlight(code: string) { | ||
const highlighter = await buildHighlighter() | ||
const out = await highlighter.codeToHast(code, { | ||
lang: 'json', | ||
defaultColor: false, | ||
themes: { | ||
light: 'catppuccin-latte', | ||
dark: 'catppuccin-mocha' | ||
}, | ||
colorReplacements: { | ||
// Remove backgrounds | ||
'catppuccin-latte': { '#eff1f5': 'transparent' }, | ||
'catppuccin-mocha': { '#1e1e2e': 'transparent' }, | ||
} | ||
}) | ||
return toJsxRuntime(out, jsxRuntime) | ||
} | ||
|
||
let highlighter: HighlighterCore | undefined | ||
async function buildHighlighter() { | ||
if (highlighter != null) { | ||
return highlighter | ||
} | ||
|
||
highlighter = await createHighlighterCore({ | ||
themes: [ | ||
shikiThemeCatppuccinLatte, | ||
shikiThemeCatppuccinMocha, | ||
], | ||
langs: [ | ||
shikiLangJson, | ||
], | ||
engine: createOnigurumaEngine(shikiHighlighter) | ||
}) | ||
|
||
return highlighter | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters