Skip to content

Commit

Permalink
Add ignoredHighlightLanguages option
Browse files Browse the repository at this point in the history
Resolves #2819
  • Loading branch information
Gerrit0 committed Dec 25, 2024
1 parent 9b62f09 commit 9e667d0
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ title: Changelog

## Unreleased

### Features

- Added `ignoredHighlightLanguages` option to specify languages which will be
allowed in code blocks but not highlighted, #2819.

### Bug Fixes

- `@include` and `@includeCode` now work in the readme file, #2814.
Expand Down
3 changes: 2 additions & 1 deletion example/src/documents/syntax-highlighting.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ TypeDoc supports code blocks in Markdown and uses

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

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

Expand Down
12 changes: 12 additions & 0 deletions site/options/output.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,18 @@ loads the following languages.
}
```

## ignoredHighlightLanguages

Specifies languages used in code blocks which should be silently ignored by TypeDoc.
By default, TypeDoc will produce an error if a code block specifies a language which
is not present in the highlightLanguages array.

```json
{
"ignoredHighlightLanguages": ["mkdocs"]
}
```

## typePrintWidth

Specifies the width at which to wrap code when rendering types, defaults to 80.
Expand Down
2 changes: 2 additions & 0 deletions src/lib/internationalization/locales/en.cts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ export = {
help_darkHighlightTheme: "Specify the code highlighting theme in dark mode",
help_highlightLanguages:
"Specify the languages which will be loaded to highlight code when rendering",
help_ignoredHighlightLanguages:
"Specify languages which will be accepted as valid highlight languages, but will not be highlighted at runtime",
help_typePrintWidth:
"Width at which to wrap code to a new line when rendering a type",
help_customCss: "Path to a custom CSS file to for the theme to import",
Expand Down
4 changes: 4 additions & 0 deletions src/lib/output/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,9 @@ export class Renderer extends AbstractComponent<Application, RendererEvents> {
@Option("highlightLanguages")
private accessor highlightLanguages!: string[];

@Option("ignoredHighlightLanguages")
private accessor ignoredHighlightLanguages!: string[];

@Option("pretty")
private accessor pretty!: boolean;

Expand Down Expand Up @@ -342,6 +345,7 @@ export class Renderer extends AbstractComponent<Application, RendererEvents> {
this.darkTheme,
// Checked in option validation
this.highlightLanguages as BundledLanguage[],
this.ignoredHighlightLanguages,
);
}

Expand Down
12 changes: 9 additions & 3 deletions src/lib/utils/highlighter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,18 @@ class DoubleHighlighter {

let shikiEngine: shiki.RegexEngine | undefined;
let highlighter: DoubleHighlighter | undefined;
let ignoredLanguages: string[] | undefined;

export async function loadHighlighter(
lightTheme: shiki.BundledTheme,
darkTheme: shiki.BundledTheme,
langs: shiki.BundledLanguage[],
ignoredLangs: string[] | undefined,
) {
if (highlighter) return;

ignoredLanguages = ignoredLangs;

if (!shikiEngine) {
await shiki.loadBuiltinWasm();
shikiEngine = await shiki.createOnigurumaEngine();
Expand All @@ -138,7 +142,7 @@ export async function loadHighlighter(
}

export function isSupportedLanguage(lang: string) {
return getSupportedLanguages().includes(lang);
return ignoredLanguages?.includes(lang) || getSupportedLanguages().includes(lang);
}

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

export function isLoadedLanguage(lang: string): boolean {
return plaintextLanguages.includes(lang) || (highlighter?.supports(lang) ?? false);
return (
plaintextLanguages.includes(lang) || ignoredLanguages?.includes(lang) || highlighter?.supports(lang) || false
);
}

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

if (plaintextLanguages.includes(lang)) {
if (plaintextLanguages.includes(lang) || ignoredLanguages?.includes(lang)) {
return JSX.renderElement(<>{code}</>);
}

Expand Down
1 change: 1 addition & 0 deletions src/lib/utils/options/declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ export interface TypeDocOptionMap {
lightHighlightTheme: ShikiTheme;
darkHighlightTheme: ShikiTheme;
highlightLanguages: string[];
ignoredHighlightLanguages: string[];
typePrintWidth: number;
customCss: string;
customJs: string;
Expand Down
2 changes: 2 additions & 0 deletions src/lib/utils/options/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ export const highlightLanguages: readonly BundledLanguage[] = [
"typescript",
];

export const ignoredHighlightLanguages: readonly string[] = [];

export const sort: readonly string[] = [
"kind",
"instance-first",
Expand Down
6 changes: 6 additions & 0 deletions src/lib/utils/options/sources/typedoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,12 @@ export function addTypeDocOptions(options: Pick<Options, "addDeclaration">) {
}
},
});
options.addDeclaration({
name: "ignoredHighlightLanguages",
help: (i18n) => i18n.help_ignoredHighlightLanguages(),
type: ParameterType.Array,
defaultValue: OptionDefaults.ignoredHighlightLanguages,
});
options.addDeclaration({
name: "typePrintWidth",
help: (i18n) => i18n.help_typePrintWidth(),
Expand Down

0 comments on commit 9e667d0

Please sign in to comment.