|
6 | 6 | HighlighterGeneric,
|
7 | 7 | } from 'shiki/core';
|
8 | 8 | import { loadWasm, createOnigurumaEngine } from 'shiki/engine/oniguruma';
|
9 |
| -import { bundledLanguages } from 'shiki/langs'; |
| 9 | +import { BundledLanguage, bundledLanguages } from 'shiki/langs'; |
10 | 10 | // @ts-ignore - onigWasm is a Wasm module
|
11 | 11 | import onigWasm from 'shiki/onig.wasm?module';
|
12 | 12 |
|
@@ -81,18 +81,35 @@ export async function highlight(block: DocumentBlockCode): Promise<HighlightLine
|
81 | 81 | });
|
82 | 82 | }
|
83 | 83 |
|
| 84 | +const syntaxAliases: Record<string, BundledLanguage> = { |
| 85 | + // "Parser" language does not exist in Shiki, but it's used in GitBook |
| 86 | + // The closest language is "Blade" |
| 87 | + parser: 'blade', |
| 88 | +}; |
| 89 | + |
| 90 | +function checkIsBundledLanguage(lang: string): lang is BundledLanguage { |
| 91 | + return lang in bundledLanguages; |
| 92 | +} |
| 93 | + |
84 | 94 | /**
|
85 | 95 | * Validate a language name.
|
86 | 96 | */
|
87 |
| -function getLanguageForSyntax(syntax: string): keyof typeof bundledLanguages | null { |
88 |
| - // @ts-ignore |
89 |
| - const lang = bundledLanguages[syntax]; |
90 |
| - if (!lang) { |
91 |
| - return null; |
| 97 | +function getLanguageForSyntax(syntax: string): BundledLanguage | null { |
| 98 | + // Normalize the syntax to lowercase. |
| 99 | + syntax = syntax.toLowerCase(); |
| 100 | + |
| 101 | + // Check if the syntax is a bundled language. |
| 102 | + if (checkIsBundledLanguage(syntax)) { |
| 103 | + return syntax; |
| 104 | + } |
| 105 | + |
| 106 | + // Check if there is a valid alias for the syntax. |
| 107 | + const alias = syntaxAliases[syntax]; |
| 108 | + if (alias && checkIsBundledLanguage(alias)) { |
| 109 | + return alias; |
92 | 110 | }
|
93 | 111 |
|
94 |
| - // @ts-ignore |
95 |
| - return syntax; |
| 112 | + return null; |
96 | 113 | }
|
97 | 114 |
|
98 | 115 | /**
|
|
0 commit comments