Skip to content

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

Preserve Markdown quote continuation prefixes

Emre Sokullu08f90ee6ca55
diff --git a/src/chrome/src/ui/markdown-render.js b/src/chrome/src/ui/markdown-render.js
index 47f8c8cf8..ed6270555 100644
--- a/src/chrome/src/ui/markdown-render.js
+++ b/src/chrome/src/ui/markdown-render.js
@@ -86,6 +86,7 @@ function fenceContainer(prefix, indentation = '') {
remainder = remainder.slice(list.length);
}
return {
+ containerPrefix: source.slice(0, source.length - remainder.length),
quotePrefix,
quoteDepth,
listPrefix,
@@ -327,7 +328,7 @@ function listContinuationContainer(source, position, prefix, indentation, noList
if (precedingContainer.listPrefix) {
const container = missingQuotes
- ? fenceContainer(`${precedingContainer.rawPrefix}${'> '.repeat(missingQuotes)}`)
+ ? fenceContainer(`${precedingContainer.containerPrefix}${'> '.repeat(missingQuotes)}`)
: precedingContainer;
const listIndent = container.listIndentGroups.at(-1);
if (listIndent && continuationIndent >= listIndent && continuationIndent <= listIndent + 3) {
@@ -385,7 +386,9 @@ function unfinishedContainerEnd(source, start, container, boundaryCache) {
if (!match[0]) break;
const line = match[0].replace(/\r?\n$/, '');
if ((container.quoteDepth || container.listPrefix) && lineIsBlankInContainer(line, container)) {
- if (lineHasExplicitContainerPrefix(line, container)) pendingBlankStart = null;
+ if (lineHasExplicitContainerPrefix(line, container)) {
+ if (!container.quoteDepth) pendingBlankStart = null;
+ }
else if (pendingBlankStart == null) pendingBlankStart = offset;
offset += match[0].length;
continue;
diff --git a/src/firefox/src/ui/markdown-render.js b/src/firefox/src/ui/markdown-render.js
index 47f8c8cf8..ed6270555 100644
--- a/src/firefox/src/ui/markdown-render.js
+++ b/src/firefox/src/ui/markdown-render.js
@@ -86,6 +86,7 @@ function fenceContainer(prefix, indentation = '') {
remainder = remainder.slice(list.length);
}
return {
+ containerPrefix: source.slice(0, source.length - remainder.length),
quotePrefix,
quoteDepth,
listPrefix,
@@ -327,7 +328,7 @@ function listContinuationContainer(source, position, prefix, indentation, noList
if (precedingContainer.listPrefix) {
const container = missingQuotes
- ? fenceContainer(`${precedingContainer.rawPrefix}${'> '.repeat(missingQuotes)}`)
+ ? fenceContainer(`${precedingContainer.containerPrefix}${'> '.repeat(missingQuotes)}`)
: precedingContainer;
const listIndent = container.listIndentGroups.at(-1);
if (listIndent && continuationIndent >= listIndent && continuationIndent <= listIndent + 3) {
@@ -385,7 +386,9 @@ function unfinishedContainerEnd(source, start, container, boundaryCache) {
if (!match[0]) break;
const line = match[0].replace(/\r?\n$/, '');
if ((container.quoteDepth || container.listPrefix) && lineIsBlankInContainer(line, container)) {
- if (lineHasExplicitContainerPrefix(line, container)) pendingBlankStart = null;
+ if (lineHasExplicitContainerPrefix(line, container)) {
+ if (!container.quoteDepth) pendingBlankStart = null;
+ }
else if (pendingBlankStart == null) pendingBlankStart = offset;
offset += match[0].length;
continue;
diff --git a/test/markdown-render.mjs b/test/markdown-render.mjs
index e9b5ce69f..494bbf30a 100644
--- a/test/markdown-render.mjs
+++ b/test/markdown-render.mjs
@@ -413,6 +413,14 @@ for (const build of ['chrome', 'firefox']) {
}), '> - BLOCK\n> second\n> ```\nAfter');
assert.deepEqual(mixedQuoteListBlocks, [{ info: 'text', code: 'first\n' }]);
+ const quotedBlankReset = '> ```text\n> first\n\n>\n> second\n> ```\nAfter';
+ const quotedBlankResetBlocks = [];
+ assert.equal(helpers.replaceMarkdownCodeFences(quotedBlankReset, (info, code) => {
+ quotedBlankResetBlocks.push({ info, code });
+ return 'BLOCK';
+ }), '> BLOCK\n>\n> second\n> ```\nAfter');
+ assert.deepEqual(quotedBlankResetBlocks, [{ info: 'text', code: 'first\n' }]);
+
const wideQuotedListBlank = '10. > ```text\n > hello\n >\n > again\n > ```';
const wideQuotedListBlocks = [];
assert.equal(helpers.replaceMarkdownCodeFences(wideQuotedListBlank, (info, code) => {
@@ -421,6 +429,14 @@ for (const build of ['chrome', 'firefox']) {
}), '10. > BLOCK');
assert.deepEqual(wideQuotedListBlocks, [{ info: 'text', code: 'hello\n\nagain\n' }]);
+ const quoteListContinuation = '- item\n > ```text\n > hello\n > ```\nAfter';
+ const quoteListContinuationBlocks = [];
+ assert.equal(helpers.replaceMarkdownCodeFences(quoteListContinuation, (info, code) => {
+ quoteListContinuationBlocks.push({ info, code });
+ return 'BLOCK';
+ }), '- item\n > BLOCK\nAfter');
+ assert.deepEqual(quoteListContinuationBlocks, [{ info: 'text', code: 'hello\n' }]);
+
const noCloser = '> ```text\n> inside\nOutside';
const noCloserBlocks = [];
assert.equal(helpers.replaceMarkdownCodeFences(noCloser, (info, code) => {