From 274164585be9a31004c73d362ef1e0d3081f8643 Mon Sep 17 00:00:00 2001 From: Gerrit Birkeland Date: Tue, 3 Dec 2024 22:06:02 -0700 Subject: [PATCH] Support language aliases in highlightLanguages Resolves #2798 --- CHANGELOG.md | 1 + src/lib/utils/highlighter.tsx | 8 -------- src/lib/utils/options/help.ts | 7 ++----- src/lib/utils/options/sources/typedoc.ts | 7 ++----- src/test/utils/options/default-options.test.ts | 13 ++++++++++--- 5 files changed, 15 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 74f93ef8f..9eda602ed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ title: Changelog - Add special handling for import types with type errors discarded with ts-expect-error, #2792. - Improved support for hosting TypeDoc with strict Content Security Policy rules, #2794. - Fixed low contrast in default colors for properties/accessors in light mode, #2795. +- The `highlightLanguages` option now permits Shiki aliases to be specified rather than just the language ID, #2798. ### Thanks! diff --git a/src/lib/utils/highlighter.tsx b/src/lib/utils/highlighter.tsx index c8319556c..57b321706 100644 --- a/src/lib/utils/highlighter.tsx +++ b/src/lib/utils/highlighter.tsx @@ -13,10 +13,6 @@ for (const lang of shiki.bundledLanguagesInfo) { const plaintextLanguages = ["txt", "text"]; -const supportedLanguagesWithoutAliases = unique([ - ...plaintextLanguages, - ...shiki.bundledLanguagesInfo.map((lang) => lang.id), -]); const supportedLanguages: string[] = unique([ ...plaintextLanguages, ...aliases.keys(), @@ -149,10 +145,6 @@ export function getSupportedLanguages(): string[] { return supportedLanguages; } -export function getSupportedLanguagesWithoutAliases(): string[] { - return supportedLanguagesWithoutAliases; -} - export function getSupportedThemes(): string[] { return supportedThemes; } diff --git a/src/lib/utils/options/help.ts b/src/lib/utils/options/help.ts index bfb8eb257..617924418 100644 --- a/src/lib/utils/options/help.ts +++ b/src/lib/utils/options/help.ts @@ -5,10 +5,7 @@ import { ParameterType, type DeclarationOption, } from "./declaration.js"; -import { - getSupportedLanguagesWithoutAliases, - getSupportedThemes, -} from "../highlighter.js"; +import { getSupportedLanguages, getSupportedThemes } from "../highlighter.js"; import type { TranslationProxy } from "../../internationalization/internationalization.js"; export interface ParameterHelp { @@ -105,7 +102,7 @@ export function getOptionsHelp( output.push( "", "Supported highlighting languages:", - ...toEvenColumns(getSupportedLanguagesWithoutAliases(), 80), + ...toEvenColumns(getSupportedLanguages(), 80), ); output.push( diff --git a/src/lib/utils/options/sources/typedoc.ts b/src/lib/utils/options/sources/typedoc.ts index 2ff938ff2..2249c6802 100644 --- a/src/lib/utils/options/sources/typedoc.ts +++ b/src/lib/utils/options/sources/typedoc.ts @@ -15,7 +15,7 @@ import { blockTags, inlineTags, modifierTags } from "../tsdoc-defaults.js"; import { getEnumKeys } from "../../enum.js"; import type { BundledTheme } from "@gerrit0/mini-shiki"; import { - getSupportedLanguagesWithoutAliases, + getSupportedLanguages, getSupportedThemes, } from "../../highlighter.js"; import { setDifference } from "../../set.js"; @@ -356,10 +356,7 @@ export function addTypeDocOptions(options: Pick) { type: ParameterType.Array, defaultValue: OptionDefaults.highlightLanguages, validate(value, i18n) { - const invalid = setDifference( - value, - getSupportedLanguagesWithoutAliases(), - ); + const invalid = setDifference(value, getSupportedLanguages()); if (invalid.size) { throw new Error( i18n.highlightLanguages_contains_invalid_languages_0( diff --git a/src/test/utils/options/default-options.test.ts b/src/test/utils/options/default-options.test.ts index e51e71da1..edf1f1114 100644 --- a/src/test/utils/options/default-options.test.ts +++ b/src/test/utils/options/default-options.test.ts @@ -1,4 +1,4 @@ -import { ok, throws, strictEqual, doesNotThrow } from "assert"; +import { ok, throws, deepStrictEqual as equal, doesNotThrow } from "assert"; import { Options } from "../../../lib/utils/index.js"; import { Internationalization } from "../../../lib/internationalization/internationalization.js"; @@ -11,13 +11,20 @@ describe("Default Options", () => { opts.setValue("lightHighlightTheme", "randomTheme" as never), ); opts.setValue("lightHighlightTheme", "github-light"); - strictEqual(opts.getValue("lightHighlightTheme"), "github-light"); + equal(opts.getValue("lightHighlightTheme"), "github-light"); throws(() => opts.setValue("darkHighlightTheme", "randomTheme" as never), ); opts.setValue("darkHighlightTheme", "github-light"); - strictEqual(opts.getValue("darkHighlightTheme"), "github-light"); + equal(opts.getValue("darkHighlightTheme"), "github-light"); + }); + }); + + describe("highlightLanguages", () => { + it("Supports aliased languages", () => { + opts.setValue("highlightLanguages", ["bash"]); + equal(opts.getValue("highlightLanguages"), ["bash"]); }); });