-
Notifications
You must be signed in to change notification settings - Fork 6.3k
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
3 changed files
with
17 additions
and
27 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
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
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 |
---|---|---|
@@ -1,26 +1,23 @@ | ||
import { getSingletonHighlighterCore } from '@shikijs/core'; | ||
import type { HighlighterCore } from '@shikijs/core'; | ||
import { createHighlighterCoreSync } from '@shikijs/core'; | ||
import { createJavaScriptRegexEngine } from '@shikijs/engine-javascript'; | ||
|
||
import { LANGUAGES, DEFAULT_THEME } from '@/shiki.config.mjs'; | ||
|
||
// This creates a memoized minimal Shikiji Syntax Highlighter | ||
export const shikiPromise = getSingletonHighlighterCore({ | ||
export const shiki = createHighlighterCoreSync({ | ||
themes: [DEFAULT_THEME], | ||
langs: LANGUAGES, | ||
// Let's use Shiki's new Experimental JavaScript-based regex engine! | ||
engine: createJavaScriptRegexEngine(), | ||
}); | ||
|
||
export const highlightToHtml = | ||
(shiki: HighlighterCore) => (code: string, language: string) => | ||
shiki | ||
.codeToHtml(code, { lang: language, theme: DEFAULT_THEME }) | ||
// Shiki will always return the Highlighted code encapsulated in a <pre> and <code> tag | ||
// since our own CodeBox component handles the <code> tag, we just want to extract | ||
// the inner highlighted code to the CodeBox | ||
.match(/<code>(.+?)<\/code>/s)![1]; | ||
export const highlightToHtml = (code: string, language: string) => | ||
shiki | ||
.codeToHtml(code, { lang: language, theme: DEFAULT_THEME }) | ||
// Shiki will always return the Highlighted code encapsulated in a <pre> and <code> tag | ||
// since our own CodeBox component handles the <code> tag, we just want to extract | ||
// the inner highlighted code to the CodeBox | ||
.match(/<code>(.+?)<\/code>/s)![1]; | ||
|
||
export const highlightToHast = | ||
(shiki: HighlighterCore) => (code: string, language: string) => | ||
shiki.codeToHast(code, { lang: language, theme: DEFAULT_THEME }); | ||
export const highlightToHast = (code: string, language: string) => | ||
shiki.codeToHast(code, { lang: language, theme: DEFAULT_THEME }); |