Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Skip the inline-code mask when every backtick is inside a fence (#9991)

Daniel Hanecea20a6bec4
diff --git a/studio/frontend/src/lib/latex.ts b/studio/frontend/src/lib/latex.ts
index 6b4fd7e3838..b0c55470284 100644
--- a/studio/frontend/src/lib/latex.ts
+++ b/studio/frontend/src/lib/latex.ts
@@ -124,6 +124,27 @@ function findInlineCodeRegions(
content: string,
blockRegions: Array<[number, number]>,
): Array<[number, number]> {
+ // Every backtick inside a block region becomes a space in the mask below, so
+ // unless one sits outside a block region the mask cannot contain a backtick,
+ // `codeSpans` returns nothing and the whole rebuild is spent to learn that.
+ // Worth checking first because the streaming path calls this once per frame
+ // over a growing prefix, twice per preprocessLaTeX, so the rebuild is O(n) per
+ // frame and O(n^2) over a reply; and a reply carrying one fenced example and
+ // no inline code, where every backtick is inside the fence, is the common
+ // shape rather than a corner case.
+ let spanTickOutsideBlock = false;
+ for (
+ let index = content.indexOf("`");
+ index !== -1;
+ index = content.indexOf("`", index + 1)
+ ) {
+ if (!isInRegion(index, blockRegions)) {
+ spanTickOutsideBlock = true;
+ break;
+ }
+ }
+ if (!spanTickOutsideBlock) return [];
+
let masked = content;
if (blockRegions.length > 0) {
const parts: string[] = [];
diff --git a/studio/frontend/tests/latex-code-regions.test.ts b/studio/frontend/tests/latex-code-regions.test.ts
index 048429b017e..41e9110ba2d 100644
--- a/studio/frontend/tests/latex-code-regions.test.ts
+++ b/studio/frontend/tests/latex-code-regions.test.ts
@@ -135,3 +135,36 @@ test("same-line nested items set the continuation content column", () => {
"- - item\n\n $x$",
);
});
+
+test("a fence's backticks do not stand in for an inline span", () => {
+ // findInlineCodeRegions returns early when no backtick sits outside a block
+ // region, because the mask it would build turns every backtick inside one
+ // into a space and can only yield no spans. The early return has to agree
+ // with the scan it replaces in both directions, so pin both.
+ const fence = "```python\ndef step(lr):\n return lr\n```";
+
+ // All backticks inside the fence: nothing is an inline span, so currency in
+ // prose is still escaped and currency inside the fence is still left alone.
+ assert.equal(
+ preprocessLaTeX(`${fence}\n\ncosts $5 a run\n`),
+ `${fence}\n\ncosts \\$5 a run\n`,
+ );
+ assert.equal(
+ preprocessLaTeX("```sh\nrun --seed $1\n```\n"),
+ "```sh\nrun --seed $1\n```\n",
+ );
+
+ // One backtick outside the fence, so the scan still has to run: the inline
+ // span protects its own currency while prose currency is escaped.
+ assert.equal(
+ preprocessLaTeX(`${fence}\n\n\`cost $5\` and $6 in prose\n`),
+ `${fence}\n\n\`cost $5\` and \\$6 in prose\n`,
+ );
+
+ // An inline span before the fence, which the region scan reaches only after
+ // the fence has been masked out.
+ assert.equal(
+ preprocessLaTeX(`\`keep $7\` then\n\n${fence}\n\nand $8\n`),
+ `\`keep $7\` then\n\n${fence}\n\nand \\$8\n`,
+ );
+});