Skip to content

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

Preserve fenced blocks across final renders

Emre Sokullu4dd8fea9d0c0
diff --git a/src/chrome/src/ui/markdown-render.js b/src/chrome/src/ui/markdown-render.js
index 0cb7a92fc..57f8ea44c 100644
--- a/src/chrome/src/ui/markdown-render.js
+++ b/src/chrome/src/ui/markdown-render.js
@@ -217,6 +217,22 @@ export function replaceMarkdownCodeFences(value, renderBlock, { streaming = fals
for (let matchIndex = 0; matchIndex < matches.length; matchIndex += 1) {
const match = matches[matchIndex];
const [, prefix, indentation, fence, info] = match;
+ if (block) {
+ const activeContainer = stack[stack.length - 1].container;
+ const boundary = unfinishedContainerEnd(source, block.start, activeContainer);
+ if (boundary < match.index) {
+ const code = source.slice(block.start, boundary);
+ const needsBoundaryNewline = /\r?\n$/.test(code) && !/^\r?\n/.test(source.slice(boundary));
+ output.push(block.prefix + renderBlock(
+ block.info,
+ normalizeContainerCode(code, block.container),
+ ) + (needsBoundaryNewline ? '\n' : ''));
+ cursor = boundary;
+ block = null;
+ stack.length = 0;
+ continue;
+ }
+ }
const container = fenceContainer(prefix, indentation);
const validOpening = fence[0] !== '`' || !info.includes('`');
const markdown = /^(?:md|markdown)$/i.test(codeFenceLanguage(info));
diff --git a/src/chrome/src/ui/sidepanel.js b/src/chrome/src/ui/sidepanel.js
index 219a57794..22bdaba94 100644
--- a/src/chrome/src/ui/sidepanel.js
+++ b/src/chrome/src/ui/sidepanel.js
@@ -12826,7 +12826,7 @@ function scheduleMathRender() {
function formatMarkdown(text, options = {}) {
if (!text) return '';
const enhance = options.enhance !== false;
- const streaming = options.streaming === true || !enhance;
+ const streaming = options.recoverNestedMarkdown !== false;
// 1. Extract fenced code blocks BEFORE escaping HTML
const codeBlocks = [];
diff --git a/src/firefox/src/ui/markdown-render.js b/src/firefox/src/ui/markdown-render.js
index 0cb7a92fc..57f8ea44c 100644
--- a/src/firefox/src/ui/markdown-render.js
+++ b/src/firefox/src/ui/markdown-render.js
@@ -217,6 +217,22 @@ export function replaceMarkdownCodeFences(value, renderBlock, { streaming = fals
for (let matchIndex = 0; matchIndex < matches.length; matchIndex += 1) {
const match = matches[matchIndex];
const [, prefix, indentation, fence, info] = match;
+ if (block) {
+ const activeContainer = stack[stack.length - 1].container;
+ const boundary = unfinishedContainerEnd(source, block.start, activeContainer);
+ if (boundary < match.index) {
+ const code = source.slice(block.start, boundary);
+ const needsBoundaryNewline = /\r?\n$/.test(code) && !/^\r?\n/.test(source.slice(boundary));
+ output.push(block.prefix + renderBlock(
+ block.info,
+ normalizeContainerCode(code, block.container),
+ ) + (needsBoundaryNewline ? '\n' : ''));
+ cursor = boundary;
+ block = null;
+ stack.length = 0;
+ continue;
+ }
+ }
const container = fenceContainer(prefix, indentation);
const validOpening = fence[0] !== '`' || !info.includes('`');
const markdown = /^(?:md|markdown)$/i.test(codeFenceLanguage(info));
diff --git a/src/firefox/src/ui/sidepanel.js b/src/firefox/src/ui/sidepanel.js
index ef495468e..2566a173e 100644
--- a/src/firefox/src/ui/sidepanel.js
+++ b/src/firefox/src/ui/sidepanel.js
@@ -12413,7 +12413,7 @@ function scheduleMathRender() {
function formatMarkdown(text, options = {}) {
if (!text) return '';
const enhance = options.enhance !== false;
- const streaming = options.streaming === true || !enhance;
+ const streaming = options.recoverNestedMarkdown !== false;
// 1. Extract fenced code blocks BEFORE escaping HTML
const codeBlocks = [];
diff --git a/test/markdown-render.mjs b/test/markdown-render.mjs
index 86a658fe6..7b54cfa7e 100644
--- a/test/markdown-render.mjs
+++ b/test/markdown-render.mjs
@@ -228,6 +228,14 @@ for (const build of ['chrome', 'firefox']) {
return 'BLOCK';
}), '- BLOCK');
assert.deepEqual(listBlocks, [{ info: 'text', code: 'hello\n\nagain' }]);
+
+ const escapedQuote = '> ```text\n> inside\nOutside\n> ```\nAfter';
+ const quoteBlocks = [];
+ assert.equal(helpers.replaceMarkdownCodeFences(escapedQuote, (info, code) => {
+ quoteBlocks.push({ info, code });
+ return 'BLOCK';
+ }), '> BLOCK\nOutside\n> ```\nAfter');
+ assert.deepEqual(quoteBlocks, [{ info: 'text', code: 'inside\n' }]);
});
test(`${build}: tab-indented list fences use visual indentation columns`, () => {