Skip to content

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

Cache Markdown fence container boundaries

Emre Sokullu698a4db47bee
diff --git a/src/chrome/src/ui/markdown-render.js b/src/chrome/src/ui/markdown-render.js
index a7413080f..7b38fec4f 100644
--- a/src/chrome/src/ui/markdown-render.js
+++ b/src/chrome/src/ui/markdown-render.js
@@ -265,9 +265,8 @@ export function replaceMarkdownCodeFences(value, renderBlock, { streaming = fals
const { prefix, indentation, fence, info } = match;
const container = fenceContainer(prefix, indentation);
if (block) {
- const activeContainer = stack[stack.length - 1].container;
- const boundary = unfinishedContainerEnd(source, block.start, activeContainer);
- if (boundary < match.index) {
+ const boundary = block.boundary;
+ 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));
@@ -296,7 +295,14 @@ export function replaceMarkdownCodeFences(value, renderBlock, { streaming = fals
openingPrefix = `${prefix}${indentation}`;
}
output.push(source.slice(cursor, match.index));
- block = { info, prefix: openingPrefix, container: openingContainer, start: match.index + match.raw.length };
+ const start = match.index + match.raw.length;
+ block = {
+ info,
+ prefix: openingPrefix,
+ container: openingContainer,
+ start,
+ boundary: unfinishedContainerEnd(source, start, openingContainer),
+ };
stack.push({ fence, markdown, container: openingContainer });
continue;
}
@@ -325,7 +331,7 @@ export function replaceMarkdownCodeFences(value, renderBlock, { streaming = fals
}
if (block) {
- const end = unfinishedContainerEnd(source, block.start, block.container);
+ const end = block.boundary;
output.push(block.prefix + renderBlock(
block.info,
normalizeContainerCode(source.slice(block.start, end), block.container),
diff --git a/src/firefox/src/ui/markdown-render.js b/src/firefox/src/ui/markdown-render.js
index a7413080f..7b38fec4f 100644
--- a/src/firefox/src/ui/markdown-render.js
+++ b/src/firefox/src/ui/markdown-render.js
@@ -265,9 +265,8 @@ export function replaceMarkdownCodeFences(value, renderBlock, { streaming = fals
const { prefix, indentation, fence, info } = match;
const container = fenceContainer(prefix, indentation);
if (block) {
- const activeContainer = stack[stack.length - 1].container;
- const boundary = unfinishedContainerEnd(source, block.start, activeContainer);
- if (boundary < match.index) {
+ const boundary = block.boundary;
+ 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));
@@ -296,7 +295,14 @@ export function replaceMarkdownCodeFences(value, renderBlock, { streaming = fals
openingPrefix = `${prefix}${indentation}`;
}
output.push(source.slice(cursor, match.index));
- block = { info, prefix: openingPrefix, container: openingContainer, start: match.index + match.raw.length };
+ const start = match.index + match.raw.length;
+ block = {
+ info,
+ prefix: openingPrefix,
+ container: openingContainer,
+ start,
+ boundary: unfinishedContainerEnd(source, start, openingContainer),
+ };
stack.push({ fence, markdown, container: openingContainer });
continue;
}
@@ -325,7 +331,7 @@ export function replaceMarkdownCodeFences(value, renderBlock, { streaming = fals
}
if (block) {
- const end = unfinishedContainerEnd(source, block.start, block.container);
+ const end = block.boundary;
output.push(block.prefix + renderBlock(
block.info,
normalizeContainerCode(source.slice(block.start, end), block.container),
diff --git a/test/markdown-render.mjs b/test/markdown-render.mjs
index afcde325c..b97e9937b 100644
--- a/test/markdown-render.mjs
+++ b/test/markdown-render.mjs
@@ -265,6 +265,17 @@ for (const build of ['chrome', 'firefox']) {
{ info: 'js', code: 'code\n' },
]);
+ const adjacentBlock = '> ```text\n> inside\n```js\ncode\n```\nAfter';
+ const adjacentBlocks = [];
+ assert.equal(helpers.replaceMarkdownCodeFences(adjacentBlock, (info, code) => {
+ adjacentBlocks.push({ info, code });
+ return 'BLOCK';
+ }), '> BLOCK\nBLOCK\nAfter');
+ assert.deepEqual(adjacentBlocks, [
+ { info: 'text', code: 'inside\n' },
+ { info: 'js', code: 'code\n' },
+ ]);
+
const quotedList = '> - ```text\n> hello\n>\n> again\n> ```';
const quotedListBlocks = [];
assert.equal(helpers.replaceMarkdownCodeFences(quotedList, (info, code) => {