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
Bound nested fence lookahead
Emre Sokullubf23a0448578
diff --git a/src/chrome/src/ui/markdown-render.js b/src/chrome/src/ui/markdown-render.jsindex 63060ba26..2e86624df 100644--- a/src/chrome/src/ui/markdown-render.js+++ b/src/chrome/src/ui/markdown-render.js@@ -109,13 +109,15 @@ function isFenceCloser(opener, candidate, fence, info) {&& fence.length >= opener.fence.length;}-function nestedFenceHasOwnCloser(matches, startIndex, active) {+function nestedFenceHasOwnCloser(matches, startIndex, active, stopAtNamedFence = false) {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;+ } else if (stopAtNamedFence && closers && info.trim()) {+ return false;}}return false;@@ -190,7 +192,13 @@ export function replaceMarkdownCodeFences(value, renderBlock, { streaming = fals// 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.- if (streaming || nestedFenceHasOwnCloser(matches, matchIndex, active)) {+ const hasMarkdownPreamble = source.slice(block.start, match.index).trim().length > 0;+ if (streaming || nestedFenceHasOwnCloser(+ matches,+ matchIndex,+ active,+ !hasMarkdownPreamble,+ )) {stack.push({ fence, markdown, container });}}diff --git a/src/firefox/src/ui/markdown-render.js b/src/firefox/src/ui/markdown-render.jsindex 63060ba26..2e86624df 100644--- a/src/firefox/src/ui/markdown-render.js+++ b/src/firefox/src/ui/markdown-render.js@@ -109,13 +109,15 @@ function isFenceCloser(opener, candidate, fence, info) {&& fence.length >= opener.fence.length;}-function nestedFenceHasOwnCloser(matches, startIndex, active) {+function nestedFenceHasOwnCloser(matches, startIndex, active, stopAtNamedFence = false) {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;+ } else if (stopAtNamedFence && closers && info.trim()) {+ return false;}}return false;@@ -190,7 +192,13 @@ export function replaceMarkdownCodeFences(value, renderBlock, { streaming = fals// 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.- if (streaming || nestedFenceHasOwnCloser(matches, matchIndex, active)) {+ const hasMarkdownPreamble = source.slice(block.start, match.index).trim().length > 0;+ if (streaming || nestedFenceHasOwnCloser(+ matches,+ matchIndex,+ active,+ !hasMarkdownPreamble,+ )) {stack.push({ fence, markdown, container });}}diff --git a/test/markdown-render.mjs b/test/markdown-render.mjsindex f87ffaf6c..2adf129e6 100644--- a/test/markdown-render.mjs+++ b/test/markdown-render.mjs@@ -85,15 +85,19 @@ for (const build of ['chrome', 'firefox']) {});test(`${build}: unmatched nested example fences do not steal an outer closer`, () => {- const source = '```markdown\n```js\nconst value = true;\n```\n\n## Outside';+ const source = '```markdown\n```js\nconst value = true;\n```\n\nOutside\n```python\nother\n```\nAfter';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\n## Outside');- assert.match(formatMarkdown(source), /<h2>Outside<\/h2>/);+ assert.deepEqual(blocks, [+ { info: 'markdown', code: '```js\nconst value = true;\n' },+ { info: 'python', code: 'other\n' },+ ]);+ assert.equal(remaining, 'BLOCK\n\nOutside\nBLOCK\nAfter');+ assert.match(formatMarkdown(source), /Outside/);+ assert.match(formatMarkdown(source), /After/);const incomplete = '```markdown\n```js\nconst value = true;\n```';assert.deepEqual(preContents(formatMarkdown(incomplete, { enhance: false })), [escapeHtml('```js\nconst value = true;\n```')]);});