Skip to content

Commit 9e667d0

Browse files
committed
Add ignoredHighlightLanguages option
Resolves #2819
1 parent 9b62f09 commit 9e667d0

File tree

9 files changed

+43
-4
lines changed

9 files changed

+43
-4
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ title: Changelog
44

55
## Unreleased
66

7+
### Features
8+
9+
- Added `ignoredHighlightLanguages` option to specify languages which will be
10+
allowed in code blocks but not highlighted, #2819.
11+
712
### Bug Fixes
813

914
- `@include` and `@includeCode` now work in the readme file, #2814.

example/src/documents/syntax-highlighting.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ TypeDoc supports code blocks in Markdown and uses
88

99
TypeDoc supports all languages supported by Shiki, but does not load all of
1010
them by default. The `highlightLanguages` option can be used to customize
11-
which languages are loaded for highlighting.
11+
which languages are loaded for highlighting. The `ignoredHighlightLanguages`
12+
option can be used to specify languages which should not be highlighted.
1213

1314
If no language is specified, the code block is assumed to be TypeScript:
1415

site/options/output.md

+12
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,18 @@ loads the following languages.
162162
}
163163
```
164164

165+
## ignoredHighlightLanguages
166+
167+
Specifies languages used in code blocks which should be silently ignored by TypeDoc.
168+
By default, TypeDoc will produce an error if a code block specifies a language which
169+
is not present in the highlightLanguages array.
170+
171+
```json
172+
{
173+
"ignoredHighlightLanguages": ["mkdocs"]
174+
}
175+
```
176+
165177
## typePrintWidth
166178

167179
Specifies the width at which to wrap code when rendering types, defaults to 80.

src/lib/internationalization/locales/en.cts

+2
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,8 @@ export = {
237237
help_darkHighlightTheme: "Specify the code highlighting theme in dark mode",
238238
help_highlightLanguages:
239239
"Specify the languages which will be loaded to highlight code when rendering",
240+
help_ignoredHighlightLanguages:
241+
"Specify languages which will be accepted as valid highlight languages, but will not be highlighted at runtime",
240242
help_typePrintWidth:
241243
"Width at which to wrap code to a new line when rendering a type",
242244
help_customCss: "Path to a custom CSS file to for the theme to import",

src/lib/output/renderer.ts

+4
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,9 @@ export class Renderer extends AbstractComponent<Application, RendererEvents> {
247247
@Option("highlightLanguages")
248248
private accessor highlightLanguages!: string[];
249249

250+
@Option("ignoredHighlightLanguages")
251+
private accessor ignoredHighlightLanguages!: string[];
252+
250253
@Option("pretty")
251254
private accessor pretty!: boolean;
252255

@@ -342,6 +345,7 @@ export class Renderer extends AbstractComponent<Application, RendererEvents> {
342345
this.darkTheme,
343346
// Checked in option validation
344347
this.highlightLanguages as BundledLanguage[],
348+
this.ignoredHighlightLanguages,
345349
);
346350
}
347351

src/lib/utils/highlighter.tsx

+9-3
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,18 @@ class DoubleHighlighter {
116116

117117
let shikiEngine: shiki.RegexEngine | undefined;
118118
let highlighter: DoubleHighlighter | undefined;
119+
let ignoredLanguages: string[] | undefined;
119120

120121
export async function loadHighlighter(
121122
lightTheme: shiki.BundledTheme,
122123
darkTheme: shiki.BundledTheme,
123124
langs: shiki.BundledLanguage[],
125+
ignoredLangs: string[] | undefined,
124126
) {
125127
if (highlighter) return;
126128

129+
ignoredLanguages = ignoredLangs;
130+
127131
if (!shikiEngine) {
128132
await shiki.loadBuiltinWasm();
129133
shikiEngine = await shiki.createOnigurumaEngine();
@@ -138,7 +142,7 @@ export async function loadHighlighter(
138142
}
139143

140144
export function isSupportedLanguage(lang: string) {
141-
return getSupportedLanguages().includes(lang);
145+
return ignoredLanguages?.includes(lang) || getSupportedLanguages().includes(lang);
142146
}
143147

144148
export function getSupportedLanguages(): string[] {
@@ -150,13 +154,15 @@ export function getSupportedThemes(): string[] {
150154
}
151155

152156
export function isLoadedLanguage(lang: string): boolean {
153-
return plaintextLanguages.includes(lang) || (highlighter?.supports(lang) ?? false);
157+
return (
158+
plaintextLanguages.includes(lang) || ignoredLanguages?.includes(lang) || highlighter?.supports(lang) || false
159+
);
154160
}
155161

156162
export function highlight(code: string, lang: string): string {
157163
assert(highlighter, "Tried to highlight with an uninitialized highlighter");
158164

159-
if (plaintextLanguages.includes(lang)) {
165+
if (plaintextLanguages.includes(lang) || ignoredLanguages?.includes(lang)) {
160166
return JSX.renderElement(<>{code}</>);
161167
}
162168

src/lib/utils/options/declaration.ts

+1
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ export interface TypeDocOptionMap {
145145
lightHighlightTheme: ShikiTheme;
146146
darkHighlightTheme: ShikiTheme;
147147
highlightLanguages: string[];
148+
ignoredHighlightLanguages: string[];
148149
typePrintWidth: number;
149150
customCss: string;
150151
customJs: string;

src/lib/utils/options/defaults.ts

+2
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ export const highlightLanguages: readonly BundledLanguage[] = [
7777
"typescript",
7878
];
7979

80+
export const ignoredHighlightLanguages: readonly string[] = [];
81+
8082
export const sort: readonly string[] = [
8183
"kind",
8284
"instance-first",

src/lib/utils/options/sources/typedoc.ts

+6
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,12 @@ export function addTypeDocOptions(options: Pick<Options, "addDeclaration">) {
366366
}
367367
},
368368
});
369+
options.addDeclaration({
370+
name: "ignoredHighlightLanguages",
371+
help: (i18n) => i18n.help_ignoredHighlightLanguages(),
372+
type: ParameterType.Array,
373+
defaultValue: OptionDefaults.ignoredHighlightLanguages,
374+
});
369375
options.addDeclaration({
370376
name: "typePrintWidth",
371377
help: (i18n) => i18n.help_typePrintWidth(),

0 commit comments

Comments
 (0)