|
1 | 1 | import { createHighlighterCoreSync } from '@shikijs/core'; |
2 | 2 | import { createJavaScriptRegexEngine } from '@shikijs/engine-javascript'; |
| 3 | +import shikiNordTheme from 'shiki/themes/nord.mjs'; |
3 | 4 |
|
4 | | -import { LANGUAGES, DEFAULT_THEME } from './languages.mjs'; |
5 | | - |
6 | | -let _shiki; |
7 | | - |
8 | | -/** |
9 | | - * Lazy-load and memoize the minimal Shikiji Syntax Highlighter |
10 | | - * @returns {import('@shikijs/core').HighlighterCore} |
11 | | - */ |
12 | | -export const getShiki = () => { |
13 | | - if (!_shiki) { |
14 | | - _shiki = createHighlighterCoreSync({ |
15 | | - themes: [DEFAULT_THEME], |
16 | | - langs: LANGUAGES, |
17 | | - // Let's use Shiki's new Experimental JavaScript-based regex engine! |
18 | | - engine: createJavaScriptRegexEngine(), |
19 | | - }); |
20 | | - } |
21 | | - return _shiki; |
| 5 | +const DEFAULT_THEME = { |
| 6 | + // We are updating this color because the background color and comment text color |
| 7 | + // in the Codebox component do not comply with accessibility standards |
| 8 | + // @see https://www.w3.org/WAI/WCAG21/Understanding/contrast-minimum.html |
| 9 | + colorReplacements: { '#616e88': '#707e99' }, |
| 10 | + ...shikiNordTheme, |
22 | 11 | }; |
23 | 12 |
|
24 | 13 | /** |
25 | | - * Highlights code and returns the inner HTML inside the <code> tag |
26 | | - * |
27 | | - * @param {string} code - The code to highlight |
28 | | - * @param {string} language - The programming language to use for highlighting |
29 | | - * @returns {string} The inner HTML of the highlighted code |
| 14 | + * Creates a syntax highlighter with utility functions |
| 15 | + * @param {import('@shikijs/core').HighlighterCoreOptions} options - Configuration options for the highlighter |
30 | 16 | */ |
31 | | -export const highlightToHtml = (code, language) => |
32 | | - getShiki() |
33 | | - .codeToHtml(code, { lang: language, theme: DEFAULT_THEME }) |
34 | | - // Shiki will always return the Highlighted code encapsulated in a <pre> and <code> tag |
35 | | - // since our own CodeBox component handles the <code> tag, we just want to extract |
36 | | - // the inner highlighted code to the CodeBox |
37 | | - .match(/<code>(.+?)<\/code>/s)[1]; |
| 17 | +export const createHighlighter = options => { |
| 18 | + const shiki = createHighlighterCoreSync({ |
| 19 | + themes: [DEFAULT_THEME], |
| 20 | + engine: createJavaScriptRegexEngine(), |
| 21 | + ...options, |
| 22 | + }); |
38 | 23 |
|
39 | | -/** |
40 | | - * Highlights code and returns a HAST tree |
41 | | - * |
42 | | - * @param {string} code - The code to highlight |
43 | | - * @param {string} language - The programming language to use for highlighting |
44 | | - * @returns {import('hast').Element} The HAST representation of the highlighted code |
45 | | - */ |
46 | | -export const highlightToHast = (code, language) => |
47 | | - getShiki().codeToHast(code, { lang: language, theme: DEFAULT_THEME }); |
| 24 | + const theme = options.themes?.[0] ?? DEFAULT_THEME; |
| 25 | + const langs = options.langs ?? []; |
| 26 | + |
| 27 | + const getLanguageDisplayName = language => { |
| 28 | + const languageByIdOrAlias = langs.find( |
| 29 | + ({ name, aliases }) => |
| 30 | + name.toLowerCase() === language.toLowerCase() || |
| 31 | + (aliases !== undefined && aliases.includes(language.toLowerCase())) |
| 32 | + ); |
| 33 | + |
| 34 | + return languageByIdOrAlias?.displayName ?? language; |
| 35 | + }; |
| 36 | + |
| 37 | + /** |
| 38 | + * Highlights code and returns the inner HTML inside the <code> tag |
| 39 | + * |
| 40 | + * @param {string} code - The code to highlight |
| 41 | + * @param {string} language - The programming language to use for highlighting |
| 42 | + * @returns {string} The inner HTML of the highlighted code |
| 43 | + */ |
| 44 | + const highlightToHtml = (code, language) => |
| 45 | + shiki |
| 46 | + .codeToHtml(code, { lang: language, theme }) |
| 47 | + // Shiki will always return the Highlighted code encapsulated in a <pre> and <code> tag |
| 48 | + // since our own CodeBox component handles the <code> tag, we just want to extract |
| 49 | + // the inner highlighted code to the CodeBox |
| 50 | + .match(/<code>(.+?)<\/code>/s)[1]; |
| 51 | + |
| 52 | + /** |
| 53 | + * Highlights code and returns a HAST tree |
| 54 | + * |
| 55 | + * @param {string} code - The code to highlight |
| 56 | + * @param {string} language - The programming language to use for highlighting |
| 57 | + */ |
| 58 | + const highlightToHast = (code, language) => |
| 59 | + shiki.codeToHast(code, { lang: language, theme }); |
| 60 | + |
| 61 | + return { |
| 62 | + shiki, |
| 63 | + getLanguageDisplayName, |
| 64 | + highlightToHtml, |
| 65 | + highlightToHast, |
| 66 | + }; |
| 67 | +}; |
0 commit comments