Commit c6a25fc62 for llama.cpp

commit c6a25fc622d322b9ad6e079e97009f7892543a4c
Author: Aleksander Grygier <aleksander.grygier@gmail.com>
Date:   Fri Nov 20 15:06:10 2026 +0100

    ui: load highlight.js languages on demand (#32661)

    * ui: load highlight.js on first use

    highlight.js and every lowlight language (~900 KB raw) moved out of
    Llama Chat's first load into a chunk that loads with the first code block.

    - @ggml-org/llama-ui/code: highlightCode stays synchronous. Until
      highlight.js has loaded it returns the code escaped as plain text and
      starts the load; a $derived or $effect that called it runs again once
      it has loaded. New loadHighlighter() and isHighlighterLoaded().
    - Markdown: rehype-highlight runs through a lazy rehype step, like
      KaTeX's: the first message with a code block waits for it, every
      other one renders in the same pass.

    First-load JS of the chat page: 3.47 -> 2.56 MB raw, 1.05 -> 0.75 MB
    gzip, 0.84 -> 0.60 MB brotli.

    Assisted-by: Claude (Opus 5.5 Medium)

    * ui: load each highlight.js language on first use

    highlight.js now loads one language at a time instead of all ~190 at
    once with the first code block: every language is its own chunk, and
    the core loads with the first one.

    - @ggml-org/llama-ui/code: highlightCode looks a language or alias up
      in a generated table (scripts/generate-highlight-languages.mjs), loads
      it with the languages it embeds (CSS and JavaScript inside HTML, say)
      and returns plain text until then; a $derived or $effect that called
      it runs again once it has loaded. Auto-detection picks among lowlight's
      37 common languages, loaded the first time something auto-detects.
      New loadHighlightLanguages(), isHighlightLanguageLoaded(),
      resolveHighlightLanguage() and getLoadedHighlightLanguages() replace
      loadHighlighter() and isHighlighterLoaded().
    - Markdown: rehypeLazyHighlight highlights with lowlight's core and the
      languages a message uses, waiting only for one it is the first to use.
      rehype-highlight is no longer in the pipeline, since it bundles
      lowlight's common languages.

    First-load JS of the chat page stays at 2.58 MB raw, 0.76 MB gzip,
    0.61 MB brotli: no highlight.js code, only the 14 KB language table.
    The first code block now loads its language (1-12 KB for most) and the
    20 KB core instead of 900 KB.

    Assisted-by: Claude (Opus 5.5 Medium)

    * ui: name the code-block class names and shell language as constants

    Assisted-by: Claude (Opus 5.5 Medium)

diff --git a/ui/apps/chat/src/lib/components/app/content/MarkdownContent/markdown-processor.ts b/ui/apps/chat/src/lib/components/app/content/MarkdownContent/markdown-processor.ts
index aed4b93e5..3f329baa6 100644
--- a/ui/apps/chat/src/lib/components/app/content/MarkdownContent/markdown-processor.ts
+++ b/ui/apps/chat/src/lib/components/app/content/MarkdownContent/markdown-processor.ts
@@ -13,6 +13,7 @@ import { rehypeEnhanceMermaidBlocks } from './plugins/rehype/enhance-mermaid-blo
 import { rehypeEnhanceSvgBlocks } from './plugins/rehype/enhance-svg-blocks';
 import { rehypeEnhanceTables } from './plugins/rehype/enhance-tables';
 import { rehypeFileBadge } from './plugins/rehype/file-badge';
+import { rehypeLazyHighlight } from './plugins/rehype/lazy-highlight';
 import { rehypeLazyKatex } from './plugins/rehype/lazy-katex';
 import { rehypeMermaidPre } from './plugins/rehype/mermaid-pre';
 import { rehypeRtlSupport } from './plugins/rehype/rehype-rtl-support';
@@ -23,9 +24,7 @@ import { remarkLiteralHtml } from './plugins/remark/literal-html';
 import { FileTypeText } from '#lib/enums/files.enums.ts';
 import type { DatabaseMessageExtra } from '#lib/types/database.js';
 import type { Root as HastRoot } from 'hast';
-import { all as lowlightAll } from 'lowlight';
 import type { Root as MdastRoot } from 'mdast';
-import rehypeHighlight from 'rehype-highlight';
 import rehypeStringify from 'rehype-stringify';
 import { remark } from 'remark';
 import remarkBreaks from 'remark-breaks';
@@ -75,10 +74,9 @@ function buildPipeline({
 	}

 	const pipeline = proc
-		.use(rehypeHighlight, {
-			aliases: { [FileTypeText.XML]: [FileTypeText.SVELTE, FileTypeText.VUE] },
-			languages: lowlightAll
-		}) // Add syntax highlighting
+		.use(rehypeLazyHighlight, {
+			aliases: { [FileTypeText.XML]: [FileTypeText.SVELTE, FileTypeText.VUE] }
+		}) // Add syntax highlighting, loading highlight.js with the first message that has a code block
 		.use(rehypeRestoreTableHtml) // Restore limited HTML (e.g. <br>, <ul>) inside Markdown tables
 		.use(rehypeEnhanceTables) // Wrap tables in a horizontal scroll container
 		.use(rehypeEnhanceLinks) // Add target="_blank" to links
diff --git a/ui/apps/chat/src/lib/components/app/content/MarkdownContent/plugins/rehype/code-block-utils.ts b/ui/apps/chat/src/lib/components/app/content/MarkdownContent/plugins/rehype/code-block-utils.ts
index 771235089..2070b9fd5 100644
--- a/ui/apps/chat/src/lib/components/app/content/MarkdownContent/plugins/rehype/code-block-utils.ts
+++ b/ui/apps/chat/src/lib/components/app/content/MarkdownContent/plugins/rehype/code-block-utils.ts
@@ -4,6 +4,7 @@
  */

 import {
+	CODE_BLOCK,
 	CODE_BLOCK_CLASS,
 	CODE_ICON_SVG,
 	COPY_ICON_SVG,
@@ -100,7 +101,9 @@ export function createSourceView(
 ): Element {
 	const code: Element = codeElement ?? {
 		children: [{ type: 'text', value: source }],
-		properties: { className: ['hljs', `language-${language}`] },
+		properties: {
+			className: [CODE_BLOCK_CLASS.HLJS, `${CODE_BLOCK.LANGUAGE_CLASS_PREFIX}${language}`]
+		},
 		tagName: 'code',
 		type: 'element'
 	};
diff --git a/ui/apps/chat/src/lib/components/app/content/MarkdownContent/plugins/rehype/enhance-code-blocks.ts b/ui/apps/chat/src/lib/components/app/content/MarkdownContent/plugins/rehype/enhance-code-blocks.ts
index 5c1b2094f..60932a246 100644
--- a/ui/apps/chat/src/lib/components/app/content/MarkdownContent/plugins/rehype/enhance-code-blocks.ts
+++ b/ui/apps/chat/src/lib/components/app/content/MarkdownContent/plugins/rehype/enhance-code-blocks.ts
@@ -17,7 +17,7 @@ import {
 	createWrapper,
 	generateBlockId
 } from './code-block-utils';
-import { CODE_BLOCK_CLASS, MARKDOWN_DATA_ATTRS } from '#lib/constants/index.ts';
+import { CODE_BLOCK, CODE_BLOCK_CLASS, MARKDOWN_DATA_ATTRS } from '#lib/constants/index.ts';
 import type { Element, ElementContent, Root } from 'hast';
 import type { Plugin } from 'unified';
 import { visit } from 'unist-util-visit';
@@ -34,8 +34,8 @@ function extractLanguage(codeElement: Element): string {
 	if (!Array.isArray(className)) return 'text';

 	for (const cls of className) {
-		if (typeof cls === 'string' && cls.startsWith('language-')) {
-			return cls.replace('language-', '');
+		if (typeof cls === 'string' && cls.startsWith(CODE_BLOCK.LANGUAGE_CLASS_PREFIX)) {
+			return cls.slice(CODE_BLOCK.LANGUAGE_CLASS_PREFIX.length);
 		}
 	}

diff --git a/ui/apps/chat/src/lib/components/app/content/MarkdownContent/plugins/rehype/lazy-highlight.ts b/ui/apps/chat/src/lib/components/app/content/MarkdownContent/plugins/rehype/lazy-highlight.ts
new file mode 100644
index 000000000..fa79294b2
--- /dev/null
+++ b/ui/apps/chat/src/lib/components/app/content/MarkdownContent/plugins/rehype/lazy-highlight.ts
@@ -0,0 +1,149 @@
+/**
+ * Rehype plugin that highlights code blocks like rehype-highlight, loading highlight.js and each language only when a
+ * message has a block in it.
+ *
+ * Only `<pre><code>` blocks with a language class (`language-ts`, `lang-ts`) are highlighted; `no-highlight` opts a
+ * block out, and a language highlight.js lacks is left plain. rehype-highlight itself is not used: it bundles
+ * lowlight's common languages, which would load all at once.
+ */
+
+import { CODE_BLOCK, CODE_BLOCK_CLASS } from '#lib/constants/index.ts';
+import {
+	getLoadedHighlightLanguages,
+	loadHighlightLanguages,
+	resolveHighlightLanguage
+} from '#lib/utils/code.ts';
+import type { Element, ElementContent, Root } from 'hast';
+import type { Plugin } from 'unified';
+import { visit } from 'unist-util-visit';
+
+type Lowlight = ReturnType<(typeof import('./lowlight-core'))['createLowlight']>;
+
+export interface LazyHighlightOptions {
+	/** Extra aliases per language, as rehype-highlight takes them: `{ xml: ['svelte', 'vue'] }`. */
+	aliases?: Record<string, string | readonly string[]>;
+}
+
+interface CodeBlock {
+	node: Element;
+	/** The highlight.js language to use, or undefined for one it lacks. */
+	name: string | undefined;
+}
+
+let lowlight: Lowlight | undefined;
+let lowlightLoading: Promise<Lowlight> | undefined;
+
+const registered = new Set<string>();
+
+// the language a code element's class names, false for `no-highlight`, as rehype-highlight reads it
+function classLanguage(node: Element): string | false | undefined {
+	const list = node.properties?.className;
+
+	if (!Array.isArray(list)) return undefined;
+
+	let name: string | undefined;
+
+	for (const item of list) {
+		const value = String(item);
+
+		if ((CODE_BLOCK.NO_HIGHLIGHT_CLASSES as readonly string[]).includes(value)) return false;
+
+		for (const prefix of [CODE_BLOCK.LANG_CLASS_PREFIX, CODE_BLOCK.LANGUAGE_CLASS_PREFIX]) {
+			if (!name && value.startsWith(prefix)) name = value.slice(prefix.length);
+		}
+	}
+
+	return name;
+}
+
+function textContent(node: ElementContent | Element): string {
+	if (node.type === 'text') return node.value;
+
+	if (node.type !== 'element') return '';
+
+	return node.children.map(textContent).join('');
+}
+
+function loadLowlight(): Promise<Lowlight> {
+	lowlightLoading ??= import('./lowlight-core').then(
+		({ createLowlight }) => (lowlight = createLowlight()),
+		(error) => {
+			lowlightLoading = undefined;
+
+			throw error;
+		}
+	);
+
+	return lowlightLoading;
+}
+
+// registers the languages highlightCode's loader has loaded with lowlight, as highlight.js ships them
+function registerLoadedLanguages(target: Lowlight): void {
+	for (const [name, definition] of Object.entries(getLoadedHighlightLanguages())) {
+		if (registered.has(name)) continue;
+
+		target.register(name, definition);
+		registered.add(name);
+	}
+}
+
+function highlightBlocks(target: Lowlight, blocks: CodeBlock[]): void {
+	for (const { name, node } of blocks) {
+		const className = Array.isArray(node.properties.className)
+			? node.properties.className
+			: (node.properties.className = []);
+
+		if (!className.includes(CODE_BLOCK_CLASS.HLJS)) className.unshift(CODE_BLOCK_CLASS.HLJS);
+
+		if (!name || !registered.has(name)) continue;
+
+		const result = target.highlight(name, textContent(node));
+
+		if (result.children.length > 0) node.children = result.children as ElementContent[];
+	}
+}
+
+// synchronous when there is no code block or its languages have loaded, so a message waits only for a language it
+// is the first to use, and every other one renders in the same pass as before
+export const rehypeLazyHighlight: Plugin<[LazyHighlightOptions?], Root> = (options = {}) => {
+	const extraAliases = new Map<string, string>();
+
+	for (const [language, aliases] of Object.entries(options.aliases ?? {})) {
+		for (const alias of [aliases].flat()) extraAliases.set(alias.toLowerCase(), language);
+	}
+
+	return (tree) => {
+		const blocks: CodeBlock[] = [];
+
+		visit(tree, 'element', (node: Element, _, parent) => {
+			if (node.tagName !== 'code' || parent?.type !== 'element' || parent.tagName !== 'pre') {
+				return;
+			}
+
+			const language = classLanguage(node);
+
+			if (!language) return;
+
+			const key = language.toLowerCase();
+
+			blocks.push({ name: resolveHighlightLanguage(extraAliases.get(key) ?? key), node });
+		});
+
+		if (!blocks.length) return;
+
+		const names = [
+			...new Set(blocks.map((block) => block.name).filter((name) => name !== undefined))
+		];
+
+		if (lowlight && names.every((name) => registered.has(name))) {
+			highlightBlocks(lowlight, blocks);
+
+			return;
+		}
+
+		return Promise.all([loadLowlight(), loadHighlightLanguages(names)]).then(([target]) => {
+			registerLoadedLanguages(target);
+			highlightBlocks(target, blocks);
+		});
+	};
+};
diff --git a/ui/apps/chat/src/lib/components/app/content/MarkdownContent/plugins/rehype/lowlight-core.ts b/ui/apps/chat/src/lib/components/app/content/MarkdownContent/plugins/rehype/lowlight-core.ts
new file mode 100644
index 000000000..86a9bdf08
--- /dev/null
+++ b/ui/apps/chat/src/lib/components/app/content/MarkdownContent/plugins/rehype/lowlight-core.ts
@@ -0,0 +1,3 @@
+// lowlight without its bundled language sets, for lazy-highlight.ts to load with the first code block: a static
+// re-export of createLowlight alone lets the build drop lowlight's `common` and `all`
+export { createLowlight } from 'lowlight';
diff --git a/ui/apps/chat/src/lib/components/app/content/MarkdownContent/plugins/rehype/pre-transform.ts b/ui/apps/chat/src/lib/components/app/content/MarkdownContent/plugins/rehype/pre-transform.ts
index af45ff0aa..309ad9bd9 100644
--- a/ui/apps/chat/src/lib/components/app/content/MarkdownContent/plugins/rehype/pre-transform.ts
+++ b/ui/apps/chat/src/lib/components/app/content/MarkdownContent/plugins/rehype/pre-transform.ts
@@ -1,3 +1,4 @@
+import { CODE_BLOCK } from '#lib/constants/index.ts';
 import type { Element, ElementContent, Root, Text } from 'hast';
 import type { Plugin } from 'unified';
 import { visit } from 'unist-util-visit';
@@ -50,7 +51,7 @@ export function createPreTransform(
 	contentGuard?: (text: string) => boolean
 ): Plugin<[], Root> {
 	const codeClasses = (Array.isArray(languages) ? languages : [languages]).map(
-		(language) => `language-${language}`
+		(language) => `${CODE_BLOCK.LANGUAGE_CLASS_PREFIX}${language}`
 	);

 	return () => {
diff --git a/ui/apps/chat/src/lib/utils/code.ts b/ui/apps/chat/src/lib/utils/code.ts
index cc9eba2d3..be556c3fc 100644
--- a/ui/apps/chat/src/lib/utils/code.ts
+++ b/ui/apps/chat/src/lib/utils/code.ts
@@ -1,6 +1,13 @@
 import { CODE_BLOCK, NEWLINE } from '#lib/constants/index.ts';

-export { highlightCode, trimCodePadding } from '@ggml-org/llama-ui/code';
+export {
+	getLoadedHighlightLanguages,
+	highlightCode,
+	isHighlightLanguageLoaded,
+	loadHighlightLanguages,
+	resolveHighlightLanguage,
+	trimCodePadding
+} from '@ggml-org/llama-ui/code';

 export interface IncompleteCodeBlock {
 	language: string;
diff --git a/ui/apps/chat/src/lib/utils/index.ts b/ui/apps/chat/src/lib/utils/index.ts
index 61c202ab0..1f1d46844 100644
--- a/ui/apps/chat/src/lib/utils/index.ts
+++ b/ui/apps/chat/src/lib/utils/index.ts
@@ -33,6 +33,8 @@ export {
 // Code
 export {
 	highlightCode,
+	isHighlightLanguageLoaded,
+	loadHighlightLanguages,
 	detectIncompleteCodeBlock,
 	splitGluedClosingCodeFences,
 	trimCodePadding,
diff --git a/ui/apps/chat/tests/unit/agentic-hotpath.bench.ts b/ui/apps/chat/tests/unit/agentic-hotpath.bench.ts
index 685ccdf68..c46f3fe4b 100644
--- a/ui/apps/chat/tests/unit/agentic-hotpath.bench.ts
+++ b/ui/apps/chat/tests/unit/agentic-hotpath.bench.ts
@@ -7,7 +7,11 @@
 // Run: npx vitest bench --project=unit tests/unit/agentic-hotpath.bench.ts

 import { classifyToolResult, parseToolResultWithMedia } from '#lib/utils/agentic.ts';
-import { detectIncompleteCodeBlock, highlightCode } from '#lib/utils/code.ts';
+import {
+	detectIncompleteCodeBlock,
+	highlightCode,
+	loadHighlightLanguages
+} from '#lib/utils/code.ts';
 import { computeLineDiff } from '#lib/utils/compute-line-diff.ts';
 import { preprocessLaTeX } from '#lib/utils/latex-protection.ts';
 import { parsePartialJsonArgs } from '#lib/utils/parse-partial-json-args.ts';
@@ -228,6 +232,9 @@ describe('parsePartialJsonArgs', () => {

 // --- F / I: highlight.js ---------------------------------------------------

+// highlightCode returns plain text until highlight.js has loaded
+await loadHighlightLanguages();
+
 describe('highlightCode', () => {
 	bench('short bash command (title row, per token)', () => {
 		highlightCode('grep -rn "foo" src/ | head -50', 'bash');
diff --git a/ui/apps/chat/tests/unit/code.test.ts b/ui/apps/chat/tests/unit/code.test.ts
index 2ecaf69f0..31f0c1846 100644
--- a/ui/apps/chat/tests/unit/code.test.ts
+++ b/ui/apps/chat/tests/unit/code.test.ts
@@ -1,5 +1,12 @@
-import { highlightCode, splitGluedClosingCodeFences, trimCodePadding } from '#lib/utils/code.ts';
-import { describe, expect, it } from 'vitest';
+import {
+	highlightCode,
+	loadHighlightLanguages,
+	splitGluedClosingCodeFences,
+	trimCodePadding
+} from '#lib/utils/code.ts';
+import { beforeAll, describe, expect, it } from 'vitest';
+
+beforeAll(() => loadHighlightLanguages());

 describe('trimCodePadding', () => {
 	it('removes a single leading newline', () => {
diff --git a/ui/apps/chat/tests/unit/lazy-highlight.test.ts b/ui/apps/chat/tests/unit/lazy-highlight.test.ts
new file mode 100644
index 000000000..f766706c2
--- /dev/null
+++ b/ui/apps/chat/tests/unit/lazy-highlight.test.ts
@@ -0,0 +1,117 @@
+import { rehypeLazyHighlight } from '#lib/components/app/content/MarkdownContent/plugins/rehype/lazy-highlight.ts';
+import {
+	highlightCode,
+	isHighlightLanguageLoaded,
+	loadHighlightLanguages,
+	resolveHighlightLanguage
+} from '#lib/utils/code.ts';
+import type { Root } from 'hast';
+import rehypeStringify from 'rehype-stringify';
+import { remark } from 'remark';
+import remarkRehype from 'remark-rehype';
+import { describe, expect, it } from 'vitest';
+
+// one file, so these run in order against one fresh loader: nothing has loaded when it starts
+describe('highlightCode before its language loads', () => {
+	it('resolves aliases to highlight.js names', () => {
+		expect(resolveHighlightLanguage('ts')).toBe('typescript');
+		expect(resolveHighlightLanguage('SH')).toBe('bash');
+		expect(resolveHighlightLanguage('yml')).toBe('yaml');
+		expect(resolveHighlightLanguage('python')).toBe('python');
+		expect(resolveHighlightLanguage('constructor')).toBeUndefined();
+		expect(resolveHighlightLanguage('not-a-language')).toBeUndefined();
+	});
+
+	it('returns plain text, then highlights once that language (and only it) has loaded', async () => {
+		expect(highlightCode('\nconst x = 1;\n', 'ts')).toBe('const x = 1;');
+
+		await loadHighlightLanguages(['ts']);
+
+		expect(isHighlightLanguageLoaded('typescript')).toBe(true);
+		expect(highlightCode('const x = 1;', 'ts')).toContain('hljs-keyword');
+		expect(isHighlightLanguageLoaded('python')).toBe(false);
+	});
+
+	it('loads the languages a language embeds with it', async () => {
+		await loadHighlightLanguages(['html']);
+
+		expect(isHighlightLanguageLoaded('css')).toBe(true);
+		expect(highlightCode('<style>a { color: red; }</style>', 'html')).toContain('hljs-attribute');
+	});
+
+	it('leaves an unknown language plain without auto-detection', () => {
+		expect(highlightCode('<b>a && b</b>', 'not-a-language', false)).toBe(
+			'&lt;b&gt;a &amp;&amp; b&lt;/b&gt;'
+		);
+	});
+
+	it('auto-detects an unknown language among the common languages once they load', async () => {
+		const code = 'def greet(name):\n    return f"hi {name}"';
+
+		expect(highlightCode(code, 'not-a-language')).toBe(code);
+
+		// that call started loading every common language
+		const html = await waitForHighlight(() => highlightCode(code, 'not-a-language'));
+
+		expect(html).toContain('hljs-');
+	});
+});
+
+async function waitForHighlight(highlight: () => string): Promise<string> {
+	for (let i = 0; i < 50; i++) {
+		const html = highlight();
+
+		if (html.includes('hljs-')) return html;
+
+		await new Promise((resolve) => setTimeout(resolve, 20));
+	}
+
+	return highlight();
+}
+
+const processor = remark()
+	.use(remarkRehype)
+	.use(rehypeLazyHighlight, { aliases: { xml: ['svelte'] } })
+	.use(rehypeStringify);
+
+async function render(markdown: string): Promise<string> {
+	return processor.stringify((await processor.run(processor.parse(markdown))) as Root);
+}
+
+describe('rehypeLazyHighlight', () => {
+	it('leaves a message without a code block as it is, in the same pass', () => {
+		const tree = processor.runSync(processor.parse('Just *text* and `code`')) as Root;
+
+		expect(processor.stringify(tree)).toBe('<p>Just <em>text</em> and <code>code</code></p>');
+	});
+
+	it('loads a block language, then highlights it in the same pass', async () => {
+		const markdown = '```rust\nfn main() {}\n```';
+		const first = await render(markdown);
+
+		expect(first).toContain('<code class="hljs language-rust">');
+		expect(first).toContain('hljs-keyword');
+
+		const second = processor.stringify(processor.runSync(processor.parse(markdown)) as Root);
+
+		expect(second).toBe(first);
+	});
+
+	it('uses highlight.js definitions, not highlightCode bash changes', async () => {
+		const html = await render('```sh\ncurl -LsSf https://example.com\n```');
+
+		expect(html).toContain('<code class="hljs language-sh">');
+		expect(html).not.toContain('hljs-built_in">curl');
+	});
+
+	it('takes extra aliases', async () => {
+		expect(await render('```svelte\n<div>{x}</div>\n```')).toContain('hljs-tag');
+	});
+
+	it('leaves unknown languages, no-highlight and blocks without a language plain', async () => {
+		expect(await render('```not-a-language\nx = 1\n```')).toBe(
+			'<pre><code class="hljs language-not-a-language">x = 1\n</code></pre>'
+		);
+		expect(await render('```\nx = 1\n```')).toBe('<pre><code>x = 1\n</code></pre>');
+	});
+});
diff --git a/ui/lib/ui/README.md b/ui/lib/ui/README.md
index 2c701d8a6..9fd1c350c 100644
--- a/ui/lib/ui/README.md
+++ b/ui/lib/ui/README.md
@@ -49,7 +49,13 @@ Next: llama.app's model page.

 The constants, enums and code helpers the components use are entry points too, for an app that shows the same things:
 `@ggml-org/llama-ui/constants` (the shared classes and code-block constants), `@ggml-org/llama-ui/enums` (`ColorMode`,
-`TooltipSide`, `BooleanString`) and `@ggml-org/llama-ui/code` (`highlightCode`, `trimCodePadding`). The model data
+`TooltipSide`, `BooleanString`) and `@ggml-org/llama-ui/code` (`highlightCode`, `trimCodePadding`,
+`loadHighlightLanguages`, `isHighlightLanguageLoaded`, `resolveHighlightLanguage`, `getLoadedHighlightLanguages`).
+highlight.js loads one language at a time, each its own chunk, so none is in an app's first load: until a language
+has loaded, `highlightCode` returns the code escaped as plain text, and a `$derived` or `$effect` that called it runs
+again once it has. A language loads with the ones it embeds (CSS and JavaScript inside HTML); auto-detection picks
+among lowlight's common languages. Await `loadHighlightLanguages([...])` where highlighted output is needed at once.
+`scripts/generate-highlight-languages.mjs` regenerates the language table after a highlight.js or lowlight update. The model data
 (constants, enums, formatters and types) comes with the Model UI at `@ggml-org/llama-ui/models`.

 ## How the apps in this repo use it
diff --git a/ui/lib/ui/scripts/generate-highlight-languages.mjs b/ui/lib/ui/scripts/generate-highlight-languages.mjs
new file mode 100644
index 000000000..38ca8551d
--- /dev/null
+++ b/ui/lib/ui/scripts/generate-highlight-languages.mjs
@@ -0,0 +1,98 @@
+// Writes src/lib/utils/highlight-languages.ts: one loader per highlight.js language lowlight ships (each its own chunk
+// in an app's build), the aliases that name them, and lowlight's common languages, which auto-detection picks from.
+// Run it after updating highlight.js or lowlight: node scripts/generate-highlight-languages.mjs
+import { execFileSync } from "node:child_process";
+import { writeFileSync } from "node:fs";
+import hljs from "highlight.js/lib/core";
+import { all, common } from "lowlight";
+
+const OUTPUT = new URL(
+  "../src/lib/utils/highlight-languages.ts",
+  import.meta.url,
+);
+
+const names = Object.keys(all).sort();
+const aliases = {};
+const subLanguages = {};
+
+// the languages a definition embeds (`subLanguage`: CSS and JavaScript inside HTML, say), which highlight.js leaves
+// plain unless they are registered too
+function collectSubLanguages(mode, found, seen = new Set()) {
+  if (!mode || typeof mode !== "object" || seen.has(mode)) return found;
+
+  seen.add(mode);
+
+  for (const sub of [mode.subLanguage ?? []].flat())
+    found.add(sub.toLowerCase());
+
+  for (const value of Object.values(mode)) {
+    if (Array.isArray(value))
+      for (const item of value) collectSubLanguages(item, found, seen);
+    else if (value && typeof value === "object")
+      collectSubLanguages(value, found, seen);
+  }
+
+  return found;
+}
+
+// register in lowlight's order, so an alias two languages claim names the same one it does in lowlight
+for (const name of names) {
+  const highlighter = hljs.newInstance();
+
+  highlighter.registerLanguage(name, all[name]);
+
+  for (const alias of highlighter.getLanguage(name)?.aliases ?? []) {
+    const key = alias.toLowerCase();
+
+    if (key !== name && !(key in all)) aliases[key] = name;
+  }
+}
+
+for (const name of names) {
+  const definition = all[name](hljs);
+  const found = [...collectSubLanguages(definition, new Set())]
+    .map((sub) => (sub in all ? sub : aliases[sub]))
+    .filter((sub) => sub && sub !== name);
+
+  if (found.length) subLanguages[name] = [...new Set(found)].sort();
+}
+
+const quote = (s) => `'${s}'`;
+const key = (s) => (/^[a-z_$][\w$]*$/i.test(s) ? s : quote(s));
+
+const source = `// Generated by scripts/generate-highlight-languages.mjs from lowlight's languages; do not edit by hand.
+import type { LanguageFn } from 'highlight.js';
+
+/** Loads one highlight.js language, by its name. */
+export const HIGHLIGHT_LANGUAGE_LOADERS: Record<string, () => Promise<{ default: LanguageFn }>> = {
+${names.map((n) => `\t${key(n)}: () => import('highlight.js/lib/languages/${n}')`).join(",\n")}
+};
+
+/** The language each alias names (\`js\` -> \`javascript\`), as highlight.js resolves it. */
+export const HIGHLIGHT_LANGUAGE_ALIASES: Record<string, string> = {
+${Object.keys(aliases)
+  .sort()
+  .map((a) => `\t${key(a)}: ${quote(aliases[a])}`)
+  .join(",\n")}
+};
+
+/** The languages each language embeds, to load with it so its embedded code is highlighted too. */
+export const HIGHLIGHT_SUB_LANGUAGES: Record<string, readonly string[]> = {
+${Object.keys(subLanguages)
+  .map((n) => `\t${key(n)}: [${subLanguages[n].map(quote).join(", ")}]`)
+  .join(",\n")}
+};
+
+/** lowlight's common languages: the ones auto-detection picks from. */
+export const HIGHLIGHT_AUTO_DETECT_LANGUAGES: readonly string[] = [
+${Object.keys(common)
+  .sort()
+  .map((n) => `\t${quote(n)}`)
+  .join(",\n")}
+];
+`;
+
+writeFileSync(OUTPUT, source);
+execFileSync("npx", ["prettier", "--write", OUTPUT.pathname], {
+  stdio: "inherit",
+});
diff --git a/ui/lib/ui/src/lib/constants/code-block.constants.ts b/ui/lib/ui/src/lib/constants/code-block.constants.ts
index d395f861e..836d619d3 100644
--- a/ui/lib/ui/src/lib/constants/code-block.constants.ts
+++ b/ui/lib/ui/src/lib/constants/code-block.constants.ts
@@ -8,9 +8,15 @@ export const CODE_BLOCK = {
   /** Matches opening/closing markdown code fences. */
   FENCE_PATTERN: /^```|\n```/g,
   GT_REGEX: />/g,
+  /** Short form of the class prefix that names a code element's language (`lang-ts`). */
+  LANG_CLASS_PREFIX: "lang-",
+  /** Class prefix that names a code element's language (`language-ts`). */
+  LANGUAGE_CLASS_PREFIX: "language-",
   /** Matches the language specifier at the start of a code fence. */
   LANG_PATTERN: /^(\w*)\n?/,
   LT_REGEX: /</g,
+  /** Classes that opt a code element out of highlighting. */
+  NO_HIGHLIGHT_CLASSES: ["no-highlight", "nohighlight"],
   /**
    * A shell command's name: the first word of the code or of a line (unless the line before it ends in a `\` continuation),
    * or the first after `|`, `;`, `&`, `(` or `$(`, but not a shell keyword.
@@ -20,6 +26,8 @@ export const CODE_BLOCK = {
     /(?<=(?:(?<![\s\S])|[^\\\s][ \t]*\n|\n\n|[|;&(][ \t]*|\$\([ \t]*)[ \t]*)(?!(?:if|then|else|elif|fi|for|while|until|do|done|case|esac|function|in|select|time)\b)[A-Za-z_][\w.+-]*(?=[ \t\n;|&)]|$)/,
   /** A shell command's flag: `-x`, `-LsSf` or `--long-name`, after whitespace. */
   SHELL_FLAG_REGEX: /(?<=[ \t])--?[A-Za-z0-9][\w-]*/,
+  /** The highlight.js language that also marks a shell command's name and flags. */
+  SHELL_LANGUAGE: "bash",

   // Matches the `text:` prefix that file-type identifiers use to denote a
   // plain-text language (e.g. `text:typescript`). Used by tool-call renderers
@@ -51,6 +59,8 @@ export const CODE_BLOCK_CLASS = {
   ACTIONS: "code-block-actions",
   COPY_BTN: "copy-code-btn",
   HEADER: "code-block-header",
+  /** highlight.js's class on a highlighted code element. */
+  HLJS: "hljs",
   LANGUAGE: "code-language",
   PREVIEW_BTN: "preview-code-btn",
   RELATIVE: "relative",
diff --git a/ui/lib/ui/src/lib/index.ts b/ui/lib/ui/src/lib/index.ts
index 8fe7855e9..542e7d84a 100644
--- a/ui/lib/ui/src/lib/index.ts
+++ b/ui/lib/ui/src/lib/index.ts
@@ -7,7 +7,7 @@ export { SearchInput } from './components/app/forms/index.js';
 export { Logo } from './components/app/misc/index.js';
 export { FILTER_TOGGLE_ITEM_CLASS, FILTER_TRIGGER_CLASS, PANEL_CLOSE_MOBILE_CLASS } from './constants/index.js';
 export { ColorMode, TooltipSide } from './enums/index.js';
-export { highlightCode } from './utils/index.js';
+export { highlightCode, isHighlightLanguageLoaded, loadHighlightLanguages, resolveHighlightLanguage } from './utils/index.js';
 export * as Alert from './components/ui/alert/index.js';
 export * as AlertDialog from './components/ui/alert-dialog/index.js';
 export { Badge, badgeVariants, type BadgeVariant } from './components/ui/badge/index.js';
diff --git a/ui/lib/ui/src/lib/utils/code.ts b/ui/lib/ui/src/lib/utils/code.ts
index f8c5ba5fc..4dc2492a1 100644
--- a/ui/lib/ui/src/lib/utils/code.ts
+++ b/ui/lib/ui/src/lib/utils/code.ts
@@ -1,26 +1,143 @@
 // highlightCode and its helpers, from ui/apps/chat's utils/code.ts (only what SyntaxHighlightedCode uses)
 import { CODE_BLOCK } from "../constants";
-import hljs from "highlight.js/lib/core";
-import bash from "highlight.js/lib/languages/bash";
-import { all as languages } from "lowlight";
-
-// reuse lowlight's language modules: the full `highlight.js` entry bundles a second, CommonJS copy of every language
-for (const [name, language] of Object.entries(languages)) hljs.registerLanguage(name, language);
+import {
+  HIGHLIGHT_AUTO_DETECT_LANGUAGES,
+  HIGHLIGHT_LANGUAGE_ALIASES,
+  HIGHLIGHT_LANGUAGE_LOADERS,
+  HIGHLIGHT_SUB_LANGUAGES,
+} from "./highlight-languages";
+import type { HLJSApi, LanguageFn } from "highlight.js";
+import { createSubscriber } from "svelte/reactivity";
+
+// highlight.js and each language load on first use, every language its own chunk, so an app's first load carries none
+let hljs: HLJSApi | undefined;
+let coreLoading: Promise<HLJSApi> | undefined;
+const languageLoading = new Map<string, Promise<void>>();
+/** Languages loaded together with every language they embed, so highlightCode can use them. */
+const readyLanguages = new Set<string>();
+/** Each loaded language's definition as highlight.js ships it, without this module's bash changes. */
+const languageDefinitions = new Map<string, LanguageFn>();
+const loadListeners = new Set<() => void>();
+
+// lets a $derived or $effect that called highlightCode before its language loaded run again once it has
+const subscribeToLoad = createSubscriber((update) => {
+  loadListeners.add(update);
+
+  return () => loadListeners.delete(update);
+});

 // highlight.js's bash marks strings, variables and keywords but leaves a command's name and its flags plain, so a
 // command (`curl -H … -d …`) reads as one colour. Mark them too: the name as a built-in, the flags as attributes.
-hljs.registerLanguage("bash", (api) => {
-  const base = bash(api);
-
-  return {
-    ...base,
-    contains: [
-      { match: CODE_BLOCK.SHELL_COMMAND_REGEX, scope: "built_in" },
-      { match: CODE_BLOCK.SHELL_FLAG_REGEX, scope: "attr" },
-      ...(base.contains ?? []),
-    ],
+function withShellCommands(bash: LanguageFn): LanguageFn {
+  return (api) => {
+    const base = bash(api);
+
+    return {
+      ...base,
+      contains: [
+        { match: CODE_BLOCK.SHELL_COMMAND_REGEX, scope: "built_in" },
+        { match: CODE_BLOCK.SHELL_FLAG_REGEX, scope: "attr" },
+        ...(base.contains ?? []),
+      ],
+    };
   };
-});
+}
+
+/** The highlight.js name of a language or one of its aliases (`ts` -> `typescript`), or undefined for one it lacks. */
+export function resolveHighlightLanguage(language: string): string | undefined {
+  const key = language.toLowerCase();
+
+  if (Object.hasOwn(HIGHLIGHT_LANGUAGE_LOADERS, key)) return key;
+
+  return Object.hasOwn(HIGHLIGHT_LANGUAGE_ALIASES, key)
+    ? HIGHLIGHT_LANGUAGE_ALIASES[key]
+    : undefined;
+}
+
+/** Whether a language (or alias) has loaded, so highlightCode highlights it rather than returning plain text. */
+export function isHighlightLanguageLoaded(language: string): boolean {
+  const name = resolveHighlightLanguage(language);
+
+  return name !== undefined && readyLanguages.has(name);
+}
+
+/** The definitions of every loaded language, as highlight.js ships them, to register with another highlighter. */
+export function getLoadedHighlightLanguages(): Record<string, LanguageFn> {
+  return Object.fromEntries(
+    [...readyLanguages].map((name) => [name, languageDefinitions.get(name)!]),
+  );
+}
+
+function loadCore(): Promise<HLJSApi> {
+  coreLoading ??= import("highlight.js/lib/core").then(
+    (module) => (hljs = module.default.newInstance()),
+    (error) => {
+      coreLoading = undefined;
+
+      throw error;
+    },
+  );
+
+  return coreLoading;
+}
+
+function loadLanguage(name: string): Promise<void> {
+  let loading = languageLoading.get(name);
+
+  if (!loading) {
+    loading = Promise.all([
+      loadCore(),
+      HIGHLIGHT_LANGUAGE_LOADERS[name](),
+    ]).then(([core, module]) => {
+      languageDefinitions.set(name, module.default);
+      core.registerLanguage(
+        name,
+        name === CODE_BLOCK.SHELL_LANGUAGE
+          ? withShellCommands(module.default)
+          : module.default,
+      );
+    });
+    // let a later call try again
+    loading.catch(() => languageLoading.delete(name));
+    languageLoading.set(name, loading);
+  }
+
+  return loading;
+}
+
+/**
+ * Loads languages (names or aliases; unknown ones are skipped) with every language they embed, or every language
+ * when called without any. highlightCode loads what it needs itself; await this where highlighted output is needed
+ * right away, as in tests or a server render.
+ */
+export async function loadHighlightLanguages(
+  languages?: readonly string[],
+): Promise<void> {
+  const names = new Set<string>();
+  const queue = languages
+    ? languages
+        .map(resolveHighlightLanguage)
+        .filter((name) => name !== undefined)
+    : Object.keys(HIGHLIGHT_LANGUAGE_LOADERS);
+
+  while (queue.length) {
+    const name = queue.pop()!;
+
+    if (names.has(name)) continue;
+
+    names.add(name);
+    queue.push(...(HIGHLIGHT_SUB_LANGUAGES[name] ?? []));
+  }
+
+  const missing = [...names].filter((name) => !readyLanguages.has(name));
+
+  if (!missing.length) return;
+
+  await Promise.all(missing.map(loadLanguage));
+
+  for (const name of missing) readyLanguages.add(name);
+  for (const update of [...loadListeners]) update();
+}

 /**
  * Strips empty lines (whitespace-only) from the start and end of code, so hljs emits no phantom row before or after
@@ -42,7 +159,13 @@ function escapeCode(code: string): string {
 const HIGHLIGHT_CACHE_MAX_SIZE = 64;
 const highlightCache = new Map<string, string>();

-/** Highlights code with highlight.js in the given language, auto-detecting one it doesn't know. */
+/**
+ * Highlights code with highlight.js in the given language (a name or an alias). For a language highlight.js lacks it
+ * auto-detects one among lowlight's common languages, unless autoDetect is false.
+ *
+ * Each language loads on first use: until it has, this returns the code escaped as plain text, and a Svelte $derived
+ * or $effect that called it runs again once it has loaded. Await loadHighlightLanguages() first to skip that.
+ */
 export function highlightCode(
   code: string,
   language: string,
@@ -50,25 +173,34 @@ export function highlightCode(
 ): string {
   if (!code) return "";

-  const cacheKey = `${language}:${autoDetect}:${code}`;
+  const name = resolveHighlightLanguage(language);
+  const needed = name
+    ? [name]
+    : autoDetect
+      ? HIGHLIGHT_AUTO_DETECT_LANGUAGES
+      : [];
+  const trimmed = trimCodePadding(code);
+
+  if (!needed.length) return escapeCode(trimmed);
+
+  if (!hljs || !needed.every((n) => readyLanguages.has(n))) {
+    subscribeToLoad();
+    loadHighlightLanguages(needed).catch(() => {});
+
+    return escapeCode(trimmed);
+  }
+
+  const cacheKey = `${name ?? ""}:${code}`;
   const cached = highlightCache.get(cacheKey);

   if (cached) return cached;

-  const trimmed = trimCodePadding(code);
-
   let result: string;

   try {
-    const lang = language.toLowerCase();
-
-    if (hljs.getLanguage(lang)) {
-      result = hljs.highlight(trimmed, { language: lang }).value;
-    } else if (autoDetect) {
-      result = hljs.highlightAuto(trimmed).value;
-    } else {
-      result = escapeCode(trimmed);
-    }
+    result = name
+      ? hljs.highlight(trimmed, { language: name }).value
+      : hljs.highlightAuto(trimmed, [...HIGHLIGHT_AUTO_DETECT_LANGUAGES]).value;
   } catch {
     result = escapeCode(trimmed);
   }
diff --git a/ui/lib/ui/src/lib/utils/highlight-languages.ts b/ui/lib/ui/src/lib/utils/highlight-languages.ts
new file mode 100644
index 000000000..e52bcc6ec
--- /dev/null
+++ b/ui/lib/ui/src/lib/utils/highlight-languages.ts
@@ -0,0 +1,474 @@
+// Generated by scripts/generate-highlight-languages.mjs from lowlight's languages; do not edit by hand.
+import type { LanguageFn } from "highlight.js";
+
+/** Loads one highlight.js language, by its name. */
+export const HIGHLIGHT_LANGUAGE_LOADERS: Record<
+  string,
+  () => Promise<{ default: LanguageFn }>
+> = {
+  "1c": () => import("highlight.js/lib/languages/1c"),
+  abnf: () => import("highlight.js/lib/languages/abnf"),
+  accesslog: () => import("highlight.js/lib/languages/accesslog"),
+  actionscript: () => import("highlight.js/lib/languages/actionscript"),
+  ada: () => import("highlight.js/lib/languages/ada"),
+  angelscript: () => import("highlight.js/lib/languages/angelscript"),
+  apache: () => import("highlight.js/lib/languages/apache"),
+  applescript: () => import("highlight.js/lib/languages/applescript"),
+  arcade: () => import("highlight.js/lib/languages/arcade"),
+  arduino: () => import("highlight.js/lib/languages/arduino"),
+  armasm: () => import("highlight.js/lib/languages/armasm"),
+  asciidoc: () => import("highlight.js/lib/languages/asciidoc"),
+  aspectj: () => import("highlight.js/lib/languages/aspectj"),
+  autohotkey: () => import("highlight.js/lib/languages/autohotkey"),
+  autoit: () => import("highlight.js/lib/languages/autoit"),
+  avrasm: () => import("highlight.js/lib/languages/avrasm"),
+  awk: () => import("highlight.js/lib/languages/awk"),
+  axapta: () => import("highlight.js/lib/languages/axapta"),
+  bash: () => import("highlight.js/lib/languages/bash"),
+  basic: () => import("highlight.js/lib/languages/basic"),
+  bnf: () => import("highlight.js/lib/languages/bnf"),
+  brainfuck: () => import("highlight.js/lib/languages/brainfuck"),
+  c: () => import("highlight.js/lib/languages/c"),
+  cal: () => import("highlight.js/lib/languages/cal"),
+  capnproto: () => import("highlight.js/lib/languages/capnproto"),
+  ceylon: () => import("highlight.js/lib/languages/ceylon"),
+  clean: () => import("highlight.js/lib/languages/clean"),
+  clojure: () => import("highlight.js/lib/languages/clojure"),
+  "clojure-repl": () => import("highlight.js/lib/languages/clojure-repl"),
+  cmake: () => import("highlight.js/lib/languages/cmake"),
+  coffeescript: () => import("highlight.js/lib/languages/coffeescript"),
+  coq: () => import("highlight.js/lib/languages/coq"),
+  cos: () => import("highlight.js/lib/languages/cos"),
+  cpp: () => import("highlight.js/lib/languages/cpp"),
+  crmsh: () => import("highlight.js/lib/languages/crmsh"),
+  crystal: () => import("highlight.js/lib/languages/crystal"),
+  csharp: () => import("highlight.js/lib/languages/csharp"),
+  csp: () => import("highlight.js/lib/languages/csp"),
+  css: () => import("highlight.js/lib/languages/css"),
+  d: () => import("highlight.js/lib/languages/d"),
+  dart: () => import("highlight.js/lib/languages/dart"),
+  delphi: () => import("highlight.js/lib/languages/delphi"),
+  diff: () => import("highlight.js/lib/languages/diff"),
+  django: () => import("highlight.js/lib/languages/django"),
+  dns: () => import("highlight.js/lib/languages/dns"),
+  dockerfile: () => import("highlight.js/lib/languages/dockerfile"),
+  dos: () => import("highlight.js/lib/languages/dos"),
+  dsconfig: () => import("highlight.js/lib/languages/dsconfig"),
+  dts: () => import("highlight.js/lib/languages/dts"),
+  dust: () => import("highlight.js/lib/languages/dust"),
+  ebnf: () => import("highlight.js/lib/languages/ebnf"),
+  elixir: () => import("highlight.js/lib/languages/elixir"),
+  elm: () => import("highlight.js/lib/languages/elm"),
+  erb: () => import("highlight.js/lib/languages/erb"),
+  erlang: () => import("highlight.js/lib/languages/erlang"),
+  "erlang-repl": () => import("highlight.js/lib/languages/erlang-repl"),
+  excel: () => import("highlight.js/lib/languages/excel"),
+  fix: () => import("highlight.js/lib/languages/fix"),
+  flix: () => import("highlight.js/lib/languages/flix"),
+  fortran: () => import("highlight.js/lib/languages/fortran"),
+  fsharp: () => import("highlight.js/lib/languages/fsharp"),
+  gams: () => import("highlight.js/lib/languages/gams"),
+  gauss: () => import("highlight.js/lib/languages/gauss"),
+  gcode: () => import("highlight.js/lib/languages/gcode"),
+  gherkin: () => import("highlight.js/lib/languages/gherkin"),
+  glsl: () => import("highlight.js/lib/languages/glsl"),
+  gml: () => import("highlight.js/lib/languages/gml"),
+  go: () => import("highlight.js/lib/languages/go"),
+  golo: () => import("highlight.js/lib/languages/golo"),
+  gradle: () => import("highlight.js/lib/languages/gradle"),
+  graphql: () => import("highlight.js/lib/languages/graphql"),
+  groovy: () => import("highlight.js/lib/languages/groovy"),
+  haml: () => import("highlight.js/lib/languages/haml"),
+  handlebars: () => import("highlight.js/lib/languages/handlebars"),
+  haskell: () => import("highlight.js/lib/languages/haskell"),
+  haxe: () => import("highlight.js/lib/languages/haxe"),
+  hsp: () => import("highlight.js/lib/languages/hsp"),
+  http: () => import("highlight.js/lib/languages/http"),
+  hy: () => import("highlight.js/lib/languages/hy"),
+  inform7: () => import("highlight.js/lib/languages/inform7"),
+  ini: () => import("highlight.js/lib/languages/ini"),
+  irpf90: () => import("highlight.js/lib/languages/irpf90"),
+  isbl: () => import("highlight.js/lib/languages/isbl"),
+  java: () => import("highlight.js/lib/languages/java"),
+  javascript: () => import("highlight.js/lib/languages/javascript"),
+  "jboss-cli": () => import("highlight.js/lib/languages/jboss-cli"),
+  json: () => import("highlight.js/lib/languages/json"),
+  julia: () => import("highlight.js/lib/languages/julia"),
+  "julia-repl": () => import("highlight.js/lib/languages/julia-repl"),
+  kotlin: () => import("highlight.js/lib/languages/kotlin"),
+  lasso: () => import("highlight.js/lib/languages/lasso"),
+  latex: () => import("highlight.js/lib/languages/latex"),
+  ldif: () => import("highlight.js/lib/languages/ldif"),
+  leaf: () => import("highlight.js/lib/languages/leaf"),
+  less: () => import("highlight.js/lib/languages/less"),
+  lisp: () => import("highlight.js/lib/languages/lisp"),
+  livecodeserver: () => import("highlight.js/lib/languages/livecodeserver"),
+  livescript: () => import("highlight.js/lib/languages/livescript"),
+  llvm: () => import("highlight.js/lib/languages/llvm"),
+  lsl: () => import("highlight.js/lib/languages/lsl"),
+  lua: () => import("highlight.js/lib/languages/lua"),
+  makefile: () => import("highlight.js/lib/languages/makefile"),
+  markdown: () => import("highlight.js/lib/languages/markdown"),
+  mathematica: () => import("highlight.js/lib/languages/mathematica"),
+  matlab: () => import("highlight.js/lib/languages/matlab"),
+  maxima: () => import("highlight.js/lib/languages/maxima"),
+  mel: () => import("highlight.js/lib/languages/mel"),
+  mercury: () => import("highlight.js/lib/languages/mercury"),
+  mipsasm: () => import("highlight.js/lib/languages/mipsasm"),
+  mizar: () => import("highlight.js/lib/languages/mizar"),
+  mojolicious: () => import("highlight.js/lib/languages/mojolicious"),
+  monkey: () => import("highlight.js/lib/languages/monkey"),
+  moonscript: () => import("highlight.js/lib/languages/moonscript"),
+  n1ql: () => import("highlight.js/lib/languages/n1ql"),
+  nestedtext: () => import("highlight.js/lib/languages/nestedtext"),
+  nginx: () => import("highlight.js/lib/languages/nginx"),
+  nim: () => import("highlight.js/lib/languages/nim"),
+  nix: () => import("highlight.js/lib/languages/nix"),
+  "node-repl": () => import("highlight.js/lib/languages/node-repl"),
+  nsis: () => import("highlight.js/lib/languages/nsis"),
+  objectivec: () => import("highlight.js/lib/languages/objectivec"),
+  ocaml: () => import("highlight.js/lib/languages/ocaml"),
+  openscad: () => import("highlight.js/lib/languages/openscad"),
+  oxygene: () => import("highlight.js/lib/languages/oxygene"),
+  parser3: () => import("highlight.js/lib/languages/parser3"),
+  perl: () => import("highlight.js/lib/languages/perl"),
+  pf: () => import("highlight.js/lib/languages/pf"),
+  pgsql: () => import("highlight.js/lib/languages/pgsql"),
+  php: () => import("highlight.js/lib/languages/php"),
+  "php-template": () => import("highlight.js/lib/languages/php-template"),
+  plaintext: () => import("highlight.js/lib/languages/plaintext"),
+  pony: () => import("highlight.js/lib/languages/pony"),
+  powershell: () => import("highlight.js/lib/languages/powershell"),
+  processing: () => import("highlight.js/lib/languages/processing"),
+  profile: () => import("highlight.js/lib/languages/profile"),
+  prolog: () => import("highlight.js/lib/languages/prolog"),
+  properties: () => import("highlight.js/lib/languages/properties"),
+  protobuf: () => import("highlight.js/lib/languages/protobuf"),
+  puppet: () => import("highlight.js/lib/languages/puppet"),
+  purebasic: () => import("highlight.js/lib/languages/purebasic"),
+  python: () => import("highlight.js/lib/languages/python"),
+  "python-repl": () => import("highlight.js/lib/languages/python-repl"),
+  q: () => import("highlight.js/lib/languages/q"),
+  qml: () => import("highlight.js/lib/languages/qml"),
+  r: () => import("highlight.js/lib/languages/r"),
+  reasonml: () => import("highlight.js/lib/languages/reasonml"),
+  rib: () => import("highlight.js/lib/languages/rib"),
+  roboconf: () => import("highlight.js/lib/languages/roboconf"),
+  routeros: () => import("highlight.js/lib/languages/routeros"),
+  rsl: () => import("highlight.js/lib/languages/rsl"),
+  ruby: () => import("highlight.js/lib/languages/ruby"),
+  ruleslanguage: () => import("highlight.js/lib/languages/ruleslanguage"),
+  rust: () => import("highlight.js/lib/languages/rust"),
+  sas: () => import("highlight.js/lib/languages/sas"),
+  scala: () => import("highlight.js/lib/languages/scala"),
+  scheme: () => import("highlight.js/lib/languages/scheme"),
+  scilab: () => import("highlight.js/lib/languages/scilab"),
+  scss: () => import("highlight.js/lib/languages/scss"),
+  shell: () => import("highlight.js/lib/languages/shell"),
+  smali: () => import("highlight.js/lib/languages/smali"),
+  smalltalk: () => import("highlight.js/lib/languages/smalltalk"),
+  sml: () => import("highlight.js/lib/languages/sml"),
+  sqf: () => import("highlight.js/lib/languages/sqf"),
+  sql: () => import("highlight.js/lib/languages/sql"),
+  stan: () => import("highlight.js/lib/languages/stan"),
+  stata: () => import("highlight.js/lib/languages/stata"),
+  step21: () => import("highlight.js/lib/languages/step21"),
+  stylus: () => import("highlight.js/lib/languages/stylus"),
+  subunit: () => import("highlight.js/lib/languages/subunit"),
+  swift: () => import("highlight.js/lib/languages/swift"),
+  taggerscript: () => import("highlight.js/lib/languages/taggerscript"),
+  tap: () => import("highlight.js/lib/languages/tap"),
+  tcl: () => import("highlight.js/lib/languages/tcl"),
+  thrift: () => import("highlight.js/lib/languages/thrift"),
+  tp: () => import("highlight.js/lib/languages/tp"),
+  twig: () => import("highlight.js/lib/languages/twig"),
+  typescript: () => import("highlight.js/lib/languages/typescript"),
+  vala: () => import("highlight.js/lib/languages/vala"),
+  vbnet: () => import("highlight.js/lib/languages/vbnet"),
+  vbscript: () => import("highlight.js/lib/languages/vbscript"),
+  "vbscript-html": () => import("highlight.js/lib/languages/vbscript-html"),
+  verilog: () => import("highlight.js/lib/languages/verilog"),
+  vhdl: () => import("highlight.js/lib/languages/vhdl"),
+  vim: () => import("highlight.js/lib/languages/vim"),
+  wasm: () => import("highlight.js/lib/languages/wasm"),
+  wren: () => import("highlight.js/lib/languages/wren"),
+  x86asm: () => import("highlight.js/lib/languages/x86asm"),
+  xl: () => import("highlight.js/lib/languages/xl"),
+  xml: () => import("highlight.js/lib/languages/xml"),
+  xquery: () => import("highlight.js/lib/languages/xquery"),
+  yaml: () => import("highlight.js/lib/languages/yaml"),
+  zephir: () => import("highlight.js/lib/languages/zephir"),
+};
+
+/** The language each alias names (`js` -> `javascript`), as highlight.js resolves it. */
+export const HIGHLIGHT_LANGUAGE_ALIASES: Record<string, string> = {
+  ado: "stata",
+  adoc: "asciidoc",
+  ahk: "autohotkey",
+  apacheconf: "apache",
+  arm: "armasm",
+  as: "actionscript",
+  asc: "angelscript",
+  atom: "xml",
+  bat: "dos",
+  bf: "brainfuck",
+  bind: "dns",
+  "c#": "csharp",
+  "c++": "cpp",
+  capnp: "capnproto",
+  cc: "cpp",
+  cjs: "javascript",
+  clj: "clojure",
+  cls: "cos",
+  "cmake.in": "cmake",
+  cmd: "dos",
+  coffee: "coffeescript",
+  console: "shell",
+  cr: "crystal",
+  craftcms: "twig",
+  crm: "crmsh",
+  cs: "csharp",
+  cson: "coffeescript",
+  cts: "typescript",
+  cxx: "cpp",
+  dcl: "clean",
+  dfm: "delphi",
+  do: "stata",
+  docker: "dockerfile",
+  dpr: "delphi",
+  dst: "dust",
+  edn: "clojure",
+  erl: "erlang",
+  ex: "elixir",
+  exs: "elixir",
+  "f#": "fsharp",
+  f90: "fortran",
+  f95: "fortran",
+  feature: "gherkin",
+  fs: "fsharp",
+  gemspec: "ruby",
+  gms: "gams",
+  golang: "go",
+  gql: "graphql",
+  graph: "roboconf",
+  gss: "gauss",
+  gyp: "python",
+  h: "c",
+  "h++": "cpp",
+  hbs: "handlebars",
+  hh: "cpp",
+  hpp: "cpp",
+  hs: "haskell",
+  html: "xml",
+  "html.handlebars": "handlebars",
+  "html.hbs": "handlebars",
+  htmlbars: "handlebars",
+  https: "http",
+  hx: "haxe",
+  hxx: "cpp",
+  hylang: "hy",
+  i7: "inform7",
+  iced: "coffeescript",
+  icl: "clean",
+  ino: "arduino",
+  instances: "roboconf",
+  ipython: "python",
+  irb: "ruby",
+  jinja: "django",
+  jldoctest: "julia-repl",
+  js: "javascript",
+  jsonc: "json",
+  jsp: "java",
+  jsx: "javascript",
+  k: "q",
+  kdb: "q",
+  kt: "kotlin",
+  kts: "kotlin",
+  lassoscript: "lasso",
+  ls: "livescript",
+  m: "mercury",
+  mak: "makefile",
+  make: "makefile",
+  md: "markdown",
+  mikrotik: "routeros",
+  mips: "mipsasm",
+  mjs: "javascript",
+  mk: "makefile",
+  mkd: "markdown",
+  mkdown: "markdown",
+  ml: "sml",
+  mm: "objectivec",
+  mma: "mathematica",
+  moo: "mercury",
+  moon: "moonscript",
+  mts: "typescript",
+  nc: "gcode",
+  nginxconf: "nginx",
+  nixos: "nix",
+  nt: "nestedtext",
+  "obj-c": "objectivec",
+  "obj-c++": "objectivec",
+  objc: "objectivec",
+  "objective-c++": "objectivec",
+  osascript: "applescript",
+  p21: "step21",
+  pas: "delphi",
+  pascal: "delphi",
+  patch: "diff",
+  pb: "purebasic",
+  pbi: "purebasic",
+  pcmk: "crmsh",
+  pde: "processing",
+  "pf.conf": "pf",
+  pl: "perl",
+  plist: "xml",
+  pluto: "lua",
+  pm: "perl",
+  podspec: "ruby",
+  postgres: "pgsql",
+  postgresql: "pgsql",
+  pp: "puppet",
+  proto: "protobuf",
+  ps: "powershell",
+  ps1: "powershell",
+  pwsh: "powershell",
+  py: "python",
+  pycon: "python-repl",
+  qt: "qml",
+  rb: "ruby",
+  re: "reasonml",
+  rs: "rust",
+  rss: "xml",
+  scad: "openscad",
+  sci: "scilab",
+  scm: "scheme",
+  sh: "bash",
+  shellsession: "shell",
+  st: "smalltalk",
+  stanfuncs: "stan",
+  step: "step21",
+  stp: "step21",
+  styl: "stylus",
+  sv: "verilog",
+  svg: "xml",
+  svh: "verilog",
+  tao: "xl",
+  tex: "latex",
+  text: "plaintext",
+  thor: "ruby",
+  tk: "tcl",
+  toml: "ini",
+  ts: "typescript",
+  tsx: "typescript",
+  txt: "plaintext",
+  v: "verilog",
+  vb: "vbnet",
+  vbs: "vbscript",
+  "wildfly-cli": "jboss-cli",
+  wl: "mathematica",
+  wsf: "xml",
+  "x++": "axapta",
+  xhtml: "xml",
+  xjb: "xml",
+  xls: "excel",
+  xlsx: "excel",
+  xpath: "xquery",
+  xq: "xquery",
+  xqm: "xquery",
+  xsd: "xml",
+  xsl: "xml",
+  yml: "yaml",
+  zep: "zephir",
+  zone: "dns",
+  zsh: "bash",
+};
+
+/** The languages each language embeds, to load with it so its embedded code is highlighted too. */
+export const HIGHLIGHT_SUB_LANGUAGES: Record<string, readonly string[]> = {
+  asciidoc: ["xml"],
+  "clojure-repl": ["clojure"],
+  coffeescript: ["javascript"],
+  cos: ["javascript", "sql", "xml"],
+  dart: ["markdown"],
+  django: ["xml"],
+  dockerfile: ["bash"],
+  dust: ["xml"],
+  erb: ["ruby", "xml"],
+  haml: ["ruby"],
+  handlebars: ["xml"],
+  javascript: ["css", "graphql", "xml"],
+  "julia-repl": ["julia"],
+  livescript: ["javascript"],
+  markdown: ["xml"],
+  mojolicious: ["perl", "xml"],
+  nix: ["markdown"],
+  "node-repl": ["javascript"],
+  parser3: ["xml"],
+  perl: ["mojolicious"],
+  pgsql: [
+    "bash",
+    "java",
+    "json",
+    "lua",
+    "perl",
+    "php",
+    "python",
+    "r",
+    "ruby",
+    "scheme",
+    "tcl",
+    "xml",
+  ],
+  "php-template": ["php", "xml"],
+  "python-repl": ["python"],
+  qml: ["xml"],
+  shell: ["bash"],
+  tap: ["yaml"],
+  twig: ["xml"],
+  typescript: ["css", "graphql", "xml"],
+  "vbscript-html": ["vbscript", "xml"],
+  xml: ["css", "handlebars", "javascript"],
+  xquery: ["xml"],
+  yaml: ["ruby"],
+};
+
+/** lowlight's common languages: the ones auto-detection picks from. */
+export const HIGHLIGHT_AUTO_DETECT_LANGUAGES: readonly string[] = [
+  "arduino",
+  "bash",
+  "c",
+  "cpp",
+  "csharp",
+  "css",
+  "diff",
+  "go",
+  "graphql",
+  "ini",
+  "java",
+  "javascript",
+  "json",
+  "kotlin",
+  "less",
+  "lua",
+  "makefile",
+  "markdown",
+  "objectivec",
+  "perl",
+  "php",
+  "php-template",
+  "plaintext",
+  "python",
+  "python-repl",
+  "r",
+  "ruby",
+  "rust",
+  "scss",
+  "shell",
+  "sql",
+  "swift",
+  "typescript",
+  "vbnet",
+  "wasm",
+  "xml",
+  "yaml",
+];
diff --git a/ui/lib/ui/src/lib/utils/index.ts b/ui/lib/ui/src/lib/utils/index.ts
index 3e3c1fdef..3eb11e230 100644
--- a/ui/lib/ui/src/lib/utils/index.ts
+++ b/ui/lib/ui/src/lib/utils/index.ts
@@ -1 +1,8 @@
-export { highlightCode, trimCodePadding } from './code';
+export {
+  getLoadedHighlightLanguages,
+  highlightCode,
+  isHighlightLanguageLoaded,
+  loadHighlightLanguages,
+  resolveHighlightLanguage,
+  trimCodePadding,
+} from "./code";