Open-source AI browser agent for Chrome and Firefox (monorepo) ðŸ§
JavaScript ai-agentai-agentsai-toolsbrowser-aibrowser-automationbrowser-extensionchrome-aichrome-devtoolschrome-extensionchrome-extensionsfirefox-addonfirefox-addonsfirefox-extensionfirefox-extensionsgemmagemma4minimaxoffline-aioffline-llmqwen
Preserve quoted fence boundaries
Emre Sokullu03aab738a687
diff --git a/src/chrome/src/ui/markdown-render.js b/src/chrome/src/ui/markdown-render.jsindex 7b38fec4f..de439d117 100644--- a/src/chrome/src/ui/markdown-render.js+++ b/src/chrome/src/ui/markdown-render.js@@ -63,9 +63,11 @@ function fenceContainer(prefix, indentation = '') {let remainder = source;let listPrefix = '';let quoteDepth = 0;+ let listBeforeQuote = false;while (remainder) {const quote = remainder.match(/^ {0,3}>[ \t]?/);if (quote) {+ if (listPrefix) listBeforeQuote = true;quoteDepth += 1;remainder = remainder.slice(quote[0].length);continue;@@ -76,7 +78,14 @@ function fenceContainer(prefix, indentation = '') {remainder = remainder.slice(list[0].length);}const leadingIndent = indentationColumns(source.match(/^[ \t]*(?=>)/)?.[0] || '');- return { quotePrefix, quoteDepth, listPrefix, leadingIndent, indentation: String(indentation) };+ return {+ quotePrefix,+ quoteDepth,+ listPrefix,+ listBeforeQuote,+ leadingIndent,+ indentation: String(indentation),+ };}function indentationColumns(value) {@@ -107,7 +116,8 @@ function stripIndentationColumns(line, columns) {function fenceCloserInContainer(opener, candidate) {if (opener.quoteDepth !== candidate.quoteDepth) return false;- const candidateIndent = candidate.leadingIndent + indentationColumns(candidate.indentation);+ const candidateIndent = (opener.listBeforeQuote ? candidate.leadingIndent : 0)+ + indentationColumns(candidate.indentation);if (!opener.listPrefix) return !candidate.listPrefix && candidateIndent <= 3;const listIndent = indentationColumns(opener.listPrefix);return !candidate.listPrefix@@ -160,7 +170,9 @@ function lineBelongsToContainer(line, container) {const content = line.slice(quotePrefix.length);if (!content.trim()) return false;- const quoteIndent = indentationColumns(quotePrefix.match(/^[ \t]*/)?.[0] || '');+ const quoteIndent = container.listBeforeQuote+ ? indentationColumns(quotePrefix.match(/^[ \t]*/)?.[0] || '')+ : 0;const indent = quoteIndent + indentationColumns(content.match(/^[ \t]*/)?.[0] || '');return indent >= indentationColumns(container.listPrefix);}@@ -332,11 +344,14 @@ export function replaceMarkdownCodeFences(value, renderBlock, { streaming = falsif (block) {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);output.push(block.prefix + renderBlock(block.info,- normalizeContainerCode(source.slice(block.start, end), block.container),- ));- output.push(source.slice(end));+ normalizeContainerCode(code, block.container),+ ) + (needsBoundaryNewline ? '\n' : ''));+ output.push(tail);} else output.push(source.slice(cursor));return output.join('');}diff --git a/src/firefox/src/ui/markdown-render.js b/src/firefox/src/ui/markdown-render.jsindex 7b38fec4f..de439d117 100644--- a/src/firefox/src/ui/markdown-render.js+++ b/src/firefox/src/ui/markdown-render.js@@ -63,9 +63,11 @@ function fenceContainer(prefix, indentation = '') {let remainder = source;let listPrefix = '';let quoteDepth = 0;+ let listBeforeQuote = false;while (remainder) {const quote = remainder.match(/^ {0,3}>[ \t]?/);if (quote) {+ if (listPrefix) listBeforeQuote = true;quoteDepth += 1;remainder = remainder.slice(quote[0].length);continue;@@ -76,7 +78,14 @@ function fenceContainer(prefix, indentation = '') {remainder = remainder.slice(list[0].length);}const leadingIndent = indentationColumns(source.match(/^[ \t]*(?=>)/)?.[0] || '');- return { quotePrefix, quoteDepth, listPrefix, leadingIndent, indentation: String(indentation) };+ return {+ quotePrefix,+ quoteDepth,+ listPrefix,+ listBeforeQuote,+ leadingIndent,+ indentation: String(indentation),+ };}function indentationColumns(value) {@@ -107,7 +116,8 @@ function stripIndentationColumns(line, columns) {function fenceCloserInContainer(opener, candidate) {if (opener.quoteDepth !== candidate.quoteDepth) return false;- const candidateIndent = candidate.leadingIndent + indentationColumns(candidate.indentation);+ const candidateIndent = (opener.listBeforeQuote ? candidate.leadingIndent : 0)+ + indentationColumns(candidate.indentation);if (!opener.listPrefix) return !candidate.listPrefix && candidateIndent <= 3;const listIndent = indentationColumns(opener.listPrefix);return !candidate.listPrefix@@ -160,7 +170,9 @@ function lineBelongsToContainer(line, container) {const content = line.slice(quotePrefix.length);if (!content.trim()) return false;- const quoteIndent = indentationColumns(quotePrefix.match(/^[ \t]*/)?.[0] || '');+ const quoteIndent = container.listBeforeQuote+ ? indentationColumns(quotePrefix.match(/^[ \t]*/)?.[0] || '')+ : 0;const indent = quoteIndent + indentationColumns(content.match(/^[ \t]*/)?.[0] || '');return indent >= indentationColumns(container.listPrefix);}@@ -332,11 +344,14 @@ export function replaceMarkdownCodeFences(value, renderBlock, { streaming = falsif (block) {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);output.push(block.prefix + renderBlock(block.info,- normalizeContainerCode(source.slice(block.start, end), block.container),- ));- output.push(source.slice(end));+ normalizeContainerCode(code, block.container),+ ) + (needsBoundaryNewline ? '\n' : ''));+ output.push(tail);} else output.push(source.slice(cursor));return output.join('');}diff --git a/test/markdown-render.mjs b/test/markdown-render.mjsindex b97e9937b..3bacdd950 100644--- a/test/markdown-render.mjs+++ b/test/markdown-render.mjs@@ -246,6 +246,22 @@ for (const build of ['chrome', 'firefox']) {}), '- BLOCK');assert.deepEqual(listBlocks, [{ info: 'text', code: 'hello\n\nagain' }]);+ const quoteListBoundary = ' > - ```text\n > hello\n > Outside\n\nAfter';+ const quoteListBlocks = [];+ assert.equal(helpers.replaceMarkdownCodeFences(quoteListBoundary, (info, code) => {+ quoteListBlocks.push({ info, code });+ return 'BLOCK';+ }), ' > - BLOCK\n > Outside\n\nAfter');+ assert.deepEqual(quoteListBlocks, [{ info: 'text', code: 'hello\n' }]);++ const noCloser = '> ```text\n> inside\nOutside';+ const noCloserBlocks = [];+ assert.equal(helpers.replaceMarkdownCodeFences(noCloser, (info, code) => {+ noCloserBlocks.push({ info, code });+ return 'BLOCK';+ }), '> BLOCK\nOutside');+ assert.deepEqual(noCloserBlocks, [{ info: 'text', code: 'inside\n' }]);+const escapedQuote = '> ```text\n> inside\nOutside\n> ```\nAfter';const quoteBlocks = [];assert.equal(helpers.replaceMarkdownCodeFences(escapedQuote, (info, code) => {