Home / Function/ createShikiHighlighter() — astro Function Reference

createShikiHighlighter() — astro Function Reference

Architecture documentation for the createShikiHighlighter() function in shiki.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  e4425add_4968_b8d8_e072_fa92e237cb7e["createShikiHighlighter()"]
  f102b00b_404a_a2b2_3274_44ec121e3e59["shiki.ts"]
  e4425add_4968_b8d8_e072_fa92e237cb7e -->|defined in| f102b00b_404a_a2b2_3274_44ec121e3e59
  08047503_d6bf_dbf6_a748_d18d88ab6af0["cssVariablesTheme()"]
  e4425add_4968_b8d8_e072_fa92e237cb7e -->|calls| 08047503_d6bf_dbf6_a748_d18d88ab6af0
  8120e48c_d15e_d5aa_1bed_233cbe57051f["normalizePropAsString()"]
  e4425add_4968_b8d8_e072_fa92e237cb7e -->|calls| 8120e48c_d15e_d5aa_1bed_233cbe57051f
  style e4425add_4968_b8d8_e072_fa92e237cb7e fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/markdown/remark/src/shiki.ts lines 77–215

export async function createShikiHighlighter({
	langs = [],
	theme = 'github-dark',
	themes = {},
	langAlias = {},
}: CreateShikiHighlighterOptions = {}): Promise<ShikiHighlighter> {
	theme = theme === 'css-variables' ? cssVariablesTheme() : theme;

	const highlighterOptions = {
		langs: ['plaintext', ...langs],
		langAlias,
		themes: Object.values(themes).length ? Object.values(themes) : [theme],
	};

	const key = JSON.stringify(highlighterOptions, Object.keys(highlighterOptions).sort());

	let highlighter: HighlighterGeneric<BundledLanguage, BundledTheme>;

	// Highlighter has already been requested, reuse the same instance
	if (cachedHighlighters.has(key)) {
		highlighter = cachedHighlighters.get(key);
	} else {
		highlighter = await createHighlighter(highlighterOptions);
		cachedHighlighters.set(key, highlighter);
	}

	async function highlight(
		code: string,
		lang = 'plaintext',
		options: ShikiHighlighterHighlightOptions,
		to: 'hast' | 'html',
	) {
		const resolvedLang = langAlias[lang] ?? lang;
		const loadedLanguages = highlighter.getLoadedLanguages();

		if (!isSpecialLang(lang) && !loadedLanguages.includes(resolvedLang)) {
			try {
				await highlighter.loadLanguage(resolvedLang as BundledLanguage);
			} catch (_err) {
				const langStr =
					lang === resolvedLang ? `"${lang}"` : `"${lang}" (aliased to "${resolvedLang}")`;
				console.warn(`[Shiki] The language ${langStr} doesn't exist, falling back to "plaintext".`);
				lang = 'plaintext';
			}
		}

		code = code.replace(/(?:\r\n|\r|\n)$/, '');

		const themeOptions = Object.values(themes).length ? { themes } : { theme };
		const inline = options?.inline ?? false;

		return highlighter[to === 'html' ? 'codeToHtml' : 'codeToHast'](code, {
			...themeOptions,
			defaultColor: options.defaultColor,
			lang,
			// NOTE: while we can spread `options.attributes` here so that Shiki can auto-serialize this as rendered
			// attributes on the top-level tag, it's not clear whether it is fine to pass all attributes as meta, as
			// they're technically not meta, nor parsed from Shiki's `parseMetaString` API.
			meta: options?.meta ? { __raw: options?.meta } : undefined,
			transformers: [
				{
					pre(node) {
						// Swap to `code` tag if inline
						if (inline) {
							node.tagName = 'code';
						}

						const {
							class: attributesClass,
							style: attributesStyle,
							...rest
						} = options?.attributes ?? {};
						Object.assign(node.properties, rest);

						const classValue =
							(normalizePropAsString(node.properties.class) ?? '') +
							(attributesClass ? ` ${attributesClass}` : '');
						const styleValue =
							(normalizePropAsString(node.properties.style) ?? '') +
							(attributesStyle ? `; ${attributesStyle}` : '');

Domain

Subdomains

Frequently Asked Questions

What does createShikiHighlighter() do?
createShikiHighlighter() is a function in the astro codebase, defined in packages/markdown/remark/src/shiki.ts.
Where is createShikiHighlighter() defined?
createShikiHighlighter() is defined in packages/markdown/remark/src/shiki.ts at line 77.
What does createShikiHighlighter() call?
createShikiHighlighter() calls 2 function(s): cssVariablesTheme, normalizePropAsString.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free