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:
- Upgrade
@ngrok/mantle,@ngrok/mantle-server-syntax-highlighter, and@ngrok/mantle-vite-pluginsin one change. A new highlighter writes token classes that an oldermantle.cssdoes not color, and a newermantle.cssdraws a second line number next to the number element of an older highlighter. Build again any highlighted HTML that you stored or cached. - 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 topre[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-widthor another--mantle-code-*variable; the variables do not change. - Search for
[data-slot="line-number"],[data-slot="line-content"],[data-slot="fold-toggle"], and[data-slot="fold-ellipsis"]. Replaceline-contentwith.mantle-code-line-contentandfold-togglewith.mantle-code-fold-toggle. Treat the other two as in step 2. - 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, ormantle-code-line-openerstill works. Leave the newstyle="counter-set:…"on a line that closes a fold. - Search for
--shiki-token-in astyle=string that code matches. A token now carries a class, so matchclass="tk"and the other classes intoken-classes.ts. A color with no class keeps its inline style. - Where highlighted HTML renders in a
<pre>that is notCodeBlock.Code, give the<pre>the attributes thatmantle.cssreads:data-slot="code-block-code"anddata-syntax-highlightedcolor the tokens and stack the lines, anddata-langkeeps plain text in the inherited color. Setdata-mantle-line-numbers="true"to show line numbers, and--mantle-line-number-startto start at a number other than 1. When you render the result of@ngrok/mantle-server-syntax-highlighterinCodeBlock, pass itsshowLineNumbersandlineNumberStarttocreateMantleCodeBlockValue: the HTML holds no numbers. Without these attributes the tokens have no color and the newline between lines adds an empty line.decorateHighlightedHtml'sshowLineNumbershas no effect now. - In tests:
- A query for the number element goes away.
container.querySelector("[data-slot='line-number']")becomes a check ofdata-line-numberon 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']becomesexpect(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")becomesexpect(line).not.toHaveAttribute("data-fold-hidden", "true"), or wait for the attribute to go. A line that closes isinertat once.
- A query for the number element goes away.
- 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
A rule on the line number
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-startwith an inlinecounter-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
contentshows 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.