Skip to content

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

Reprocess fences after Markdown container boundaries

Emre Sokullu5af372b1182b
diff --git a/src/chrome/src/ui/markdown-render.js b/src/chrome/src/ui/markdown-render.js
index 3ff171fc9..96d7b6bdf 100644
--- a/src/chrome/src/ui/markdown-render.js
+++ b/src/chrome/src/ui/markdown-render.js
@@ -534,7 +534,6 @@ export function replaceMarkdownCodeFences(value, renderBlock, { streaming = fals
if (block) {
const boundary = block.boundary;
if (boundary <= match.index) {
- const wasClosingFence = isFenceCloser(stack[stack.length - 1], container, fence, info);
const code = source.slice(block.start, boundary);
const needsBoundaryNewline = (/\r?\n$/.test(code) || (!code && block.openingEndsWithNewline))
&& !/^\r?\n/.test(source.slice(boundary));
@@ -545,7 +544,7 @@ export function replaceMarkdownCodeFences(value, renderBlock, { streaming = fals
cursor = boundary;
block = null;
stack.length = 0;
- if (!wasClosingFence) matchIndex -= 1;
+ matchIndex -= 1;
continue;
}
}
diff --git a/src/firefox/src/ui/markdown-render.js b/src/firefox/src/ui/markdown-render.js
index 3ff171fc9..96d7b6bdf 100644
--- a/src/firefox/src/ui/markdown-render.js
+++ b/src/firefox/src/ui/markdown-render.js
@@ -534,7 +534,6 @@ export function replaceMarkdownCodeFences(value, renderBlock, { streaming = fals
if (block) {
const boundary = block.boundary;
if (boundary <= match.index) {
- const wasClosingFence = isFenceCloser(stack[stack.length - 1], container, fence, info);
const code = source.slice(block.start, boundary);
const needsBoundaryNewline = (/\r?\n$/.test(code) || (!code && block.openingEndsWithNewline))
&& !/^\r?\n/.test(source.slice(boundary));
@@ -545,7 +544,7 @@ export function replaceMarkdownCodeFences(value, renderBlock, { streaming = fals
cursor = boundary;
block = null;
stack.length = 0;
- if (!wasClosingFence) matchIndex -= 1;
+ matchIndex -= 1;
continue;
}
}
diff --git a/test/markdown-render.mjs b/test/markdown-render.mjs
index 32c3fe2ea..c79b465da 100644
--- a/test/markdown-render.mjs
+++ b/test/markdown-render.mjs
@@ -371,6 +371,13 @@ for (const build of ['chrome', 'firefox']) {
assert.equal(remaining, expected);
assert.equal(preContents(formatMarkdown(source)).length, 1);
}
+ const siblingUnfinishedFences = '- item\n ```js\n one\n- next\n ```\n two\nOutside';
+ const siblingBlocks = [];
+ assert.equal(helpers.replaceMarkdownCodeFences(siblingUnfinishedFences, (info, code) => {
+ siblingBlocks.push({ info, code });
+ return 'BLOCK';
+ }), '- item\n BLOCK\n- next\n BLOCK\nOutside');
+ assert.deepEqual(siblingBlocks, [{ info: 'js', code: 'one\n' }, { info: '', code: 'two\n' }]);
assert.match(formatMarkdown(cases[0][0]), /Outside/);
assert.match(formatMarkdown(cases[1][0]), /Next/);
const unfinishedList = '- ```text\n hello\n\n again';
@@ -402,24 +409,24 @@ for (const build of ['chrome', 'firefox']) {
assert.equal(helpers.replaceMarkdownCodeFences(unprefixedQuoteBreak, (info, code) => {
unprefixedQuoteBlocks.push({ info, code });
return 'BLOCK';
- }), '> BLOCK\n> second\n> ```\nAfter');
- assert.deepEqual(unprefixedQuoteBlocks, [{ info: 'text', code: 'first\n' }]);
+ }), '> BLOCK\n> second\n> BLOCK\nAfter');
+ assert.deepEqual(unprefixedQuoteBlocks, [{ info: 'text', code: 'first\n' }, { info: '', code: '' }]);
const mixedQuoteListBreak = '> - ```text\n> first\n\n> second\n> ```\nAfter';
const mixedQuoteListBlocks = [];
assert.equal(helpers.replaceMarkdownCodeFences(mixedQuoteListBreak, (info, code) => {
mixedQuoteListBlocks.push({ info, code });
return 'BLOCK';
- }), '> - BLOCK\n> second\n> ```\nAfter');
- assert.deepEqual(mixedQuoteListBlocks, [{ info: 'text', code: 'first\n' }]);
+ }), '> - BLOCK\n> second\n> BLOCK\nAfter');
+ assert.deepEqual(mixedQuoteListBlocks, [{ info: 'text', code: 'first\n' }, { info: '', code: '' }]);
const quotedBlankReset = '> ```text\n> first\n\n>\n> second\n> ```\nAfter';
const quotedBlankResetBlocks = [];
assert.equal(helpers.replaceMarkdownCodeFences(quotedBlankReset, (info, code) => {
quotedBlankResetBlocks.push({ info, code });
return 'BLOCK';
- }), '> BLOCK\n>\n> second\n> ```\nAfter');
- assert.deepEqual(quotedBlankResetBlocks, [{ info: 'text', code: 'first\n' }]);
+ }), '> BLOCK\n>\n> second\n> BLOCK\nAfter');
+ assert.deepEqual(quotedBlankResetBlocks, [{ info: 'text', code: 'first\n' }, { info: '', code: '' }]);
const wideQuotedListBlank = '10. > ```text\n > hello\n >\n > again\n > ```';
const wideQuotedListBlocks = [];
@@ -522,8 +529,8 @@ for (const build of ['chrome', 'firefox']) {
assert.equal(helpers.replaceMarkdownCodeFences(escapedQuote, (info, code) => {
quoteBlocks.push({ info, code });
return 'BLOCK';
- }), '> BLOCK\nOutside\n> ```\nAfter');
- assert.deepEqual(quoteBlocks, [{ info: 'text', code: 'inside\n' }]);
+ }), '> BLOCK\nOutside\n> BLOCK\nAfter');
+ assert.deepEqual(quoteBlocks, [{ info: 'text', code: 'inside\n' }, { info: '', code: '' }]);
const laterBlock = '> ```text\n> inside\nOutside\n```js\ncode\n```\nAfter';
const laterBlocks = [];