Skip to main content

Mantle Compact Code Markup Migration Guide

@ngrok/mantle's highlighted code markup is smaller. A token has a short color class, such as tk for a keyword, in place of an inline style="color:var(--shiki-token-…)", and a token in the foreground color has no element. mantle.css draws the line number, the fold caret, and the fold ellipsis as pseudo-elements. The elements .mantle-code-line-number, .mantle-code-fold-caret, and .mantle-code-fold-ellipsis are gone, and so are data-slot="line-number", "line-content", "fold-toggle", and "fold-ellipsis". Lines are separated by a newline, so the code's textContent is the code. An empty line holds one space, as before. A call site that renders CodeBlock with mantleCode(), the Vite plugin, or useCodeBlockStream() changes nothing. A call site that passes server-highlighted HTML to createMantleCodeBlockValue also passes the result's showLineNumbers and lineNumberStart (step 6).

TL;DR: Search for the removed class names, data-slot values, and --shiki-token- inside a style= string. Restyle a removed element through its line, .mantle-code-line::before for the number. Delete a step that strips the line number or the ellipsis from the markup. Upgrade @ngrok/mantle, @ngrok/mantle-server-syntax-highlighter, and @ngrok/mantle-vite-plugins together. Render highlighted HTML inside CodeBlock.Code, or give your own <pre> the attributes in step 6.

Codemod

Codemod: manual: every step is a CSS selector, a regular expression over an HTML string, or a test query.


You are an agent migrating a codebase. Do this:

  1. Upgrade @ngrok/mantle, @ngrok/mantle-server-syntax-highlighter, and @ngrok/mantle-vite-plugins in one change. A new highlighter writes token classes that an older mantle.css does not color, and a newer mantle.css draws a second line number next to the number element of an older highlighter. Build again any highlighted HTML that you stored or cached.
  2. Search CSS and class strings for .mantle-code-line-number, .mantle-code-fold-caret, and .mantle-code-fold-ellipsis. Move a rule on the number to pre[data-mantle-line-numbers="true"] .mantle-code-line::before, a rule on the caret to .mantle-code-fold-toggle::after, and a rule on the ellipsis to .mantle-code-line-opener > .mantle-code-line-content::after. Keep a rule that sets --mantle-code-line-number-width or another --mantle-code-* variable; the variables do not change.
  3. Search for [data-slot="line-number"], [data-slot="line-content"], [data-slot="fold-toggle"], and [data-slot="fold-ellipsis"]. Replace line-content with .mantle-code-line-content and fold-toggle with .mantle-code-fold-toggle. Treat the other two as in step 2.
  4. Search for code that reads or rewrites highlighted HTML as a string, such as a function that strips fold markup. Delete a step that removes <span class="mantle-code-fold-ellipsis"> or the line-number span: the markup no longer has them. A step that removes the fold <button>, data-fold-regions, or mantle-code-line-opener still works. Leave the new style="counter-set:…" on a line that closes a fold.
  5. Search for --shiki-token- in a style= string that code matches. A token now carries a class, so match class="tk" and the other classes in token-classes.ts. A color with no class keeps its inline style.
  6. Where highlighted HTML renders in a <pre> that is not CodeBlock.Code, give the <pre> the attributes that mantle.css reads: data-slot="code-block-code" and data-syntax-highlighted color the tokens and stack the lines, and data-lang keeps plain text in the inherited color. Set data-mantle-line-numbers="true" to show line numbers, and --mantle-line-number-start to start at a number other than 1. When you render the result of @ngrok/mantle-server-syntax-highlighter in CodeBlock, pass its showLineNumbers and lineNumberStart to createMantleCodeBlockValue: the HTML holds no numbers. Without these attributes the tokens have no color and the newline between lines adds an empty line. decorateHighlightedHtml's showLineNumbers has no effect now.
  7. In tests:
    • A query for the number element goes away. container.querySelector("[data-slot='line-number']") becomes a check of data-line-number on the line: expect(line).toHaveAttribute("data-line-number", "2").
    • A test that read each line's text reads the whole code. lines.map((line) => line.textContent) over [data-slot='line-content'] becomes expect(code.textContent).toBe("a\nb").
    • A golden HTML fixture of highlighted code changes. Generate it again from the highlighter.
    • A fold animates. A line that opens has data-fold-hidden="false" for 200ms, then no attribute. Right after a click, expect(line).not.toHaveAttribute("data-fold-hidden") becomes expect(line).not.toHaveAttribute("data-fold-hidden", "true"), or wait for the attribute to go. A line that closes is inert at once.
  8. Run the project's formatter, linter, type checker, and tests.

Why CSS for the chrome? A highlighted block repeated an inline color on every token and a number element on every line. A 29-block answer held 123 KB of code markup and 1,641 elements in its code. With classes and pseudo-elements it holds 52 KB and 1,046 elements, and copy, find in page, screen readers, and crawlers read only the code.


Before → after

A line of highlighted code

<!-- BEFORE -->
<span class="mantle-code-line" data-line-number="2"
	><span class="mantle-code-line-number" data-slot="line-number" aria-hidden="true">2</span
	><span class="mantle-code-line-content" data-slot="line-content"
		><span style="color:var(--shiki-token-keyword)">import</span
		><span style="color:var(--shiki-foreground)"> { </span
		><span style="color:var(--shiki-token-variable, var(--shiki-foreground))">x</span></span
	></span
>
<!-- AFTER -->
<span class="mantle-code-line" data-line-number="2"
	><span class="mantle-code-line-content"
		><span class="tk">import</span> { <span class="tv">x</span></span
	></span
>

A rule on the line number

/* BEFORE */
.my-code .mantle-code-line-number {
	color: var(--text-color-muted);
}
/* AFTER */
.my-code pre[data-mantle-line-numbers="true"] .mantle-code-line::before {
	color: var(--text-color-muted);
}

Edge cases & gotchas

  • The line number comes from a CSS counter. A line hidden by a fold is not counted, so the line that closes a fold sets the counter again from --mantle-line-number-start with an inline counter-set. Keep that attribute when you rewrite the markup.
  • The number and the ellipsis have empty alternative text, so a screen reader reads neither. A browser without alternative text in content shows them all the same.

How to verify

Run the project's tests and type checker, then open a page with a numbered code block and a folded JSON block. The numbers, the caret, and the ellipsis show as before, and a copy of the code holds no line numbers. Step 7 lists the test changes.