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
Keep unmatched nested fences from consuming prose
Emre Sokullu4f3ff946ff80
diff --git a/src/chrome/src/ui/markdown-render.js b/src/chrome/src/ui/markdown-render.jsindex 50deaa6f9..7f4739078 100644--- a/src/chrome/src/ui/markdown-render.js+++ b/src/chrome/src/ui/markdown-render.js@@ -102,6 +102,25 @@ function fenceCloserInContainer(opener, candidate) {&& candidateIndent <= listIndent + 3;}+function isFenceCloser(opener, candidate, fence, info) {+ return fenceCloserInContainer(opener.container, candidate)+ && !info.trim()+ && fence[0] === opener.fence[0]+ && fence.length >= opener.fence.length;+}++function nestedFenceHasOwnCloser(matches, startIndex, active) {+ let closers = 0;+ for (let index = startIndex + 1; index < matches.length; index += 1) {+ const [, prefix, indentation, fence, info] = matches[index];+ if (isFenceCloser(active, fenceContainer(prefix, indentation), fence, info)) {+ closers += 1;+ if (closers === 2) return true;+ }+ }+ return false;+}+function normalizeContainerCode(code, prefix) {const { quoteDepth, listPrefix } = fenceContainer(prefix);if (!quoteDepth && !listPrefix) return code;@@ -135,7 +154,9 @@ export function replaceMarkdownCodeFences(value, renderBlock) {let block = null;const stack = [];- for (const match of source.matchAll(fenceLines)) {+ const matches = [...source.matchAll(fenceLines)];+ for (let matchIndex = 0; matchIndex < matches.length; matchIndex += 1) {+ const match = matches[matchIndex];const [, prefix, indentation, fence, info] = match;const container = fenceContainer(prefix, indentation);const validOpening = fence[0] !== '`' || !info.includes('`');@@ -153,8 +174,11 @@ export function replaceMarkdownCodeFences(value, renderBlock) {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 (fenceCloserInContainer(active.container, container)- && !info.trim() && fence[0] === active.fence[0] && fence.length >= active.fence.length) {+ const hasImmediatelyFollowingText = /^[ \t]*[^\r\n]/.test(+ source.slice(match.index + match[0].length),+ );+ if (isFenceCloser(active, container, fence, info)+ && (!active.pendingNested || hasImmediatelyFollowingText)) {stack.pop();if (!stack.length) {output.push(block.prefix + renderBlock(@@ -170,7 +194,13 @@ 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, container });+ if (nestedFenceHasOwnCloser(matches, matchIndex, active)) {+ stack.push({ fence, markdown, container });+ } else {+ // During streaming, the only available blank fence may close this+ // example rather than its still-open Markdown wrapper.+ active.pendingNested = true;+ }}}diff --git a/src/firefox/src/ui/markdown-render.js b/src/firefox/src/ui/markdown-render.jsindex 50deaa6f9..7f4739078 100644--- a/src/firefox/src/ui/markdown-render.js+++ b/src/firefox/src/ui/markdown-render.js@@ -102,6 +102,25 @@ function fenceCloserInContainer(opener, candidate) {&& candidateIndent <= listIndent + 3;}+function isFenceCloser(opener, candidate, fence, info) {+ return fenceCloserInContainer(opener.container, candidate)+ && !info.trim()+ && fence[0] === opener.fence[0]+ && fence.length >= opener.fence.length;+}++function nestedFenceHasOwnCloser(matches, startIndex, active) {+ let closers = 0;+ for (let index = startIndex + 1; index < matches.length; index += 1) {+ const [, prefix, indentation, fence, info] = matches[index];+ if (isFenceCloser(active, fenceContainer(prefix, indentation), fence, info)) {+ closers += 1;+ if (closers === 2) return true;+ }+ }+ return false;+}+function normalizeContainerCode(code, prefix) {const { quoteDepth, listPrefix } = fenceContainer(prefix);if (!quoteDepth && !listPrefix) return code;@@ -135,7 +154,9 @@ export function replaceMarkdownCodeFences(value, renderBlock) {let block = null;const stack = [];- for (const match of source.matchAll(fenceLines)) {+ const matches = [...source.matchAll(fenceLines)];+ for (let matchIndex = 0; matchIndex < matches.length; matchIndex += 1) {+ const match = matches[matchIndex];const [, prefix, indentation, fence, info] = match;const container = fenceContainer(prefix, indentation);const validOpening = fence[0] !== '`' || !info.includes('`');@@ -153,8 +174,11 @@ export function replaceMarkdownCodeFences(value, renderBlock) {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 (fenceCloserInContainer(active.container, container)- && !info.trim() && fence[0] === active.fence[0] && fence.length >= active.fence.length) {+ const hasImmediatelyFollowingText = /^[ \t]*[^\r\n]/.test(+ source.slice(match.index + match[0].length),+ );+ if (isFenceCloser(active, container, fence, info)+ && (!active.pendingNested || hasImmediatelyFollowingText)) {stack.pop();if (!stack.length) {output.push(block.prefix + renderBlock(@@ -170,7 +194,13 @@ 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, container });+ if (nestedFenceHasOwnCloser(matches, matchIndex, active)) {+ stack.push({ fence, markdown, container });+ } else {+ // During streaming, the only available blank fence may close this+ // example rather than its still-open Markdown wrapper.+ active.pendingNested = true;+ }}}diff --git a/test/markdown-render.mjs b/test/markdown-render.mjsindex 2b8524194..94b8058c1 100644--- a/test/markdown-render.mjs+++ b/test/markdown-render.mjs@@ -84,6 +84,20 @@ for (const build of ['chrome', 'firefox']) {assert.match(formatMarkdown(source), /<h2>Outside<\/h2>/);});+ test(`${build}: unmatched nested example fences do not steal an outer closer`, () => {+ const source = '```markdown\n```js\nconst value = true;\n```\n## Outside';+ const blocks = [];+ const remaining = helpers.replaceMarkdownCodeFences(source, (info, code) => {+ blocks.push({ info, code });+ return 'BLOCK';+ });+ assert.deepEqual(blocks, [{ info: 'markdown', code: '```js\nconst value = true;\n' }]);+ assert.equal(remaining, 'BLOCK\n## Outside');+ assert.match(formatMarkdown(source), /<h2>Outside<\/h2>/);+ const incomplete = '```markdown\n```js\nconst value = true;\n```';+ assert.deepEqual(preContents(formatMarkdown(incomplete, { enhance: false })), [escapeHtml('```js\nconst value = true;\n```')]);+ });+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}`;