Skip to content

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

Distinguish streamed and completed nested fences

Emre Sokulluc92a21d29cae
diff --git a/src/chrome/src/ui/markdown-render.js b/src/chrome/src/ui/markdown-render.js
index 7f4739078..63060ba26 100644
--- a/src/chrome/src/ui/markdown-render.js
+++ b/src/chrome/src/ui/markdown-render.js
@@ -142,7 +142,7 @@ function normalizeContainerCode(code, prefix) {
}
/** Replace whole fenced blocks, including an unfinished block during streaming. */
-export function replaceMarkdownCodeFences(value, renderBlock) {
+export function replaceMarkdownCodeFences(value, renderBlock, { streaming = false } = {}) {
const source = String(value ?? '');
// Accept fences at the document root and inside the simple containers this
// renderer preserves (lists and blockquotes). The old unanchored matcher
@@ -174,11 +174,7 @@ export function replaceMarkdownCodeFences(value, renderBlock) {
const active = stack[stack.length - 1];
// A closing fence occupies its own line, has no info string, and is at
// least as long as its opener. Backticks inside source code are literal.
- const hasImmediatelyFollowingText = /^[ \t]*[^\r\n]/.test(
- source.slice(match.index + match[0].length),
- );
- if (isFenceCloser(active, container, fence, info)
- && (!active.pendingNested || hasImmediatelyFollowingText)) {
+ if (isFenceCloser(active, container, fence, info)) {
stack.pop();
if (!stack.length) {
output.push(block.prefix + renderBlock(
@@ -194,12 +190,8 @@ export function replaceMarkdownCodeFences(value, renderBlock) {
// Models sometimes wrap a README in ```markdown and reuse ```lang
// inside it. Recover only this named Markdown nesting; ordinary code
// and correctly longer outer fences retain their literal contents.
- if (nestedFenceHasOwnCloser(matches, matchIndex, active)) {
+ if (streaming || nestedFenceHasOwnCloser(matches, matchIndex, active)) {
stack.push({ fence, markdown, container });
- } else {
- // During streaming, the only available blank fence may close this
- // example rather than its still-open Markdown wrapper.
- active.pendingNested = true;
}
}
}
diff --git a/src/chrome/src/ui/sidepanel.js b/src/chrome/src/ui/sidepanel.js
index 5964754db..97ac05cce 100644
--- a/src/chrome/src/ui/sidepanel.js
+++ b/src/chrome/src/ui/sidepanel.js
@@ -12834,7 +12834,7 @@ function formatMarkdown(text, options = {}) {
const id = `__CODEBLOCK_${codeBlocks.length}__`;
codeBlocks.push({ lang: lang || '', code });
return id;
- });
+ }, { streaming: !enhance });
// 2. Extract inline code before escaping
const inlineCodes = [];
diff --git a/src/chrome/src/ui/skill-markdown.js b/src/chrome/src/ui/skill-markdown.js
index 2dd0f4ac6..d5c3c93c8 100644
--- a/src/chrome/src/ui/skill-markdown.js
+++ b/src/chrome/src/ui/skill-markdown.js
@@ -42,7 +42,7 @@ export function renderSkillMarkdown(content) {
const placeholder = `__SKILL_CODE_BLOCK_${codeBlocks.length}__`;
codeBlocks.push(code);
return placeholder;
- });
+ }, { streaming: true });
const inlineCodes = [];
text = text.replace(/`([^`\n]+)`/g, (_match, code) => {
const placeholder = `__SKILL_INLINE_CODE_${inlineCodes.length}__`;
diff --git a/src/firefox/src/ui/markdown-render.js b/src/firefox/src/ui/markdown-render.js
index 7f4739078..63060ba26 100644
--- a/src/firefox/src/ui/markdown-render.js
+++ b/src/firefox/src/ui/markdown-render.js
@@ -142,7 +142,7 @@ function normalizeContainerCode(code, prefix) {
}
/** Replace whole fenced blocks, including an unfinished block during streaming. */
-export function replaceMarkdownCodeFences(value, renderBlock) {
+export function replaceMarkdownCodeFences(value, renderBlock, { streaming = false } = {}) {
const source = String(value ?? '');
// Accept fences at the document root and inside the simple containers this
// renderer preserves (lists and blockquotes). The old unanchored matcher
@@ -174,11 +174,7 @@ export function replaceMarkdownCodeFences(value, renderBlock) {
const active = stack[stack.length - 1];
// A closing fence occupies its own line, has no info string, and is at
// least as long as its opener. Backticks inside source code are literal.
- const hasImmediatelyFollowingText = /^[ \t]*[^\r\n]/.test(
- source.slice(match.index + match[0].length),
- );
- if (isFenceCloser(active, container, fence, info)
- && (!active.pendingNested || hasImmediatelyFollowingText)) {
+ if (isFenceCloser(active, container, fence, info)) {
stack.pop();
if (!stack.length) {
output.push(block.prefix + renderBlock(
@@ -194,12 +190,8 @@ export function replaceMarkdownCodeFences(value, renderBlock) {
// Models sometimes wrap a README in ```markdown and reuse ```lang
// inside it. Recover only this named Markdown nesting; ordinary code
// and correctly longer outer fences retain their literal contents.
- if (nestedFenceHasOwnCloser(matches, matchIndex, active)) {
+ if (streaming || nestedFenceHasOwnCloser(matches, matchIndex, active)) {
stack.push({ fence, markdown, container });
- } else {
- // During streaming, the only available blank fence may close this
- // example rather than its still-open Markdown wrapper.
- active.pendingNested = true;
}
}
}
diff --git a/src/firefox/src/ui/sidepanel.js b/src/firefox/src/ui/sidepanel.js
index c6ed35f98..53f2dcb40 100644
--- a/src/firefox/src/ui/sidepanel.js
+++ b/src/firefox/src/ui/sidepanel.js
@@ -12421,7 +12421,7 @@ function formatMarkdown(text, options = {}) {
const id = `__CODEBLOCK_${codeBlocks.length}__`;
codeBlocks.push({ lang: lang || '', code });
return id;
- });
+ }, { streaming: !enhance });
// 2. Extract inline code before escaping
const inlineCodes = [];
diff --git a/src/firefox/src/ui/skill-markdown.js b/src/firefox/src/ui/skill-markdown.js
index 2dd0f4ac6..d5c3c93c8 100644
--- a/src/firefox/src/ui/skill-markdown.js
+++ b/src/firefox/src/ui/skill-markdown.js
@@ -42,7 +42,7 @@ export function renderSkillMarkdown(content) {
const placeholder = `__SKILL_CODE_BLOCK_${codeBlocks.length}__`;
codeBlocks.push(code);
return placeholder;
- });
+ }, { streaming: true });
const inlineCodes = [];
text = text.replace(/`([^`\n]+)`/g, (_match, code) => {
const placeholder = `__SKILL_INLINE_CODE_${inlineCodes.length}__`;
diff --git a/test/markdown-render.mjs b/test/markdown-render.mjs
index 94b8058c1..f87ffaf6c 100644
--- a/test/markdown-render.mjs
+++ b/test/markdown-render.mjs
@@ -85,14 +85,14 @@ for (const build of ['chrome', 'firefox']) {
});
test(`${build}: unmatched nested example fences do not steal an outer closer`, () => {
- const source = '```markdown\n```js\nconst value = true;\n```\n## Outside';
+ const source = '```markdown\n```js\nconst value = true;\n```\n\n## Outside';
const blocks = [];
const remaining = helpers.replaceMarkdownCodeFences(source, (info, code) => {
blocks.push({ info, code });
return 'BLOCK';
});
assert.deepEqual(blocks, [{ info: 'markdown', code: '```js\nconst value = true;\n' }]);
- assert.equal(remaining, 'BLOCK\n## Outside');
+ assert.equal(remaining, 'BLOCK\n\n## Outside');
assert.match(formatMarkdown(source), /<h2>Outside<\/h2>/);
const incomplete = '```markdown\n```js\nconst value = true;\n```';
assert.deepEqual(preContents(formatMarkdown(incomplete, { enhance: false })), [escapeHtml('```js\nconst value = true;\n```')]);