Skip to content

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

End unfinished fences at container boundaries

Emre Sokullueb5accb82169
diff --git a/src/chrome/src/ui/markdown-render.js b/src/chrome/src/ui/markdown-render.js
index 5aae847d2..6653db406 100644
--- a/src/chrome/src/ui/markdown-render.js
+++ b/src/chrome/src/ui/markdown-render.js
@@ -164,6 +164,31 @@ function listContinuationContainer(source, position, prefix, indentation) {
return null;
}
+function lineBelongsToContainer(line, container) {
+ const quotePrefix = line.match(/^(?: {0,3}>[ \t]?)+/)?.[0] || '';
+ const quoteDepth = (quotePrefix.match(/>/g) || []).length;
+ if (quoteDepth !== container.quoteDepth) return false;
+ if (!container.listPrefix) return true;
+
+ const content = line.slice(quotePrefix.length);
+ if (!content.trim()) return false;
+ const indent = indentationColumns(content.match(/^[ \t]*/)?.[0] || '');
+ return indent >= indentationColumns(container.listPrefix);
+}
+
+function unfinishedContainerEnd(source, start, container) {
+ if (!container.quoteDepth && !container.listPrefix) return source.length;
+ const remainder = source.slice(start);
+ let offset = start;
+ for (const match of remainder.matchAll(/[^\r\n]*(?:\r?\n|$)/g)) {
+ if (!match[0]) break;
+ const line = match[0].replace(/\r?\n$/, '');
+ if (!lineBelongsToContainer(line, container)) return offset;
+ offset += match[0].length;
+ }
+ return source.length;
+}
+
function normalizeContainerCode(code, prefixOrContainer) {
const { quoteDepth, listPrefix } = typeof prefixOrContainer === 'object'
? prefixOrContainer
@@ -254,11 +279,14 @@ export function replaceMarkdownCodeFences(value, renderBlock, { streaming = fals
}
}
- if (block) output.push(block.prefix + renderBlock(
- block.info,
- normalizeContainerCode(source.slice(block.start), block.container),
- ));
- else output.push(source.slice(cursor));
+ if (block) {
+ const end = unfinishedContainerEnd(source, block.start, block.container);
+ output.push(block.prefix + renderBlock(
+ block.info,
+ normalizeContainerCode(source.slice(block.start, end), block.container),
+ ));
+ output.push(source.slice(end));
+ } else output.push(source.slice(cursor));
return output.join('');
}
diff --git a/src/firefox/src/ui/markdown-render.js b/src/firefox/src/ui/markdown-render.js
index 5aae847d2..6653db406 100644
--- a/src/firefox/src/ui/markdown-render.js
+++ b/src/firefox/src/ui/markdown-render.js
@@ -164,6 +164,31 @@ function listContinuationContainer(source, position, prefix, indentation) {
return null;
}
+function lineBelongsToContainer(line, container) {
+ const quotePrefix = line.match(/^(?: {0,3}>[ \t]?)+/)?.[0] || '';
+ const quoteDepth = (quotePrefix.match(/>/g) || []).length;
+ if (quoteDepth !== container.quoteDepth) return false;
+ if (!container.listPrefix) return true;
+
+ const content = line.slice(quotePrefix.length);
+ if (!content.trim()) return false;
+ const indent = indentationColumns(content.match(/^[ \t]*/)?.[0] || '');
+ return indent >= indentationColumns(container.listPrefix);
+}
+
+function unfinishedContainerEnd(source, start, container) {
+ if (!container.quoteDepth && !container.listPrefix) return source.length;
+ const remainder = source.slice(start);
+ let offset = start;
+ for (const match of remainder.matchAll(/[^\r\n]*(?:\r?\n|$)/g)) {
+ if (!match[0]) break;
+ const line = match[0].replace(/\r?\n$/, '');
+ if (!lineBelongsToContainer(line, container)) return offset;
+ offset += match[0].length;
+ }
+ return source.length;
+}
+
function normalizeContainerCode(code, prefixOrContainer) {
const { quoteDepth, listPrefix } = typeof prefixOrContainer === 'object'
? prefixOrContainer
@@ -254,11 +279,14 @@ export function replaceMarkdownCodeFences(value, renderBlock, { streaming = fals
}
}
- if (block) output.push(block.prefix + renderBlock(
- block.info,
- normalizeContainerCode(source.slice(block.start), block.container),
- ));
- else output.push(source.slice(cursor));
+ if (block) {
+ const end = unfinishedContainerEnd(source, block.start, block.container);
+ output.push(block.prefix + renderBlock(
+ block.info,
+ normalizeContainerCode(source.slice(block.start, end), block.container),
+ ));
+ output.push(source.slice(end));
+ } else output.push(source.slice(cursor));
return output.join('');
}
diff --git a/test/markdown-render.mjs b/test/markdown-render.mjs
index 407cb5caf..94c057118 100644
--- a/test/markdown-render.mjs
+++ b/test/markdown-render.mjs
@@ -205,6 +205,25 @@ for (const build of ['chrome', 'firefox']) {
assert.match(renderSkillMarkdown(cases[2][0]), /<blockquote>[\s\S]*<pre><code>const value = true;\n<\/code><\/pre>/);
});
+ test(`${build}: unfinished container fences stop at their container boundary`, () => {
+ const cases = [
+ ['> ```text\n> hello\n\nOutside', '> BLOCK\nOutside'],
+ ['- ```text\n hello\n\n- Next', '- BLOCK\n- Next'],
+ ];
+ for (const [source, expected] of cases) {
+ const blocks = [];
+ const remaining = helpers.replaceMarkdownCodeFences(source, (info, code) => {
+ blocks.push({ info, code });
+ return 'BLOCK';
+ });
+ assert.deepEqual(blocks, [{ info: 'text', code: 'hello\n' }]);
+ assert.equal(remaining, expected);
+ assert.equal(preContents(formatMarkdown(source)).length, 1);
+ }
+ assert.match(formatMarkdown(cases[0][0]), /Outside/);
+ assert.match(formatMarkdown(cases[1][0]), /Next/);
+ });
+
test(`${build}: tab-indented list fences use visual indentation columns`, () => {
const source = '-\t```text\n\tvalue\n\t```\nAfter';
const blocks = [];