angular/adev/shared-docs/pipeline/shared/shiki.mts
Matthieu Riegler 47dc4ffd1a docs(docs-infra): share markdown rendering and highlighting code between api-gen and guides (#63357)
This reduces code duplication and improves the maintability.

PR Close #63357
2025-08-25 15:33:55 -07:00

51 lines
1.1 KiB
TypeScript

/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import {HighlighterGeneric} from 'shiki';
const LIGHT_THEME = 'github-light';
const DARK_THEME = 'github-dark';
export async function initHighlighter(): Promise<HighlighterGeneric<any, any>> {
const {createHighlighter} = await import('shiki');
return await createHighlighter({
themes: [LIGHT_THEME, DARK_THEME],
langs: [
'javascript',
'typescript',
'angular-html',
'angular-ts',
'shell',
'html',
'http',
'json',
'jsonc',
'nginx',
'markdown',
'apache',
],
});
}
export function codeToHtml(
highlighter: HighlighterGeneric<any, any>,
code: string,
language: string | undefined,
): string {
const html = highlighter.codeToHtml(code, {
lang: language ?? 'text',
themes: {
light: LIGHT_THEME,
dark: DARK_THEME,
},
cssVariablePrefix: '--shiki-',
defaultColor: false,
});
return html;
}