Skip to content

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

Preserve Markdown fence boundary newlines

Emre Sokullub2422589a208
diff --git a/src/chrome/src/ui/markdown-render.js b/src/chrome/src/ui/markdown-render.js
index 9bfbbca7f..35c0680aa 100644
--- a/src/chrome/src/ui/markdown-render.js
+++ b/src/chrome/src/ui/markdown-render.js
@@ -225,7 +225,7 @@ function unfinishedContainerEnd(source, start, container) {
pendingBlankStart = null;
offset += match[0].length;
}
- return pendingBlankStart ?? source.length;
+ return source.length;
}
function normalizeContainerCode(code, prefixOrContainer) {
@@ -292,7 +292,8 @@ export function replaceMarkdownCodeFences(value, renderBlock, { streaming = fals
if (boundary <= match.index) {
const wasClosingFence = isFenceCloser(stack[stack.length - 1], container, fence, info);
const code = source.slice(block.start, boundary);
- const needsBoundaryNewline = /\r?\n$/.test(code) && !/^\r?\n/.test(source.slice(boundary));
+ const needsBoundaryNewline = (/\r?\n$/.test(code) || (!code && block.openingEndsWithNewline))
+ && !/^\r?\n/.test(source.slice(boundary));
output.push(block.prefix + renderBlock(
block.info,
normalizeContainerCode(code, block.container),
@@ -328,6 +329,7 @@ export function replaceMarkdownCodeFences(value, renderBlock, { streaming = fals
prefix: openingPrefix,
container: openingContainer,
start,
+ openingEndsWithNewline: /\r?\n$/.test(match.raw),
boundary: unfinishedContainerEnd(source, start, openingContainer),
};
stack.push({ fence, markdown, container: openingContainer });
@@ -362,7 +364,8 @@ export function replaceMarkdownCodeFences(value, renderBlock, { streaming = fals
const end = block.boundary;
const code = source.slice(block.start, end);
const tail = source.slice(end);
- const needsBoundaryNewline = /\r?\n$/.test(code) && !/^\r?\n/.test(tail);
+ const needsBoundaryNewline = tail && (/\r?\n$/.test(code) || (!code && block.openingEndsWithNewline))
+ && !/^\r?\n/.test(tail);
output.push(block.prefix + renderBlock(
block.info,
normalizeContainerCode(code, block.container),
diff --git a/src/firefox/src/ui/markdown-render.js b/src/firefox/src/ui/markdown-render.js
index 9bfbbca7f..35c0680aa 100644
--- a/src/firefox/src/ui/markdown-render.js
+++ b/src/firefox/src/ui/markdown-render.js
@@ -225,7 +225,7 @@ function unfinishedContainerEnd(source, start, container) {
pendingBlankStart = null;
offset += match[0].length;
}
- return pendingBlankStart ?? source.length;
+ return source.length;
}
function normalizeContainerCode(code, prefixOrContainer) {
@@ -292,7 +292,8 @@ export function replaceMarkdownCodeFences(value, renderBlock, { streaming = fals
if (boundary <= match.index) {
const wasClosingFence = isFenceCloser(stack[stack.length - 1], container, fence, info);
const code = source.slice(block.start, boundary);
- const needsBoundaryNewline = /\r?\n$/.test(code) && !/^\r?\n/.test(source.slice(boundary));
+ const needsBoundaryNewline = (/\r?\n$/.test(code) || (!code && block.openingEndsWithNewline))
+ && !/^\r?\n/.test(source.slice(boundary));
output.push(block.prefix + renderBlock(
block.info,
normalizeContainerCode(code, block.container),
@@ -328,6 +329,7 @@ export function replaceMarkdownCodeFences(value, renderBlock, { streaming = fals
prefix: openingPrefix,
container: openingContainer,
start,
+ openingEndsWithNewline: /\r?\n$/.test(match.raw),
boundary: unfinishedContainerEnd(source, start, openingContainer),
};
stack.push({ fence, markdown, container: openingContainer });
@@ -362,7 +364,8 @@ export function replaceMarkdownCodeFences(value, renderBlock, { streaming = fals
const end = block.boundary;
const code = source.slice(block.start, end);
const tail = source.slice(end);
- const needsBoundaryNewline = /\r?\n$/.test(code) && !/^\r?\n/.test(tail);
+ const needsBoundaryNewline = tail && (/\r?\n$/.test(code) || (!code && block.openingEndsWithNewline))
+ && !/^\r?\n/.test(tail);
output.push(block.prefix + renderBlock(
block.info,
normalizeContainerCode(code, block.container),
diff --git a/test/markdown-render.mjs b/test/markdown-render.mjs
index 4907acd71..817c91abd 100644
--- a/test/markdown-render.mjs
+++ b/test/markdown-render.mjs
@@ -328,6 +328,26 @@ for (const build of ['chrome', 'firefox']) {
}), '> BLOCK\nOutside');
assert.deepEqual(noCloserBlocks, [{ info: 'text', code: 'inside\n' }]);
+ for (const [source, expected] of [
+ ['> ```text\nOutside', '> BLOCK\nOutside'],
+ ['- ```text\nOutside', '- BLOCK\nOutside'],
+ ]) {
+ const emptyBlocks = [];
+ assert.equal(helpers.replaceMarkdownCodeFences(source, (info, code) => {
+ emptyBlocks.push({ info, code });
+ return 'BLOCK';
+ }), expected);
+ assert.deepEqual(emptyBlocks, [{ info: 'text', code: '' }]);
+ }
+
+ const trailingQuoteBlank = '> ```text\n> hello\n>\n';
+ const trailingQuoteBlocks = [];
+ assert.equal(helpers.replaceMarkdownCodeFences(trailingQuoteBlank, (info, code) => {
+ trailingQuoteBlocks.push({ info, code });
+ return 'BLOCK';
+ }), '> BLOCK');
+ assert.deepEqual(trailingQuoteBlocks, [{ info: 'text', code: 'hello\n\n' }]);
+
const escapedQuote = '> ```text\n> inside\nOutside\n> ```\nAfter';
const quoteBlocks = [];
assert.equal(helpers.replaceMarkdownCodeFences(escapedQuote, (info, code) => {