Skip to content

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

Retain list context for indented fences

Emre Sokulluebb0f9e936d8
diff --git a/src/chrome/src/ui/markdown-render.js b/src/chrome/src/ui/markdown-render.js
index 638d53a26..5aae847d2 100644
--- a/src/chrome/src/ui/markdown-render.js
+++ b/src/chrome/src/ui/markdown-render.js
@@ -134,19 +134,34 @@ function nestedFenceHasOwnCloser(matches, startIndex, active, stopAtNamedFence =
return false;
}
-function listContinuationContainer(source, position, indentation) {
- const beforeFence = source.slice(0, position).replace(/\r?\n$/, '');
- const previousLine = beforeFence.slice(beforeFence.lastIndexOf('\n') + 1);
- const prefix = previousLine.match(
- /^((?: {0,3}>[ \t]?)*(?:[ \t]*(?:[-+*]|\d+[.)])[ \t]+))/,
- )?.[1];
- if (!prefix) return null;
-
- const container = fenceContainer(prefix);
+function listContinuationContainer(source, position, prefix, indentation) {
+ const current = fenceContainer(prefix, indentation);
const continuationIndent = indentationColumns(indentation);
- const listIndent = indentationColumns(container.listPrefix);
- if (!listIndent || continuationIndent < listIndent || continuationIndent > listIndent + 3) return null;
- return container;
+ const lines = source.slice(0, position).split(/\r?\n/);
+ if (lines.at(-1) === '') lines.pop();
+
+ for (let index = lines.length - 1; index >= 0; index -= 1) {
+ const line = lines[index];
+ if (!line.trim()) continue;
+ const quotePrefix = line.match(/^(?: {0,3}>[ \t]?)+/)?.[0] || '';
+ const quoteDepth = (quotePrefix.match(/>/g) || []).length;
+ if (quoteDepth !== current.quoteDepth) break;
+
+ const content = line.slice(quotePrefix.length);
+ const listPrefix = content.match(/^[ \t]*(?:[-+*]|\d+[.)])[ \t]+/)?.[0] || '';
+ if (listPrefix) {
+ const container = fenceContainer(`${quotePrefix}${listPrefix}`);
+ const listIndent = indentationColumns(container.listPrefix);
+ if (listIndent && continuationIndent >= listIndent && continuationIndent <= listIndent + 3) {
+ return container;
+ }
+ return null;
+ }
+
+ const lineIndent = indentationColumns(content.match(/^[ \t]*/)?.[0] || '');
+ if (lineIndent < continuationIndent) break;
+ }
+ return null;
}
function normalizeContainerCode(code, prefixOrContainer) {
@@ -198,9 +213,9 @@ export function replaceMarkdownCodeFences(value, renderBlock, { streaming = fals
let openingPrefix = prefix;
if (!validOpening) continue;
if (indentationColumns(container.indentation) > 3) {
- openingContainer = listContinuationContainer(source, match.index, indentation);
+ openingContainer = listContinuationContainer(source, match.index, prefix, indentation);
if (!openingContainer) continue;
- openingPrefix = indentation;
+ openingPrefix = `${prefix}${indentation}`;
}
output.push(source.slice(cursor, match.index));
block = { info, prefix: openingPrefix, container: openingContainer, start: match.index + match[0].length };
diff --git a/src/firefox/src/ui/markdown-render.js b/src/firefox/src/ui/markdown-render.js
index 638d53a26..5aae847d2 100644
--- a/src/firefox/src/ui/markdown-render.js
+++ b/src/firefox/src/ui/markdown-render.js
@@ -134,19 +134,34 @@ function nestedFenceHasOwnCloser(matches, startIndex, active, stopAtNamedFence =
return false;
}
-function listContinuationContainer(source, position, indentation) {
- const beforeFence = source.slice(0, position).replace(/\r?\n$/, '');
- const previousLine = beforeFence.slice(beforeFence.lastIndexOf('\n') + 1);
- const prefix = previousLine.match(
- /^((?: {0,3}>[ \t]?)*(?:[ \t]*(?:[-+*]|\d+[.)])[ \t]+))/,
- )?.[1];
- if (!prefix) return null;
-
- const container = fenceContainer(prefix);
+function listContinuationContainer(source, position, prefix, indentation) {
+ const current = fenceContainer(prefix, indentation);
const continuationIndent = indentationColumns(indentation);
- const listIndent = indentationColumns(container.listPrefix);
- if (!listIndent || continuationIndent < listIndent || continuationIndent > listIndent + 3) return null;
- return container;
+ const lines = source.slice(0, position).split(/\r?\n/);
+ if (lines.at(-1) === '') lines.pop();
+
+ for (let index = lines.length - 1; index >= 0; index -= 1) {
+ const line = lines[index];
+ if (!line.trim()) continue;
+ const quotePrefix = line.match(/^(?: {0,3}>[ \t]?)+/)?.[0] || '';
+ const quoteDepth = (quotePrefix.match(/>/g) || []).length;
+ if (quoteDepth !== current.quoteDepth) break;
+
+ const content = line.slice(quotePrefix.length);
+ const listPrefix = content.match(/^[ \t]*(?:[-+*]|\d+[.)])[ \t]+/)?.[0] || '';
+ if (listPrefix) {
+ const container = fenceContainer(`${quotePrefix}${listPrefix}`);
+ const listIndent = indentationColumns(container.listPrefix);
+ if (listIndent && continuationIndent >= listIndent && continuationIndent <= listIndent + 3) {
+ return container;
+ }
+ return null;
+ }
+
+ const lineIndent = indentationColumns(content.match(/^[ \t]*/)?.[0] || '');
+ if (lineIndent < continuationIndent) break;
+ }
+ return null;
}
function normalizeContainerCode(code, prefixOrContainer) {
@@ -198,9 +213,9 @@ export function replaceMarkdownCodeFences(value, renderBlock, { streaming = fals
let openingPrefix = prefix;
if (!validOpening) continue;
if (indentationColumns(container.indentation) > 3) {
- openingContainer = listContinuationContainer(source, match.index, indentation);
+ openingContainer = listContinuationContainer(source, match.index, prefix, indentation);
if (!openingContainer) continue;
- openingPrefix = indentation;
+ openingPrefix = `${prefix}${indentation}`;
}
output.push(source.slice(cursor, match.index));
block = { info, prefix: openingPrefix, container: openingContainer, start: match.index + match[0].length };
diff --git a/test/markdown-render.mjs b/test/markdown-render.mjs
index 44dab7362..407cb5caf 100644
--- a/test/markdown-render.mjs
+++ b/test/markdown-render.mjs
@@ -187,16 +187,22 @@ for (const build of ['chrome', 'firefox']) {
});
test(`${build}: list continuation fences use the preceding list indent`, () => {
- const source = '10. Step\n ```js\n const value = true;\n ```\nAfter';
- const blocks = [];
- const remaining = helpers.replaceMarkdownCodeFences(source, (info, code) => {
- blocks.push({ info, code });
- return 'BLOCK';
- });
- assert.deepEqual(blocks, [{ info: 'js', code: 'const value = true;\n' }]);
- assert.equal(remaining, '10. Step\n BLOCK\nAfter');
- assert.equal(preContents(formatMarkdown(source)).length, 1);
- assert.match(formatMarkdown(source), /After/);
+ const cases = [
+ ['10. Step\n ```js\n const value = true;\n ```\nAfter', '10. Step\n BLOCK\nAfter'],
+ ['10. Step\n continuation\n ```js\n const value = true;\n ```\nAfter', '10. Step\n continuation\n BLOCK\nAfter'],
+ ['> 10. Step\n> ```js\n> const value = true;\n> ```\n> After', '> 10. Step\n> BLOCK\n> After'],
+ ];
+ 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: 'js', code: 'const value = true;\n' }]);
+ assert.equal(remaining, expected);
+ assert.equal(preContents(formatMarkdown(source)).length, 1);
+ }
+ assert.match(renderSkillMarkdown(cases[2][0]), /<blockquote>[\s\S]*<pre><code>const value = true;\n<\/code><\/pre>/);
});
test(`${build}: tab-indented list fences use visual indentation columns`, () => {