Open-source AI browser agent for Chrome and Firefox (monorepo) ðŸ§
JavaScript ai-agentai-agentsai-toolsbrowser-aibrowser-automationbrowser-extensionchrome-aichrome-devtoolschrome-extensionchrome-extensionsfirefox-addonfirefox-addonsfirefox-extensionfirefox-extensionsgemmagemma4minimaxoffline-aioffline-llmqwen
Preserve Markdown fence containers
Emre Sokullu24c8e360b3b8
diff --git a/src/chrome/src/ui/markdown-render.js b/src/chrome/src/ui/markdown-render.jsindex f1d55e26f..5fa95e64f 100644--- a/src/chrome/src/ui/markdown-render.js+++ b/src/chrome/src/ui/markdown-render.js@@ -57,6 +57,24 @@ export function codeFenceLanguage(infoString) {return String(infoString || '').trim().split(/\s+/, 1)[0] || '';}+function normalizeContainerCode(code, prefix) {+ const quotePrefix = String(prefix).match(/^(?: {0,3}>[ \t]?)+/)?.[0] || '';+ const listPrefix = String(prefix).slice(quotePrefix.length)+ .match(/^[ \t]*(?:[-+*]|\d+[.)])[ \t]+$/)?.[0] || '';+ if (!quotePrefix && !listPrefix) return code;++ const listIndent = listPrefix.length;+ const lines = String(code).split(/(\r?\n)/);+ for (let index = 0; index < lines.length; index += 2) {+ let line = lines[index];+ if (quotePrefix && line.startsWith(quotePrefix)) line = line.slice(quotePrefix.length);+ let consumed = 0;+ while (consumed < listIndent && /^[ \t]$/.test(line[consumed] || '')) consumed += 1;+ lines[index] = line.slice(consumed);+ }+ return lines.join('');+}+/** Replace whole fenced blocks, including an unfinished block during streaming. */export function replaceMarkdownCodeFences(value, renderBlock) {const source = String(value ?? '');@@ -64,20 +82,20 @@ export function replaceMarkdownCodeFences(value, renderBlock) {// renderer preserves (lists and blockquotes). The old unanchored matcher// accepted these forms, while a root-only matcher mistakes their closer for// a new opener and consumes the rest of the message as code.- const fenceLines = /^(?:(?: {0,3}>[ \t]?)*(?:(?:[ \t]*(?:[-+*]|\d+[.)])[ \t]+)?)[ \t]{0,3})(`{3,}|~{3,})([^\r\n]*)(?:\r?\n|$)/gm;+ const fenceLines = /^((?: {0,3}>[ \t]?)*(?:[ \t]*(?:[-+*]|\d+[.)])[ \t]+)?)[ \t]{0,3}(`{3,}|~{3,})([^\r\n]*)(?:\r?\n|$)/gm;const output = [];let cursor = 0;let block = null;const stack = [];for (const match of source.matchAll(fenceLines)) {- const [, fence, info] = match;+ const [, prefix, fence, info] = match;const validOpening = fence[0] !== '`' || !info.includes('`');const markdown = /^(?:md|markdown)$/i.test(codeFenceLanguage(info));if (!block) {if (!validOpening) continue;output.push(source.slice(cursor, match.index));- block = { info, start: match.index + match[0].length };+ block = { info, prefix, start: match.index + match[0].length };stack.push({ fence, markdown });continue;}@@ -88,7 +106,10 @@ export function replaceMarkdownCodeFences(value, renderBlock) {if (!info.trim() && fence[0] === active.fence[0] && fence.length >= active.fence.length) {stack.pop();if (!stack.length) {- output.push(renderBlock(block.info, source.slice(block.start, match.index)));+ output.push(block.prefix + renderBlock(+ block.info,+ normalizeContainerCode(source.slice(block.start, match.index), block.prefix),+ ));// Leave the closing line's newline for the surrounding Markdown.cursor = match.index + match[0].replace(/\r?\n$/, '').length;block = null;@@ -101,7 +122,10 @@ export function replaceMarkdownCodeFences(value, renderBlock) {}}- if (block) output.push(renderBlock(block.info, source.slice(block.start)));+ if (block) output.push(block.prefix + renderBlock(+ block.info,+ normalizeContainerCode(source.slice(block.start), block.prefix),+ ));else output.push(source.slice(cursor));return output.join('');}diff --git a/src/firefox/src/ui/markdown-render.js b/src/firefox/src/ui/markdown-render.jsindex f1d55e26f..5fa95e64f 100644--- a/src/firefox/src/ui/markdown-render.js+++ b/src/firefox/src/ui/markdown-render.js@@ -57,6 +57,24 @@ export function codeFenceLanguage(infoString) {return String(infoString || '').trim().split(/\s+/, 1)[0] || '';}+function normalizeContainerCode(code, prefix) {+ const quotePrefix = String(prefix).match(/^(?: {0,3}>[ \t]?)+/)?.[0] || '';+ const listPrefix = String(prefix).slice(quotePrefix.length)+ .match(/^[ \t]*(?:[-+*]|\d+[.)])[ \t]+$/)?.[0] || '';+ if (!quotePrefix && !listPrefix) return code;++ const listIndent = listPrefix.length;+ const lines = String(code).split(/(\r?\n)/);+ for (let index = 0; index < lines.length; index += 2) {+ let line = lines[index];+ if (quotePrefix && line.startsWith(quotePrefix)) line = line.slice(quotePrefix.length);+ let consumed = 0;+ while (consumed < listIndent && /^[ \t]$/.test(line[consumed] || '')) consumed += 1;+ lines[index] = line.slice(consumed);+ }+ return lines.join('');+}+/** Replace whole fenced blocks, including an unfinished block during streaming. */export function replaceMarkdownCodeFences(value, renderBlock) {const source = String(value ?? '');@@ -64,20 +82,20 @@ export function replaceMarkdownCodeFences(value, renderBlock) {// renderer preserves (lists and blockquotes). The old unanchored matcher// accepted these forms, while a root-only matcher mistakes their closer for// a new opener and consumes the rest of the message as code.- const fenceLines = /^(?:(?: {0,3}>[ \t]?)*(?:(?:[ \t]*(?:[-+*]|\d+[.)])[ \t]+)?)[ \t]{0,3})(`{3,}|~{3,})([^\r\n]*)(?:\r?\n|$)/gm;+ const fenceLines = /^((?: {0,3}>[ \t]?)*(?:[ \t]*(?:[-+*]|\d+[.)])[ \t]+)?)[ \t]{0,3}(`{3,}|~{3,})([^\r\n]*)(?:\r?\n|$)/gm;const output = [];let cursor = 0;let block = null;const stack = [];for (const match of source.matchAll(fenceLines)) {- const [, fence, info] = match;+ const [, prefix, fence, info] = match;const validOpening = fence[0] !== '`' || !info.includes('`');const markdown = /^(?:md|markdown)$/i.test(codeFenceLanguage(info));if (!block) {if (!validOpening) continue;output.push(source.slice(cursor, match.index));- block = { info, start: match.index + match[0].length };+ block = { info, prefix, start: match.index + match[0].length };stack.push({ fence, markdown });continue;}@@ -88,7 +106,10 @@ export function replaceMarkdownCodeFences(value, renderBlock) {if (!info.trim() && fence[0] === active.fence[0] && fence.length >= active.fence.length) {stack.pop();if (!stack.length) {- output.push(renderBlock(block.info, source.slice(block.start, match.index)));+ output.push(block.prefix + renderBlock(+ block.info,+ normalizeContainerCode(source.slice(block.start, match.index), block.prefix),+ ));// Leave the closing line's newline for the surrounding Markdown.cursor = match.index + match[0].replace(/\r?\n$/, '').length;block = null;@@ -101,7 +122,10 @@ export function replaceMarkdownCodeFences(value, renderBlock) {}}- if (block) output.push(renderBlock(block.info, source.slice(block.start)));+ if (block) output.push(block.prefix + renderBlock(+ block.info,+ normalizeContainerCode(source.slice(block.start), block.prefix),+ ));else output.push(source.slice(cursor));return output.join('');}diff --git a/test/markdown-render.mjs b/test/markdown-render.mjsindex e677dcb28..012dc126a 100644--- a/test/markdown-render.mjs+++ b/test/markdown-render.mjs@@ -80,6 +80,12 @@ for (const build of ['chrome', 'firefox']) {assert.deepEqual(preContents(renderSkillMarkdown(source)), [escapeHtml(content)]);assert.doesNotMatch(html, /<h[1-6]>|<img |code-copy-btn/);}+ const quoted = '> ```text\n> hello\n';+ assert.match(+ renderSkillMarkdown(quoted),+ /<blockquote><pre><code>hello\n<\/code><\/pre><\/blockquote>/,+ 'an unfinished quoted fence should retain its container and strip its quote marker',+ );const start = '```markdown\n';for (let length = start.length; length <= start.length + readme.length; length += 1) {const prefix = (start + readme).slice(0, length);@@ -104,25 +110,31 @@ for (const build of ['chrome', 'firefox']) {const cases = [['numbered list',- '1. ```python\nvalue = "**literal**"\n```\n\n2. Start the server.',+ '1. ```python\n value = "**literal**"\n ```\n\n2. Start the server.',/2\. Start the server\./,+ /<ol><li><pre><code>value = "\*\*literal\*\*"\n<\/code><\/pre><\/li><\/ol><br><br><ol><li>Start the server\.<\/li><\/ol>/,],['list marker','- ```python\n value = "**literal**"\n ```\n\n## Next steps\nCheck the result.',/<h2>Next steps<\/h2>Check the result\./,+ /<ul><li><pre><code>value = "\*\*literal\*\*"\n<\/code><\/pre><\/li><\/ul>/,],['blockquote',- '> ```text\n> **literal**\n> ```\n\n## Next steps\nCheck the result.',+ '> ```text\n> hello\n> ```\n\n## Next steps\nCheck the result.',/<h2>Next steps<\/h2>Check the result\./,+ /<blockquote><pre><code>hello\n<\/code><\/pre><\/blockquote>/,],];- for (const [label, source, followingProse] of cases) {+ for (const [label, source, followingProse, expectedContainer] of cases) {const html = formatMarkdown(source);assert.equal(preContents(html).length, 1, `${label}: fence did not render as one code block`);assert.doesNotMatch(html, /<strong>literal<\/strong>/, `${label}: code formatting leaked into prose`);assert.match(html, followingProse, `${label}: closing fence consumed following prose`);+ const historyHtml = renderSkillMarkdown(source);+ assert.match(historyHtml, expectedContainer, `${label}: code block lost its Markdown container`);+ assert.doesNotMatch(historyHtml, /> hello/, `${label}: blockquote marker leaked into code`);}});