Skip to content

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

Support continuation list code fences

Emre Sokullu4020be80d388
diff --git a/src/chrome/src/ui/markdown-render.js b/src/chrome/src/ui/markdown-render.js
index 2e86624df..638d53a26 100644
--- a/src/chrome/src/ui/markdown-render.js
+++ b/src/chrome/src/ui/markdown-render.js
@@ -109,11 +109,22 @@ function isFenceCloser(opener, candidate, fence, info) {
&& fence.length >= opener.fence.length;
}
+function hasCompatibleFenceAfter(matches, startIndex, active) {
+ 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)) return true;
+ }
+ return false;
+}
+
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)) {
+ if (stopAtNamedFence && closers && hasCompatibleFenceAfter(matches, index, active)) {
+ return false;
+ }
closers += 1;
if (closers === 2) return true;
} else if (stopAtNamedFence && closers && info.trim()) {
@@ -123,8 +134,25 @@ function nestedFenceHasOwnCloser(matches, startIndex, active, stopAtNamedFence =
return false;
}
-function normalizeContainerCode(code, prefix) {
- const { quoteDepth, listPrefix } = fenceContainer(prefix);
+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);
+ const continuationIndent = indentationColumns(indentation);
+ const listIndent = indentationColumns(container.listPrefix);
+ if (!listIndent || continuationIndent < listIndent || continuationIndent > listIndent + 3) return null;
+ return container;
+}
+
+function normalizeContainerCode(code, prefixOrContainer) {
+ const { quoteDepth, listPrefix } = typeof prefixOrContainer === 'object'
+ ? prefixOrContainer
+ : fenceContainer(prefixOrContainer);
if (!quoteDepth && !listPrefix) return code;
const listIndent = indentationColumns(listPrefix);
@@ -166,10 +194,17 @@ export function replaceMarkdownCodeFences(value, renderBlock, { streaming = fals
if (!block) {
// Four-space indented code is not a fenced block at the document root,
// but list continuations may require more than three spaces to close.
- if (!validOpening || indentationColumns(container.indentation) > 3) continue;
+ let openingContainer = container;
+ let openingPrefix = prefix;
+ if (!validOpening) continue;
+ if (indentationColumns(container.indentation) > 3) {
+ openingContainer = listContinuationContainer(source, match.index, indentation);
+ if (!openingContainer) continue;
+ openingPrefix = indentation;
+ }
output.push(source.slice(cursor, match.index));
- block = { info, prefix, container, start: match.index + match[0].length };
- stack.push({ fence, markdown, container });
+ block = { info, prefix: openingPrefix, container: openingContainer, start: match.index + match[0].length };
+ stack.push({ fence, markdown, container: openingContainer });
continue;
}
@@ -181,7 +216,7 @@ export function replaceMarkdownCodeFences(value, renderBlock, { streaming = fals
if (!stack.length) {
output.push(block.prefix + renderBlock(
block.info,
- normalizeContainerCode(source.slice(block.start, match.index), block.prefix),
+ normalizeContainerCode(source.slice(block.start, match.index), block.container),
));
// Leave the closing line's newline for the surrounding Markdown.
cursor = match.index + match[0].replace(/\r?\n$/, '').length;
@@ -206,7 +241,7 @@ export function replaceMarkdownCodeFences(value, renderBlock, { streaming = fals
if (block) output.push(block.prefix + renderBlock(
block.info,
- normalizeContainerCode(source.slice(block.start), block.prefix),
+ normalizeContainerCode(source.slice(block.start), block.container),
));
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 2e86624df..638d53a26 100644
--- a/src/firefox/src/ui/markdown-render.js
+++ b/src/firefox/src/ui/markdown-render.js
@@ -109,11 +109,22 @@ function isFenceCloser(opener, candidate, fence, info) {
&& fence.length >= opener.fence.length;
}
+function hasCompatibleFenceAfter(matches, startIndex, active) {
+ 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)) return true;
+ }
+ return false;
+}
+
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)) {
+ if (stopAtNamedFence && closers && hasCompatibleFenceAfter(matches, index, active)) {
+ return false;
+ }
closers += 1;
if (closers === 2) return true;
} else if (stopAtNamedFence && closers && info.trim()) {
@@ -123,8 +134,25 @@ function nestedFenceHasOwnCloser(matches, startIndex, active, stopAtNamedFence =
return false;
}
-function normalizeContainerCode(code, prefix) {
- const { quoteDepth, listPrefix } = fenceContainer(prefix);
+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);
+ const continuationIndent = indentationColumns(indentation);
+ const listIndent = indentationColumns(container.listPrefix);
+ if (!listIndent || continuationIndent < listIndent || continuationIndent > listIndent + 3) return null;
+ return container;
+}
+
+function normalizeContainerCode(code, prefixOrContainer) {
+ const { quoteDepth, listPrefix } = typeof prefixOrContainer === 'object'
+ ? prefixOrContainer
+ : fenceContainer(prefixOrContainer);
if (!quoteDepth && !listPrefix) return code;
const listIndent = indentationColumns(listPrefix);
@@ -166,10 +194,17 @@ export function replaceMarkdownCodeFences(value, renderBlock, { streaming = fals
if (!block) {
// Four-space indented code is not a fenced block at the document root,
// but list continuations may require more than three spaces to close.
- if (!validOpening || indentationColumns(container.indentation) > 3) continue;
+ let openingContainer = container;
+ let openingPrefix = prefix;
+ if (!validOpening) continue;
+ if (indentationColumns(container.indentation) > 3) {
+ openingContainer = listContinuationContainer(source, match.index, indentation);
+ if (!openingContainer) continue;
+ openingPrefix = indentation;
+ }
output.push(source.slice(cursor, match.index));
- block = { info, prefix, container, start: match.index + match[0].length };
- stack.push({ fence, markdown, container });
+ block = { info, prefix: openingPrefix, container: openingContainer, start: match.index + match[0].length };
+ stack.push({ fence, markdown, container: openingContainer });
continue;
}
@@ -181,7 +216,7 @@ export function replaceMarkdownCodeFences(value, renderBlock, { streaming = fals
if (!stack.length) {
output.push(block.prefix + renderBlock(
block.info,
- normalizeContainerCode(source.slice(block.start, match.index), block.prefix),
+ normalizeContainerCode(source.slice(block.start, match.index), block.container),
));
// Leave the closing line's newline for the surrounding Markdown.
cursor = match.index + match[0].replace(/\r?\n$/, '').length;
@@ -206,7 +241,7 @@ export function replaceMarkdownCodeFences(value, renderBlock, { streaming = fals
if (block) output.push(block.prefix + renderBlock(
block.info,
- normalizeContainerCode(source.slice(block.start), block.prefix),
+ normalizeContainerCode(source.slice(block.start), block.container),
));
else output.push(source.slice(cursor));
return output.join('');
diff --git a/test/markdown-render.mjs b/test/markdown-render.mjs
index 2adf129e6..44dab7362 100644
--- a/test/markdown-render.mjs
+++ b/test/markdown-render.mjs
@@ -85,7 +85,7 @@ 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\nOutside\n```python\nother\n```\nAfter';
+ const source = '```markdown\n```js\nconst value = true;\n```\n\nOutside\n```\nother\n```\nAfter';
const blocks = [];
const remaining = helpers.replaceMarkdownCodeFences(source, (info, code) => {
blocks.push({ info, code });
@@ -93,7 +93,7 @@ for (const build of ['chrome', 'firefox']) {
});
assert.deepEqual(blocks, [
{ info: 'markdown', code: '```js\nconst value = true;\n' },
- { info: 'python', code: 'other\n' },
+ { info: '', code: 'other\n' },
]);
assert.equal(remaining, 'BLOCK\n\nOutside\nBLOCK\nAfter');
assert.match(formatMarkdown(source), /Outside/);
@@ -186,6 +186,19 @@ for (const build of ['chrome', 'firefox']) {
assert.match(formatMarkdown(source), /After/);
});
+ 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/);
+ });
+
test(`${build}: tab-indented list fences use visual indentation columns`, () => {
const source = '-\t```text\n\tvalue\n\t```\nAfter';
const blocks = [];