Skip to content

Commit 5dab70f

Browse files
authored
Fix "Parser" language syntax highlighting (#2676)
1 parent 665b6be commit 5dab70f

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

.changeset/three-sloths-taste.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'gitbook': patch
3+
---
4+
5+
Fix "Parser" language syntax highlighting

packages/gitbook/src/components/DocumentView/CodeBlock/highlight.ts

+25-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
HighlighterGeneric,
77
} from 'shiki/core';
88
import { loadWasm, createOnigurumaEngine } from 'shiki/engine/oniguruma';
9-
import { bundledLanguages } from 'shiki/langs';
9+
import { BundledLanguage, bundledLanguages } from 'shiki/langs';
1010
// @ts-ignore - onigWasm is a Wasm module
1111
import onigWasm from 'shiki/onig.wasm?module';
1212

@@ -81,18 +81,35 @@ export async function highlight(block: DocumentBlockCode): Promise<HighlightLine
8181
});
8282
}
8383

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+
8494
/**
8595
* Validate a language name.
8696
*/
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;
92110
}
93111

94-
// @ts-ignore
95-
return syntax;
112+
return null;
96113
}
97114

98115
/**

0 commit comments

Comments
 (0)