-
Notifications
You must be signed in to change notification settings - Fork 17
feat: use shiki for syntax highlight #3052
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,116 @@ | ||
| import type {Highlighter} from 'shiki'; | ||
|
|
||
| import {yqlDarkTheme, yqlLightTheme} from './themes'; | ||
| import type {Language, Theme} from './types'; | ||
|
|
||
| import yqlGrammar from 'monaco-yql-languages/build/yql/YQL.tmLanguage.json'; | ||
|
|
||
| // Custom themes for YQL | ||
| const YQL_LIGHT_THEME = 'yql-light'; | ||
| const YQL_DARK_THEME = 'yql-dark'; | ||
|
|
||
| // Standard themes for other languages | ||
| const STANDARD_LIGHT_THEME = 'github-light'; | ||
| const STANDARD_DARK_THEME = 'github-dark'; | ||
|
|
||
| // Cache the highlighter promise to prevent multiple instances | ||
| let highlighterPromise: Promise<Highlighter> | null = null; | ||
|
|
||
| // Track what's already loaded | ||
| const loadedLanguages = new Set<Language>(); | ||
| const loadedThemes = new Set<Theme>(); | ||
|
|
||
| /** | ||
| * Get or create the single highlighter instance | ||
| * Lazy loads the shiki library on first use | ||
| */ | ||
| async function getHighlighter(): Promise<Highlighter> { | ||
| if (!highlighterPromise) { | ||
| // Dynamically import shiki library only when needed | ||
| highlighterPromise = (async () => { | ||
| const {createHighlighter} = await import('shiki'); | ||
| return createHighlighter({ | ||
| themes: [], | ||
| langs: [], | ||
| }); | ||
| })(); | ||
| } | ||
| return highlighterPromise; | ||
| } | ||
|
|
||
| /** | ||
| * Ensure language is loaded into the highlighter | ||
| */ | ||
| async function ensureLanguageLoaded(lang: Language): Promise<void> { | ||
| if (loadedLanguages.has(lang)) { | ||
| return; | ||
| } | ||
|
|
||
| const hl = await getHighlighter(); | ||
|
|
||
| try { | ||
| if (lang === 'yql') { | ||
| await hl.loadLanguage(yqlGrammar); | ||
| } else { | ||
| await hl.loadLanguage(lang); | ||
| } | ||
| loadedLanguages.add(lang); | ||
| } catch (error) { | ||
| console.error(`Failed to load language: ${lang}`, error); | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Ensure theme is loaded into the highlighter | ||
| */ | ||
| async function ensureThemeLoaded(themeName: Theme): Promise<void> { | ||
| if (loadedThemes.has(themeName)) { | ||
| return; | ||
| } | ||
|
|
||
| const hl = await getHighlighter(); | ||
|
|
||
| try { | ||
| if (themeName === YQL_LIGHT_THEME) { | ||
| await hl.loadTheme(yqlLightTheme); | ||
| } else if (themeName === YQL_DARK_THEME) { | ||
| await hl.loadTheme(yqlDarkTheme); | ||
| } else { | ||
| await hl.loadTheme(themeName); | ||
| } | ||
| loadedThemes.add(themeName); | ||
| } catch (error) { | ||
| console.error(`Failed to load theme: ${themeName}`, error); | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Highlight code with Shiki | ||
| * Uses a single highlighter instance with on-demand loading of languages and themes | ||
| */ | ||
| export async function highlightCode( | ||
| code: string, | ||
| lang: Language, | ||
| theme: 'light' | 'dark', | ||
| ): Promise<string> { | ||
| // Determine theme name | ||
| const isYql = lang === 'yql'; | ||
| const isDark = theme === 'dark'; | ||
|
|
||
| let themeName: Theme = isDark ? STANDARD_DARK_THEME : STANDARD_LIGHT_THEME; | ||
| if (isYql) { | ||
| themeName = isDark ? YQL_DARK_THEME : YQL_LIGHT_THEME; | ||
| } | ||
|
|
||
| // Load language and theme if needed | ||
| await Promise.all([ensureLanguageLoaded(lang), ensureThemeLoaded(themeName)]); | ||
|
|
||
| const hl = await getHighlighter(); | ||
|
|
||
| return hl.codeToHtml(code, { | ||
| lang, | ||
| theme: themeName, | ||
| }); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.