Skip to content

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

Scan Markdown fence blank lines once

Emre Sokullu87c0dbc7c66c
diff --git a/src/chrome/src/ui/markdown-render.js b/src/chrome/src/ui/markdown-render.js
index 0b55570fc..0a209a1a0 100644
--- a/src/chrome/src/ui/markdown-render.js
+++ b/src/chrome/src/ui/markdown-render.js
@@ -202,31 +202,24 @@ function lineIsBlankInContainer(line, container) {
return quoteDepth === container.quoteDepth && !line.slice(quotePrefix.length).trim();
}
-function followingLineBelongsToContainer(remainder, offset, container) {
- for (const match of remainder.slice(offset).matchAll(/[^\r\n]*(?:\r?\n|$)/g)) {
- if (!match[0]) break;
- const line = match[0].replace(/\r?\n$/, '');
- if (line.trim()) return lineBelongsToContainer(line, container);
- }
- return false;
-}
-
function unfinishedContainerEnd(source, start, container) {
if (!container.quoteDepth && !container.listPrefix) return source.length;
const remainder = source.slice(start);
let offset = start;
+ let pendingBlankStart = null;
for (const match of remainder.matchAll(/[^\r\n]*(?:\r?\n|$)/g)) {
if (!match[0]) break;
const line = match[0].replace(/\r?\n$/, '');
- if ((container.quoteDepth || container.listPrefix) && lineIsBlankInContainer(line, container)
- && followingLineBelongsToContainer(remainder, match.index + match[0].length, container)) {
+ if ((container.quoteDepth || container.listPrefix) && lineIsBlankInContainer(line, container)) {
+ if (pendingBlankStart == null) pendingBlankStart = offset;
offset += match[0].length;
continue;
}
- if (!lineBelongsToContainer(line, container)) return offset;
+ if (!lineBelongsToContainer(line, container)) return pendingBlankStart ?? offset;
+ pendingBlankStart = null;
offset += match[0].length;
}
- return source.length;
+ return pendingBlankStart ?? source.length;
}
function normalizeContainerCode(code, prefixOrContainer) {
diff --git a/src/firefox/src/ui/markdown-render.js b/src/firefox/src/ui/markdown-render.js
index 0b55570fc..0a209a1a0 100644
--- a/src/firefox/src/ui/markdown-render.js
+++ b/src/firefox/src/ui/markdown-render.js
@@ -202,31 +202,24 @@ function lineIsBlankInContainer(line, container) {
return quoteDepth === container.quoteDepth && !line.slice(quotePrefix.length).trim();
}
-function followingLineBelongsToContainer(remainder, offset, container) {
- for (const match of remainder.slice(offset).matchAll(/[^\r\n]*(?:\r?\n|$)/g)) {
- if (!match[0]) break;
- const line = match[0].replace(/\r?\n$/, '');
- if (line.trim()) return lineBelongsToContainer(line, container);
- }
- return false;
-}
-
function unfinishedContainerEnd(source, start, container) {
if (!container.quoteDepth && !container.listPrefix) return source.length;
const remainder = source.slice(start);
let offset = start;
+ let pendingBlankStart = null;
for (const match of remainder.matchAll(/[^\r\n]*(?:\r?\n|$)/g)) {
if (!match[0]) break;
const line = match[0].replace(/\r?\n$/, '');
- if ((container.quoteDepth || container.listPrefix) && lineIsBlankInContainer(line, container)
- && followingLineBelongsToContainer(remainder, match.index + match[0].length, container)) {
+ if ((container.quoteDepth || container.listPrefix) && lineIsBlankInContainer(line, container)) {
+ if (pendingBlankStart == null) pendingBlankStart = offset;
offset += match[0].length;
continue;
}
- if (!lineBelongsToContainer(line, container)) return offset;
+ if (!lineBelongsToContainer(line, container)) return pendingBlankStart ?? offset;
+ pendingBlankStart = null;
offset += match[0].length;
}
- return source.length;
+ return pendingBlankStart ?? source.length;
}
function normalizeContainerCode(code, prefixOrContainer) {
diff --git a/test/markdown-render.mjs b/test/markdown-render.mjs
index cd8133754..9742be9ab 100644
--- a/test/markdown-render.mjs
+++ b/test/markdown-render.mjs
@@ -286,13 +286,13 @@ for (const build of ['chrome', 'firefox']) {
}), ' > - BLOCK\n > Outside\n\nAfter');
assert.deepEqual(quoteListBlocks, [{ info: 'text', code: 'hello\n' }]);
- const quoteBlank = '> ```text\n> hello\n>\n> again\n> ```';
+ const quoteBlank = '> ```text\n> hello\n>\n>\n> again\n> ```';
const quoteBlankBlocks = [];
assert.equal(helpers.replaceMarkdownCodeFences(quoteBlank, (info, code) => {
quoteBlankBlocks.push({ info, code });
return 'BLOCK';
}), '> BLOCK');
- assert.deepEqual(quoteBlankBlocks, [{ info: 'text', code: 'hello\n\nagain\n' }]);
+ assert.deepEqual(quoteBlankBlocks, [{ info: 'text', code: 'hello\n\n\nagain\n' }]);
const noCloser = '> ```text\n> inside\nOutside';
const noCloserBlocks = [];