Skip to content

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

Support wide list quote fences

Emre Sokullub40b007630a7
diff --git a/src/chrome/src/ui/markdown-render.js b/src/chrome/src/ui/markdown-render.js
index 7e27938b7..9bfbbca7f 100644
--- a/src/chrome/src/ui/markdown-render.js
+++ b/src/chrome/src/ui/markdown-render.js
@@ -59,13 +59,13 @@ export function codeFenceLanguage(infoString) {
function fenceContainer(prefix, indentation = '') {
const source = String(prefix);
- const quotePrefix = source.match(/^(?: {0,3}>[ \t]?)+/)?.[0] || '';
+ const quotePrefix = source.match(/^(?:[ \t]*>[ \t]?)+/)?.[0] || '';
let remainder = source;
let listPrefix = '';
let quoteDepth = 0;
const listIndentGroups = [0];
while (remainder) {
- const quote = remainder.match(/^ {0,3}>[ \t]?/);
+ const quote = remainder.match(/^[ \t]*>[ \t]?/);
if (quote) {
quoteDepth += 1;
listIndentGroups.push(0);
@@ -83,6 +83,7 @@ function fenceContainer(prefix, indentation = '') {
quoteDepth,
listPrefix,
listIndentGroups,
+ leadingQuoteIndent: indentationColumns(source.match(/^[ \t]*(?=>)/)?.[0] || ''),
rawPrefix: source,
indentation: String(indentation),
};
@@ -149,7 +150,10 @@ function isFenceCloser(opener, candidate, fence, info) {
function listContinuationContainer(source, position, prefix, indentation) {
const current = fenceContainer(prefix, indentation);
- const continuationIndent = indentationColumns(indentation);
+ const continuationIndent = Math.max(
+ indentationColumns(indentation),
+ current.leadingQuoteIndent,
+ );
let lineEnd = position;
if (source[lineEnd - 1] === '\n') lineEnd -= 1;
if (source[lineEnd - 1] === '\r') lineEnd -= 1;
@@ -164,14 +168,15 @@ function listContinuationContainer(source, position, prefix, indentation) {
if (source[lineEnd] === '\r') lineEnd -= 1;
continue;
}
- const quotePrefix = line.match(/^(?: {0,3}>[ \t]?)+/)?.[0] || '';
+ const quotePrefix = line.match(/^(?:[ \t]*>[ \t]?)+/)?.[0] || '';
const quoteDepth = (quotePrefix.match(/>/g) || []).length;
- if (quoteDepth !== current.quoteDepth) break;
+ const missingQuotes = current.quoteDepth - quoteDepth;
+ if (missingQuotes < 0 || (missingQuotes && !current.leadingQuoteIndent)) break;
const content = line.slice(quotePrefix.length);
const listPrefix = content.match(/^(?:[ \t]*(?:[-+*]|\d+[.)])[ \t]+)+/)?.[0] || '';
if (listPrefix) {
- const container = fenceContainer(`${quotePrefix}${listPrefix}`);
+ const container = fenceContainer(`${quotePrefix}${listPrefix}${'> '.repeat(missingQuotes)}`);
const listIndent = indentationColumns(container.listPrefix);
if (listIndent && continuationIndent >= listIndent && continuationIndent <= listIndent + 3) {
return container;
@@ -199,9 +204,8 @@ function lineBelongsToContainer(line, container) {
}
function lineIsBlankInContainer(line, container) {
- const quotePrefix = line.match(/^(?: {0,3}>[ \t]?)+/)?.[0] || '';
- const quoteDepth = (quotePrefix.match(/>/g) || []).length;
- return quoteDepth === container.quoteDepth && !line.slice(quotePrefix.length).trim();
+ if (!container.quoteDepth && !container.listPrefix) return !line.trim();
+ return stripContainerPrefix(line, container) === '';
}
function unfinishedContainerEnd(source, start, container) {
@@ -243,7 +247,7 @@ function parseFenceLine(line) {
let offset = 0;
while (offset < line.length) {
const segment = line.slice(offset);
- const quote = segment.match(/^ {0,3}>[ \t]?/);
+ const quote = segment.match(/^[ \t]*>[ \t]?/);
if (quote) {
offset += quote[0].length;
continue;
@@ -308,13 +312,13 @@ export function replaceMarkdownCodeFences(value, renderBlock, { streaming = fals
let openingContainer = container;
let openingPrefix = prefix;
if (!validOpening) continue;
- const continuation = indentationColumns(container.indentation)
+ const continuation = (indentationColumns(container.indentation) || container.leadingQuoteIndent)
? listContinuationContainer(source, match.index, prefix, indentation)
: null;
if (continuation) {
openingContainer = continuation;
openingPrefix = `${prefix}${indentation}`;
- } else if (indentationColumns(container.indentation) > 3) {
+ } else if (indentationColumns(container.indentation) > 3 || container.leadingQuoteIndent > 3) {
continue;
}
output.push(source.slice(cursor, match.index));
diff --git a/src/firefox/src/ui/markdown-render.js b/src/firefox/src/ui/markdown-render.js
index 7e27938b7..9bfbbca7f 100644
--- a/src/firefox/src/ui/markdown-render.js
+++ b/src/firefox/src/ui/markdown-render.js
@@ -59,13 +59,13 @@ export function codeFenceLanguage(infoString) {
function fenceContainer(prefix, indentation = '') {
const source = String(prefix);
- const quotePrefix = source.match(/^(?: {0,3}>[ \t]?)+/)?.[0] || '';
+ const quotePrefix = source.match(/^(?:[ \t]*>[ \t]?)+/)?.[0] || '';
let remainder = source;
let listPrefix = '';
let quoteDepth = 0;
const listIndentGroups = [0];
while (remainder) {
- const quote = remainder.match(/^ {0,3}>[ \t]?/);
+ const quote = remainder.match(/^[ \t]*>[ \t]?/);
if (quote) {
quoteDepth += 1;
listIndentGroups.push(0);
@@ -83,6 +83,7 @@ function fenceContainer(prefix, indentation = '') {
quoteDepth,
listPrefix,
listIndentGroups,
+ leadingQuoteIndent: indentationColumns(source.match(/^[ \t]*(?=>)/)?.[0] || ''),
rawPrefix: source,
indentation: String(indentation),
};
@@ -149,7 +150,10 @@ function isFenceCloser(opener, candidate, fence, info) {
function listContinuationContainer(source, position, prefix, indentation) {
const current = fenceContainer(prefix, indentation);
- const continuationIndent = indentationColumns(indentation);
+ const continuationIndent = Math.max(
+ indentationColumns(indentation),
+ current.leadingQuoteIndent,
+ );
let lineEnd = position;
if (source[lineEnd - 1] === '\n') lineEnd -= 1;
if (source[lineEnd - 1] === '\r') lineEnd -= 1;
@@ -164,14 +168,15 @@ function listContinuationContainer(source, position, prefix, indentation) {
if (source[lineEnd] === '\r') lineEnd -= 1;
continue;
}
- const quotePrefix = line.match(/^(?: {0,3}>[ \t]?)+/)?.[0] || '';
+ const quotePrefix = line.match(/^(?:[ \t]*>[ \t]?)+/)?.[0] || '';
const quoteDepth = (quotePrefix.match(/>/g) || []).length;
- if (quoteDepth !== current.quoteDepth) break;
+ const missingQuotes = current.quoteDepth - quoteDepth;
+ if (missingQuotes < 0 || (missingQuotes && !current.leadingQuoteIndent)) break;
const content = line.slice(quotePrefix.length);
const listPrefix = content.match(/^(?:[ \t]*(?:[-+*]|\d+[.)])[ \t]+)+/)?.[0] || '';
if (listPrefix) {
- const container = fenceContainer(`${quotePrefix}${listPrefix}`);
+ const container = fenceContainer(`${quotePrefix}${listPrefix}${'> '.repeat(missingQuotes)}`);
const listIndent = indentationColumns(container.listPrefix);
if (listIndent && continuationIndent >= listIndent && continuationIndent <= listIndent + 3) {
return container;
@@ -199,9 +204,8 @@ function lineBelongsToContainer(line, container) {
}
function lineIsBlankInContainer(line, container) {
- const quotePrefix = line.match(/^(?: {0,3}>[ \t]?)+/)?.[0] || '';
- const quoteDepth = (quotePrefix.match(/>/g) || []).length;
- return quoteDepth === container.quoteDepth && !line.slice(quotePrefix.length).trim();
+ if (!container.quoteDepth && !container.listPrefix) return !line.trim();
+ return stripContainerPrefix(line, container) === '';
}
function unfinishedContainerEnd(source, start, container) {
@@ -243,7 +247,7 @@ function parseFenceLine(line) {
let offset = 0;
while (offset < line.length) {
const segment = line.slice(offset);
- const quote = segment.match(/^ {0,3}>[ \t]?/);
+ const quote = segment.match(/^[ \t]*>[ \t]?/);
if (quote) {
offset += quote[0].length;
continue;
@@ -308,13 +312,13 @@ export function replaceMarkdownCodeFences(value, renderBlock, { streaming = fals
let openingContainer = container;
let openingPrefix = prefix;
if (!validOpening) continue;
- const continuation = indentationColumns(container.indentation)
+ const continuation = (indentationColumns(container.indentation) || container.leadingQuoteIndent)
? listContinuationContainer(source, match.index, prefix, indentation)
: null;
if (continuation) {
openingContainer = continuation;
openingPrefix = `${prefix}${indentation}`;
- } else if (indentationColumns(container.indentation) > 3) {
+ } else if (indentationColumns(container.indentation) > 3 || container.leadingQuoteIndent > 3) {
continue;
}
output.push(source.slice(cursor, match.index));
diff --git a/test/markdown-render.mjs b/test/markdown-render.mjs
index 660ad669f..4907acd71 100644
--- a/test/markdown-render.mjs
+++ b/test/markdown-render.mjs
@@ -141,6 +141,8 @@ for (const build of ['chrome', 'firefox']) {
assert.match(renderSkillMarkdown(source), /<strong>Between<\/strong>/);
const prose = 'Use ``` inline; this is not a block.\n ```js\nIndented example.';
assert.equal(helpers.replaceMarkdownCodeFences(prose, () => 'BLOCK'), prose);
+ const indentedQuote = ' > ```text\n > literal\n > ```';
+ assert.equal(helpers.replaceMarkdownCodeFences(indentedQuote, () => 'BLOCK'), indentedQuote);
});
test(`${build}: fences inside list and quote containers do not consume following prose`, () => {
@@ -310,6 +312,14 @@ for (const build of ['chrome', 'firefox']) {
}), '> BLOCK');
assert.deepEqual(quoteBlankBlocks, [{ info: 'text', code: 'hello\n\n\nagain\n' }]);
+ const wideQuotedListBlank = '10. > ```text\n > hello\n >\n > again\n > ```';
+ const wideQuotedListBlocks = [];
+ assert.equal(helpers.replaceMarkdownCodeFences(wideQuotedListBlank, (info, code) => {
+ wideQuotedListBlocks.push({ info, code });
+ return 'BLOCK';
+ }), '10. > BLOCK');
+ assert.deepEqual(wideQuotedListBlocks, [{ info: 'text', code: 'hello\n\nagain\n' }]);
+
const noCloser = '> ```text\n> inside\nOutside';
const noCloserBlocks = [];
assert.equal(helpers.replaceMarkdownCodeFences(noCloser, (info, code) => {