Skip to content

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

Use streaming state for nested Markdown fences

Emre Sokullu8e994c5858f0
diff --git a/src/chrome/src/ui/markdown-render.js b/src/chrome/src/ui/markdown-render.js
index 6653db406..0cb7a92fc 100644
--- a/src/chrome/src/ui/markdown-render.js
+++ b/src/chrome/src/ui/markdown-render.js
@@ -109,31 +109,6 @@ function isFenceCloser(opener, candidate, fence, info) {
&& fence.length >= opener.fence.length;
}
-function hasCompatibleFenceAfter(matches, startIndex, active) {
- for (let index = startIndex + 1; index < matches.length; index += 1) {
- const [, prefix, indentation, fence, info] = matches[index];
- if (isFenceCloser(active, fenceContainer(prefix, indentation), fence, info)) return true;
- }
- return false;
-}
-
-function nestedFenceHasOwnCloser(matches, startIndex, active, stopAtNamedFence = false) {
- let closers = 0;
- for (let index = startIndex + 1; index < matches.length; index += 1) {
- const [, prefix, indentation, fence, info] = matches[index];
- if (isFenceCloser(active, fenceContainer(prefix, indentation), fence, info)) {
- if (stopAtNamedFence && closers && hasCompatibleFenceAfter(matches, index, active)) {
- return false;
- }
- closers += 1;
- if (closers === 2) return true;
- } else if (stopAtNamedFence && closers && info.trim()) {
- return false;
- }
- }
- return false;
-}
-
function listContinuationContainer(source, position, prefix, indentation) {
const current = fenceContainer(prefix, indentation);
const continuationIndent = indentationColumns(indentation);
@@ -176,6 +151,15 @@ function lineBelongsToContainer(line, container) {
return indent >= indentationColumns(container.listPrefix);
}
+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);
@@ -183,6 +167,11 @@ function unfinishedContainerEnd(source, start, container) {
for (const match of remainder.matchAll(/[^\r\n]*(?:\r?\n|$)/g)) {
if (!match[0]) break;
const line = match[0].replace(/\r?\n$/, '');
+ if (!line.trim() && container.listPrefix
+ && followingLineBelongsToContainer(remainder, match.index + match[0].length, container)) {
+ offset += match[0].length;
+ continue;
+ }
if (!lineBelongsToContainer(line, container)) return offset;
offset += match[0].length;
}
@@ -262,20 +251,12 @@ export function replaceMarkdownCodeFences(value, renderBlock, { streaming = fals
cursor = match.index + match[0].replace(/\r?\n$/, '').length;
block = null;
}
- } else if (active.markdown && validOpening && info.trim() && fence === active.fence
+ } else if (streaming && active.markdown && validOpening && info.trim() && fence === active.fence
&& fenceCloserInContainer(active.container, container)) {
// 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.
- const hasMarkdownPreamble = source.slice(block.start, match.index).trim().length > 0;
- if (streaming || nestedFenceHasOwnCloser(
- matches,
- matchIndex,
- active,
- !hasMarkdownPreamble,
- )) {
- stack.push({ fence, markdown, container });
- }
+ stack.push({ fence, markdown, container });
}
}
diff --git a/src/chrome/src/ui/sidepanel.js b/src/chrome/src/ui/sidepanel.js
index 97ac05cce..219a57794 100644
--- a/src/chrome/src/ui/sidepanel.js
+++ b/src/chrome/src/ui/sidepanel.js
@@ -12826,6 +12826,7 @@ function scheduleMathRender() {
function formatMarkdown(text, options = {}) {
if (!text) return '';
const enhance = options.enhance !== false;
+ const streaming = options.streaming === true || !enhance;
// 1. Extract fenced code blocks BEFORE escaping HTML
const codeBlocks = [];
@@ -12834,7 +12835,7 @@ function formatMarkdown(text, options = {}) {
const id = `__CODEBLOCK_${codeBlocks.length}__`;
codeBlocks.push({ lang: lang || '', code });
return id;
- }, { streaming: !enhance });
+ }, { streaming });
// 2. Extract inline code before escaping
const inlineCodes = [];
diff --git a/src/firefox/src/ui/markdown-render.js b/src/firefox/src/ui/markdown-render.js
index 6653db406..0cb7a92fc 100644
--- a/src/firefox/src/ui/markdown-render.js
+++ b/src/firefox/src/ui/markdown-render.js
@@ -109,31 +109,6 @@ function isFenceCloser(opener, candidate, fence, info) {
&& fence.length >= opener.fence.length;
}
-function hasCompatibleFenceAfter(matches, startIndex, active) {
- for (let index = startIndex + 1; index < matches.length; index += 1) {
- const [, prefix, indentation, fence, info] = matches[index];
- if (isFenceCloser(active, fenceContainer(prefix, indentation), fence, info)) return true;
- }
- return false;
-}
-
-function nestedFenceHasOwnCloser(matches, startIndex, active, stopAtNamedFence = false) {
- let closers = 0;
- for (let index = startIndex + 1; index < matches.length; index += 1) {
- const [, prefix, indentation, fence, info] = matches[index];
- if (isFenceCloser(active, fenceContainer(prefix, indentation), fence, info)) {
- if (stopAtNamedFence && closers && hasCompatibleFenceAfter(matches, index, active)) {
- return false;
- }
- closers += 1;
- if (closers === 2) return true;
- } else if (stopAtNamedFence && closers && info.trim()) {
- return false;
- }
- }
- return false;
-}
-
function listContinuationContainer(source, position, prefix, indentation) {
const current = fenceContainer(prefix, indentation);
const continuationIndent = indentationColumns(indentation);
@@ -176,6 +151,15 @@ function lineBelongsToContainer(line, container) {
return indent >= indentationColumns(container.listPrefix);
}
+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);
@@ -183,6 +167,11 @@ function unfinishedContainerEnd(source, start, container) {
for (const match of remainder.matchAll(/[^\r\n]*(?:\r?\n|$)/g)) {
if (!match[0]) break;
const line = match[0].replace(/\r?\n$/, '');
+ if (!line.trim() && container.listPrefix
+ && followingLineBelongsToContainer(remainder, match.index + match[0].length, container)) {
+ offset += match[0].length;
+ continue;
+ }
if (!lineBelongsToContainer(line, container)) return offset;
offset += match[0].length;
}
@@ -262,20 +251,12 @@ export function replaceMarkdownCodeFences(value, renderBlock, { streaming = fals
cursor = match.index + match[0].replace(/\r?\n$/, '').length;
block = null;
}
- } else if (active.markdown && validOpening && info.trim() && fence === active.fence
+ } else if (streaming && active.markdown && validOpening && info.trim() && fence === active.fence
&& fenceCloserInContainer(active.container, container)) {
// 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.
- const hasMarkdownPreamble = source.slice(block.start, match.index).trim().length > 0;
- if (streaming || nestedFenceHasOwnCloser(
- matches,
- matchIndex,
- active,
- !hasMarkdownPreamble,
- )) {
- stack.push({ fence, markdown, container });
- }
+ stack.push({ fence, markdown, container });
}
}
diff --git a/src/firefox/src/ui/sidepanel.js b/src/firefox/src/ui/sidepanel.js
index 53f2dcb40..ef495468e 100644
--- a/src/firefox/src/ui/sidepanel.js
+++ b/src/firefox/src/ui/sidepanel.js
@@ -12413,6 +12413,7 @@ function scheduleMathRender() {
function formatMarkdown(text, options = {}) {
if (!text) return '';
const enhance = options.enhance !== false;
+ const streaming = options.streaming === true || !enhance;
// 1. Extract fenced code blocks BEFORE escaping HTML
const codeBlocks = [];
@@ -12421,7 +12422,7 @@ function formatMarkdown(text, options = {}) {
const id = `__CODEBLOCK_${codeBlocks.length}__`;
codeBlocks.push({ lang: lang || '', code });
return id;
- }, { streaming: !enhance });
+ }, { streaming });
// 2. Extract inline code before escaping
const inlineCodes = [];
diff --git a/test/markdown-render.mjs b/test/markdown-render.mjs
index 94c057118..86a658fe6 100644
--- a/test/markdown-render.mjs
+++ b/test/markdown-render.mjs
@@ -37,10 +37,10 @@ for (const build of ['chrome', 'firefox']) {
test(`${build}: nested README remains one complete, copyable Markdown block`, () => {
for (const language of ['markdown', 'md', 'MARKDOWN']) {
const source = draft.replace('```markdown', `\`\`\`${language}`);
- for (const enhance of [true, false]) {
- const html = formatMarkdown(source, { enhance });
+ for (const [options, copyButton] of [[{ streaming: true }, true], [{ enhance: false }, false]]) {
+ const html = formatMarkdown(source, options);
assert.deepEqual(preContents(html), [helpers.escapeCodeHtml(readme)]);
- assert.equal((html.match(/class="code-copy-btn"/g) || []).length, enhance ? 1 : 0);
+ assert.equal((html.match(/class="code-copy-btn"/g) || []).length, copyButton ? 1 : 0);
assert.match(html, /<h2>Next steps<\/h2>Review it\./);
assert.doesNotMatch(html, /<script>|<h[1-6]>Role|<br>text<br>|<br>bash<br>/i);
}
@@ -55,7 +55,6 @@ for (const build of ['chrome', 'firefox']) {
['```javascript title="sample.js"', 'const s = "```";\n~~~\n``\n``` trailing text\n', '```'],
[' ```text', 'literal **bold** and [link](javascript:alert(1))\n', ' ``` '],
['```text', '````js\n## Literal heading\n', '```'],
- ['~~~markdown', '~~~bash\necho ok\n~~~\n## Still inside\n', '~~~'],
];
for (const [open, code, close] of cases) {
for (const newline of ['\n', '\r\n']) {
@@ -222,6 +221,13 @@ for (const build of ['chrome', 'firefox']) {
}
assert.match(formatMarkdown(cases[0][0]), /Outside/);
assert.match(formatMarkdown(cases[1][0]), /Next/);
+ const unfinishedList = '- ```text\n hello\n\n again';
+ const listBlocks = [];
+ assert.equal(helpers.replaceMarkdownCodeFences(unfinishedList, (info, code) => {
+ listBlocks.push({ info, code });
+ return 'BLOCK';
+ }), '- BLOCK');
+ assert.deepEqual(listBlocks, [{ info: 'text', code: 'hello\n\nagain' }]);
});
test(`${build}: tab-indented list fences use visual indentation columns`, () => {
@@ -303,9 +309,9 @@ for (const build of ['chrome', 'firefox']) {
let copied = null;
Object.defineProperty(navigator, 'clipboard', { value: { writeText: async text => { copied = text; } } });
const message = document.querySelector('#message');
- message.innerHTML = format(source, { enhance: false });
+ message.innerHTML = format(source, { streaming: true });
const streamed = message.querySelector('pre code').textContent;
- message.innerHTML = format(source);
+ message.innerHTML = format(source, { streaming: true });
await new Promise(resolve => setTimeout(resolve, 20));
message.querySelector('.code-copy-btn').click();
await Promise.resolve();