Skip to content

Open-source AI browser agent for Chrome and Firefox (monorepo) 🧠

Harden Markdown fence container handling

Emre Sokullua8218c664275
diff --git a/src/chrome/src/ui/history-text.js b/src/chrome/src/ui/history-text.js
index b80a24369..568768ffa 100644
--- a/src/chrome/src/ui/history-text.js
+++ b/src/chrome/src/ui/history-text.js
@@ -132,9 +132,12 @@ export function historyTextFromElement(root, { markdown = true } = {}) {
const code = output;
// History must not reintroduce ambiguous fences around a Markdown
// document (or any code sample containing literal backtick runs).
+ const fenceCharacter = language.includes('`') ? '~' : '`';
let fenceLength = 3;
- for (const match of code.matchAll(/`{3,}/g)) fenceLength = Math.max(fenceLength, match[0].length + 1);
- const fence = '`'.repeat(fenceLength);
+ for (const match of code.matchAll(new RegExp(`${fenceCharacter}{3,}`, 'g'))) {
+ fenceLength = Math.max(fenceLength, match[0].length + 1);
+ }
+ const fence = fenceCharacter.repeat(fenceLength);
output = `${beforeCode}${fence}${language}\n${code}${fence}`;
return;
}
diff --git a/src/chrome/src/ui/markdown-render.js b/src/chrome/src/ui/markdown-render.js
index 5fa95e64f..ecfe1570e 100644
--- a/src/chrome/src/ui/markdown-render.js
+++ b/src/chrome/src/ui/markdown-render.js
@@ -57,10 +57,22 @@ export function codeFenceLanguage(infoString) {
return String(infoString || '').trim().split(/\s+/, 1)[0] || '';
}
+function fenceContainer(prefix, indentation = '') {
+ const source = String(prefix);
+ const quotePrefix = source.match(/^(?: {0,3}>[ \t]?)+/)?.[0] || '';
+ const remainder = source.slice(quotePrefix.length);
+ const listPrefix = remainder.match(/^[ \t]*(?:[-+*]|\d+[.)])[ \t]+/)?.[0] || '';
+ return { quotePrefix, listPrefix, indentation: String(indentation) };
+}
+
+function fenceCloserInContainer(opener, candidate) {
+ if (opener.quotePrefix !== candidate.quotePrefix) return false;
+ if (!opener.listPrefix) return !candidate.listPrefix;
+ return !candidate.listPrefix && candidate.indentation.length >= opener.listPrefix.length;
+}
+
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] || '';
+ const { quotePrefix, listPrefix } = fenceContainer(prefix);
if (!quotePrefix && !listPrefix) return code;
const listIndent = listPrefix.length;
@@ -82,28 +94,30 @@ 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 [, prefix, fence, info] = match;
+ const [, prefix, indentation, fence, info] = match;
+ const container = fenceContainer(prefix, indentation);
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, prefix, start: match.index + match[0].length };
- stack.push({ fence, markdown });
+ block = { info, prefix, container, start: match.index + match[0].length };
+ stack.push({ fence, markdown, container });
continue;
}
const active = stack[stack.length - 1];
// A closing fence occupies its own line, has no info string, and is at
// least as long as its opener. Backticks inside source code are literal.
- if (!info.trim() && fence[0] === active.fence[0] && fence.length >= active.fence.length) {
+ if (fenceCloserInContainer(active.container, container)
+ && !info.trim() && fence[0] === active.fence[0] && fence.length >= active.fence.length) {
stack.pop();
if (!stack.length) {
output.push(block.prefix + renderBlock(
@@ -118,7 +132,7 @@ export function replaceMarkdownCodeFences(value, renderBlock) {
// Models sometimes wrap a README in ```markdown and reuse ```lang
// inside it. Recover only this named Markdown nesting; ordinary code
// and correctly longer outer fences retain their literal contents.
- stack.push({ fence, markdown });
+ stack.push({ fence, markdown, container });
}
}
diff --git a/src/firefox/src/ui/history-text.js b/src/firefox/src/ui/history-text.js
index b80a24369..568768ffa 100644
--- a/src/firefox/src/ui/history-text.js
+++ b/src/firefox/src/ui/history-text.js
@@ -132,9 +132,12 @@ export function historyTextFromElement(root, { markdown = true } = {}) {
const code = output;
// History must not reintroduce ambiguous fences around a Markdown
// document (or any code sample containing literal backtick runs).
+ const fenceCharacter = language.includes('`') ? '~' : '`';
let fenceLength = 3;
- for (const match of code.matchAll(/`{3,}/g)) fenceLength = Math.max(fenceLength, match[0].length + 1);
- const fence = '`'.repeat(fenceLength);
+ for (const match of code.matchAll(new RegExp(`${fenceCharacter}{3,}`, 'g'))) {
+ fenceLength = Math.max(fenceLength, match[0].length + 1);
+ }
+ const fence = fenceCharacter.repeat(fenceLength);
output = `${beforeCode}${fence}${language}\n${code}${fence}`;
return;
}
diff --git a/src/firefox/src/ui/markdown-render.js b/src/firefox/src/ui/markdown-render.js
index 5fa95e64f..ecfe1570e 100644
--- a/src/firefox/src/ui/markdown-render.js
+++ b/src/firefox/src/ui/markdown-render.js
@@ -57,10 +57,22 @@ export function codeFenceLanguage(infoString) {
return String(infoString || '').trim().split(/\s+/, 1)[0] || '';
}
+function fenceContainer(prefix, indentation = '') {
+ const source = String(prefix);
+ const quotePrefix = source.match(/^(?: {0,3}>[ \t]?)+/)?.[0] || '';
+ const remainder = source.slice(quotePrefix.length);
+ const listPrefix = remainder.match(/^[ \t]*(?:[-+*]|\d+[.)])[ \t]+/)?.[0] || '';
+ return { quotePrefix, listPrefix, indentation: String(indentation) };
+}
+
+function fenceCloserInContainer(opener, candidate) {
+ if (opener.quotePrefix !== candidate.quotePrefix) return false;
+ if (!opener.listPrefix) return !candidate.listPrefix;
+ return !candidate.listPrefix && candidate.indentation.length >= opener.listPrefix.length;
+}
+
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] || '';
+ const { quotePrefix, listPrefix } = fenceContainer(prefix);
if (!quotePrefix && !listPrefix) return code;
const listIndent = listPrefix.length;
@@ -82,28 +94,30 @@ 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 [, prefix, fence, info] = match;
+ const [, prefix, indentation, fence, info] = match;
+ const container = fenceContainer(prefix, indentation);
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, prefix, start: match.index + match[0].length };
- stack.push({ fence, markdown });
+ block = { info, prefix, container, start: match.index + match[0].length };
+ stack.push({ fence, markdown, container });
continue;
}
const active = stack[stack.length - 1];
// A closing fence occupies its own line, has no info string, and is at
// least as long as its opener. Backticks inside source code are literal.
- if (!info.trim() && fence[0] === active.fence[0] && fence.length >= active.fence.length) {
+ if (fenceCloserInContainer(active.container, container)
+ && !info.trim() && fence[0] === active.fence[0] && fence.length >= active.fence.length) {
stack.pop();
if (!stack.length) {
output.push(block.prefix + renderBlock(
@@ -118,7 +132,7 @@ export function replaceMarkdownCodeFences(value, renderBlock) {
// Models sometimes wrap a README in ```markdown and reuse ```lang
// inside it. Recover only this named Markdown nesting; ordinary code
// and correctly longer outer fences retain their literal contents.
- stack.push({ fence, markdown });
+ stack.push({ fence, markdown, container });
}
}
diff --git a/test/markdown-render.mjs b/test/markdown-render.mjs
index 012dc126a..b4c2a7309 100644
--- a/test/markdown-render.mjs
+++ b/test/markdown-render.mjs
@@ -72,6 +72,18 @@ for (const build of ['chrome', 'firefox']) {
}
});
+ test(`${build}: container-prefixed markers do not close an outer fenced block`, () => {
+ const source = '~~~markdown\n> ~~~\n- ~~~\n> ```text\n~~~\n## Outside';
+ const blocks = [];
+ const remaining = helpers.replaceMarkdownCodeFences(source, (info, code) => {
+ blocks.push({ info, code });
+ return 'BLOCK';
+ });
+ assert.deepEqual(blocks, [{ info: 'markdown', code: '> ~~~\n- ~~~\n> ```text\n' }]);
+ assert.equal(remaining, 'BLOCK\n## Outside');
+ assert.match(formatMarkdown(source), /<h2>Outside<\/h2>/);
+ });
+
test(`${build}: streamed open fences keep headings, HTML and nested examples literal`, () => {
for (const content of [readme, '## Heading\n<img src=x onerror=alert(1)>\n', '']) {
const source = `\`\`\`markdown\n${content}`;
@@ -152,6 +164,19 @@ for (const build of ['chrome', 'firefox']) {
}
});
+ test(`${build}: history uses tilde fences for language labels containing backticks`, () => {
+ const text = value => ({ nodeType: 3, nodeValue: value });
+ const element = (tagName, ...childNodes) => ({ nodeType: 1, tagName, childNodes });
+ const code = '~~~\nconst value = true;\n';
+ const pre = element('PRE', element('CODE', text(code)));
+ pre.parentElement = { querySelector: () => ({ textContent: '`javascript`' }) };
+ const saved = historyTextFromElement(element('DIV', pre));
+ assert.match(saved, /^~~~~`javascript`\n~~~\nconst value = true;\n~~~~$/);
+ const source = `${saved}\n## After`;
+ assert.deepEqual(preContents(formatMarkdown(source)), [helpers.escapeCodeHtml(code)]);
+ assert.match(formatMarkdown(source), /<h2>After<\/h2>/);
+ });
+
// Opt-in native DOM checks: WEBBRAIN_MARKDOWN_DOM=1 npm run test:markdown.
if (process.env.WEBBRAIN_MARKDOWN_DOM === '1') test(`${build}: browser render, Copy and history round trip`, async () => {
const { chromium, firefox } = await import('playwright');