* 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)
193 lines
6.3 KiB
TypeScript
193 lines
6.3 KiB
TypeScript
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', () => {
|
|
expect(trimCodePadding('\nfunction foo() {}')).toBe('function foo() {}');
|
|
});
|
|
|
|
it('removes multiple leading newlines', () => {
|
|
expect(trimCodePadding('\n\n\nfunction foo() {}')).toBe('function foo() {}');
|
|
});
|
|
|
|
it('removes whitespace-only leading lines', () => {
|
|
expect(trimCodePadding('\n \n\t\nfunction foo() {}')).toBe('function foo() {}');
|
|
});
|
|
|
|
it('removes a single trailing newline', () => {
|
|
expect(trimCodePadding('function foo() {}\n')).toBe('function foo() {}');
|
|
});
|
|
|
|
it('removes multiple trailing newlines', () => {
|
|
expect(trimCodePadding('function foo() {}\n\n\n')).toBe('function foo() {}');
|
|
});
|
|
|
|
it('removes whitespace-only trailing lines', () => {
|
|
expect(trimCodePadding('function foo() {}\n \n\t\n')).toBe('function foo() {}');
|
|
});
|
|
|
|
it('removes newlines on both sides at once', () => {
|
|
expect(trimCodePadding('\nfunction foo() {}\n')).toBe('function foo() {}');
|
|
});
|
|
|
|
it('preserves internal blank lines', () => {
|
|
expect(trimCodePadding('\nfunction foo() {\n\n return 1;\n}\n')).toBe(
|
|
'function foo() {\n\n return 1;\n}'
|
|
);
|
|
});
|
|
|
|
it('drops a leading whitespace-only line but keeps following code intact', () => {
|
|
expect(trimCodePadding(' \nfunction foo() {}')).toBe('function foo() {}');
|
|
});
|
|
|
|
it('passes through already-trimmed input unchanged', () => {
|
|
expect(trimCodePadding('function foo() {}')).toBe('function foo() {}');
|
|
expect(trimCodePadding('function foo() {\n return 1;\n}')).toBe(
|
|
'function foo() {\n return 1;\n}'
|
|
);
|
|
});
|
|
|
|
it('returns empty string when input is whitespace only', () => {
|
|
expect(trimCodePadding('\n\n\n')).toBe('');
|
|
expect(trimCodePadding('\n \n\t\n')).toBe('');
|
|
});
|
|
});
|
|
|
|
describe('highlightCode', () => {
|
|
it('returns empty string for empty input', () => {
|
|
expect(highlightCode('', 'javascript')).toBe('');
|
|
});
|
|
|
|
it('does not produce a leading newline in the highlighted html', () => {
|
|
const html = highlightCode('\nfunction multiply(a, b) {\n return a * b;\n}\n', 'javascript');
|
|
|
|
expect(html.startsWith('\n')).toBe(false);
|
|
expect(html.startsWith(' ')).toBe(false);
|
|
});
|
|
|
|
it('does not produce a trailing newline in the highlighted html', () => {
|
|
const html = highlightCode('\nfunction foo() {}\n', 'javascript');
|
|
|
|
expect(html.endsWith('\n')).toBe(false);
|
|
});
|
|
|
|
it('preserves internal blank lines in highlighted code', () => {
|
|
const html = highlightCode('\nfunction foo() {\n\n return 1;\n}\n', 'javascript');
|
|
|
|
expect(html).toContain('\n\n');
|
|
});
|
|
|
|
it('produces the same body for framed and unframed input', () => {
|
|
const trimmed = highlightCode('function foo() {}', 'javascript');
|
|
const framed = highlightCode('\nfunction foo() {}\n', 'javascript');
|
|
|
|
expect(framed).toBe(trimmed);
|
|
});
|
|
|
|
it('auto-detects an unknown language by default', () => {
|
|
const html = highlightCode('const answer = 42;', 'not-a-language');
|
|
|
|
expect(html).toContain('hljs-');
|
|
});
|
|
|
|
it('escapes instead of auto-detecting when autoDetect is false', () => {
|
|
const html = highlightCode('const answer = 42;', 'not-a-language', false);
|
|
|
|
expect(html).not.toContain('hljs-');
|
|
expect(html).toBe('const answer = 42;');
|
|
});
|
|
|
|
it('still highlights a known language when autoDetect is false', () => {
|
|
const html = highlightCode('const answer = 42;', 'javascript', false);
|
|
|
|
expect(html).toContain('hljs-');
|
|
});
|
|
|
|
it('escapes html metacharacters when falling back to plain text', () => {
|
|
const html = highlightCode('<script>a && b</script>', 'not-a-language', false);
|
|
|
|
expect(html).toBe('<script>a && b</script>');
|
|
});
|
|
});
|
|
|
|
describe('splitGluedClosingCodeFences', () => {
|
|
it('splits text glued to a closing fence onto its own line', () => {
|
|
const input = "```ts\nlet foo = 'bar';\n```create this file on [Desktop](file:///a/b/)";
|
|
|
|
expect(splitGluedClosingCodeFences(input)).toBe(
|
|
"```ts\nlet foo = 'bar';\n```\ncreate this file on [Desktop](file:///a/b/)"
|
|
);
|
|
});
|
|
|
|
it('leaves a well-formed code block untouched', () => {
|
|
const input = "```ts\nlet foo = 'bar';\n```\ncreate this file on [Desktop](file:///a/b/)";
|
|
|
|
expect(splitGluedClosingCodeFences(input)).toBe(input);
|
|
});
|
|
|
|
it('leaves content without fences untouched', () => {
|
|
expect(splitGluedClosingCodeFences('hello world')).toBe('hello world');
|
|
});
|
|
|
|
it('keeps nested markdown fences inside a block intact', () => {
|
|
const input = '```md\n# Example\n```python\nprint(1)\n```\n```';
|
|
|
|
expect(splitGluedClosingCodeFences(input)).toBe(input);
|
|
});
|
|
|
|
it('splits every glued closing fence when several blocks are present', () => {
|
|
const input = '```ts\na\n```first words\n\n```js\nb\n```second words';
|
|
|
|
expect(splitGluedClosingCodeFences(input)).toBe(
|
|
'```ts\na\n```\nfirst words\n\n```js\nb\n```\nsecond words'
|
|
);
|
|
});
|
|
|
|
it('leaves a still-open fence untouched', () => {
|
|
const input = '```ts\nlet foo = 1;';
|
|
|
|
expect(splitGluedClosingCodeFences(input)).toBe(input);
|
|
});
|
|
});
|
|
|
|
describe('highlightCode in bash', () => {
|
|
const API_CALL = `curl http://localhost:9931/v1/chat/completions \\
|
|
-H "Content-Type: application/json" \\
|
|
-d '{"model": "x"}'`;
|
|
|
|
it('marks a command name as a built-in', () => {
|
|
expect(highlightCode(API_CALL, 'bash')).toContain('<span class="hljs-built_in">curl</span>');
|
|
});
|
|
|
|
it('marks flags as attributes', () => {
|
|
const html = highlightCode('brew install --cask llama-app -LsSf', 'bash');
|
|
|
|
expect(html).toContain('<span class="hljs-attr">--cask</span>');
|
|
expect(html).toContain('<span class="hljs-attr">-LsSf</span>');
|
|
});
|
|
|
|
it('marks the command after a pipe', () => {
|
|
expect(highlightCode('curl -LsSf https://llama.app/install.sh | sh', 'bash')).toContain(
|
|
'| <span class="hljs-built_in">sh</span>'
|
|
);
|
|
});
|
|
|
|
it("doesn't mark a continued line's first word as a command", () => {
|
|
expect(highlightCode('brew \\\n install x', 'bash')).not.toContain('hljs-built_in">install');
|
|
});
|
|
|
|
it('leaves keywords, and flags inside strings, as they were', () => {
|
|
const html = highlightCode('if true; then\n echo "hi -x"\nfi', 'bash');
|
|
|
|
expect(html).toContain('<span class="hljs-keyword">if</span>');
|
|
expect(html).not.toContain('hljs-attr">-x');
|
|
});
|
|
});
|