Skip to content

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

Handle Markdown fence container whitespace

Emre Sokullu94a885cf3230
diff --git a/src/chrome/src/ui/markdown-render.js b/src/chrome/src/ui/markdown-render.js
index 6f4dec5d5..0b55570fc 100644
--- a/src/chrome/src/ui/markdown-render.js
+++ b/src/chrome/src/ui/markdown-render.js
@@ -133,7 +133,11 @@ function fenceCloserInContainer(opener, candidate) {
return !candidate.quoteDepth && !candidate.listPrefix
&& indentationColumns(candidate.indentation) <= 3;
}
- return stripContainerPrefix(`${candidate.rawPrefix}${candidate.indentation}x`, opener) === 'x';
+ const remainder = stripContainerPrefix(`${candidate.rawPrefix}${candidate.indentation}x`, opener);
+ const extraIndentation = remainder?.slice(0, -1);
+ return remainder?.endsWith('x')
+ && /^[ \t]*$/.test(extraIndentation)
+ && indentationColumns(extraIndentation) <= 3;
}
function isFenceCloser(opener, candidate, fence, info) {
@@ -214,7 +218,7 @@ function unfinishedContainerEnd(source, start, container) {
for (const match of remainder.matchAll(/[^\r\n]*(?:\r?\n|$)/g)) {
if (!match[0]) break;
const line = match[0].replace(/\r?\n$/, '');
- if (container.listPrefix && lineIsBlankInContainer(line, container)
+ if ((container.quoteDepth || container.listPrefix) && lineIsBlankInContainer(line, container)
&& followingLineBelongsToContainer(remainder, match.index + match[0].length, container)) {
offset += match[0].length;
continue;
diff --git a/src/firefox/src/ui/markdown-render.js b/src/firefox/src/ui/markdown-render.js
index 6f4dec5d5..0b55570fc 100644
--- a/src/firefox/src/ui/markdown-render.js
+++ b/src/firefox/src/ui/markdown-render.js
@@ -133,7 +133,11 @@ function fenceCloserInContainer(opener, candidate) {
return !candidate.quoteDepth && !candidate.listPrefix
&& indentationColumns(candidate.indentation) <= 3;
}
- return stripContainerPrefix(`${candidate.rawPrefix}${candidate.indentation}x`, opener) === 'x';
+ const remainder = stripContainerPrefix(`${candidate.rawPrefix}${candidate.indentation}x`, opener);
+ const extraIndentation = remainder?.slice(0, -1);
+ return remainder?.endsWith('x')
+ && /^[ \t]*$/.test(extraIndentation)
+ && indentationColumns(extraIndentation) <= 3;
}
function isFenceCloser(opener, candidate, fence, info) {
@@ -214,7 +218,7 @@ function unfinishedContainerEnd(source, start, container) {
for (const match of remainder.matchAll(/[^\r\n]*(?:\r?\n|$)/g)) {
if (!match[0]) break;
const line = match[0].replace(/\r?\n$/, '');
- if (container.listPrefix && lineIsBlankInContainer(line, container)
+ if ((container.quoteDepth || container.listPrefix) && lineIsBlankInContainer(line, container)
&& followingLineBelongsToContainer(remainder, match.index + match[0].length, container)) {
offset += match[0].length;
continue;
diff --git a/test/markdown-render.mjs b/test/markdown-render.mjs
index c764988ff..cd8133754 100644
--- a/test/markdown-render.mjs
+++ b/test/markdown-render.mjs
@@ -183,6 +183,14 @@ for (const build of ['chrome', 'firefox']) {
assert.deepEqual(blocks, [{ info: 'text', code: ' ```\nvalue\n' }]);
assert.equal(remaining, '- BLOCK\nAfter');
assert.match(formatMarkdown(source), /After/);
+
+ const allowedCloserIndent = '- ```text\n hello\n ```\nAfter';
+ const allowedCloserBlocks = [];
+ assert.equal(helpers.replaceMarkdownCodeFences(allowedCloserIndent, (info, code) => {
+ allowedCloserBlocks.push({ info, code });
+ return 'BLOCK';
+ }), '- BLOCK\nAfter');
+ assert.deepEqual(allowedCloserBlocks, [{ info: 'text', code: 'hello\n' }]);
});
test(`${build}: list continuation fences use the preceding list indent`, () => {
@@ -278,6 +286,14 @@ for (const build of ['chrome', 'firefox']) {
}), ' > - BLOCK\n > Outside\n\nAfter');
assert.deepEqual(quoteListBlocks, [{ info: 'text', code: 'hello\n' }]);
+ const quoteBlank = '> ```text\n> hello\n>\n> again\n> ```';
+ const quoteBlankBlocks = [];
+ assert.equal(helpers.replaceMarkdownCodeFences(quoteBlank, (info, code) => {
+ quoteBlankBlocks.push({ info, code });
+ return 'BLOCK';
+ }), '> BLOCK');
+ assert.deepEqual(quoteBlankBlocks, [{ info: 'text', code: 'hello\n\nagain\n' }]);
+
const noCloser = '> ```text\n> inside\nOutside';
const noCloserBlocks = [];
assert.equal(helpers.replaceMarkdownCodeFences(noCloser, (info, code) => {