From 5dab70fab44d71e9a2ecb05ae37c15adbdc1fda8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Berg=C3=A9?= Date: Tue, 7 Jan 2025 10:23:25 +0100 Subject: [PATCH] Fix "Parser" language syntax highlighting (#2676) --- .changeset/three-sloths-taste.md | 5 +++ .../DocumentView/CodeBlock/highlight.ts | 33 ++++++++++++++----- 2 files changed, 30 insertions(+), 8 deletions(-) create mode 100644 .changeset/three-sloths-taste.md diff --git a/.changeset/three-sloths-taste.md b/.changeset/three-sloths-taste.md new file mode 100644 index 000000000..7737c04df --- /dev/null +++ b/.changeset/three-sloths-taste.md @@ -0,0 +1,5 @@ +--- +'gitbook': patch +--- + +Fix "Parser" language syntax highlighting diff --git a/packages/gitbook/src/components/DocumentView/CodeBlock/highlight.ts b/packages/gitbook/src/components/DocumentView/CodeBlock/highlight.ts index 3432f56bc..56ada6b4e 100644 --- a/packages/gitbook/src/components/DocumentView/CodeBlock/highlight.ts +++ b/packages/gitbook/src/components/DocumentView/CodeBlock/highlight.ts @@ -6,7 +6,7 @@ import { HighlighterGeneric, } from 'shiki/core'; import { loadWasm, createOnigurumaEngine } from 'shiki/engine/oniguruma'; -import { bundledLanguages } from 'shiki/langs'; +import { BundledLanguage, bundledLanguages } from 'shiki/langs'; // @ts-ignore - onigWasm is a Wasm module import onigWasm from 'shiki/onig.wasm?module'; @@ -81,18 +81,35 @@ export async function highlight(block: DocumentBlockCode): Promise = { + // "Parser" language does not exist in Shiki, but it's used in GitBook + // The closest language is "Blade" + parser: 'blade', +}; + +function checkIsBundledLanguage(lang: string): lang is BundledLanguage { + return lang in bundledLanguages; +} + /** * Validate a language name. */ -function getLanguageForSyntax(syntax: string): keyof typeof bundledLanguages | null { - // @ts-ignore - const lang = bundledLanguages[syntax]; - if (!lang) { - return null; +function getLanguageForSyntax(syntax: string): BundledLanguage | null { + // Normalize the syntax to lowercase. + syntax = syntax.toLowerCase(); + + // Check if the syntax is a bundled language. + if (checkIsBundledLanguage(syntax)) { + return syntax; } - // @ts-ignore - return syntax; + // Check if there is a valid alias for the syntax. + const alias = syntaxAliases[syntax]; + if (alias && checkIsBundledLanguage(alias)) { + return alias; + } + + return null; } /**