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
Strip Markdown fence indentation from code
Emre Sokullu5960479cffe6
diff --git a/src/chrome/src/ui/markdown-render.js b/src/chrome/src/ui/markdown-render.jsindex 35c0680aa..79dc6e5ab 100644--- a/src/chrome/src/ui/markdown-render.js+++ b/src/chrome/src/ui/markdown-render.js@@ -115,6 +115,12 @@ function consumeIndentationColumns(line, columns) {return consumed === columns ? line.slice(offset) : null;}+function stripIndentationColumns(line, columns) {+ const stripped = consumeIndentationColumns(line, columns);+ if (stripped != null) return stripped;+ return String(line).replace(/^[ \t]*/, '');+}+function stripContainerPrefix(line, container) {let remainder = String(line);for (let quoteIndex = 0; quoteIndex < container.quoteDepth; quoteIndex += 1) {@@ -129,6 +135,12 @@ function stripContainerPrefix(line, container) {return consumeIndentationColumns(remainder, container.listIndentGroups.at(-1));}+function fenceIndentationInContainer(fence, container) {+ const indentation = indentationColumns(fence.indentation);+ if (fence.listPrefix) return indentation;+ return Math.max(0, indentation - container.listIndentGroups.at(-1));+}+function fenceCloserInContainer(opener, candidate) {if (!opener.quoteDepth && !opener.listPrefix) {return !candidate.quoteDepth && !candidate.listPrefix@@ -228,17 +240,18 @@ function unfinishedContainerEnd(source, start, container) {return source.length;}-function normalizeContainerCode(code, prefixOrContainer) {+function normalizeContainerCode(code, prefixOrContainer, fenceIndentation = 0) {const container = typeof prefixOrContainer === 'object'? prefixOrContainer: fenceContainer(prefixOrContainer);const { quoteDepth, listPrefix } = container;- if (!quoteDepth && !listPrefix) return code;+ if (!quoteDepth && !listPrefix && !fenceIndentation) return code;const lines = String(code).split(/(\r?\n)/);for (let index = 0; index < lines.length; index += 2) {const normalized = stripContainerPrefix(lines[index], container);- if (normalized != null) lines[index] = normalized;+ const line = normalized != null ? normalized : lines[index];+ lines[index] = fenceIndentation ? stripIndentationColumns(line, fenceIndentation) : line;}return lines.join('');}@@ -296,7 +309,7 @@ export function replaceMarkdownCodeFences(value, renderBlock, { streaming = fals&& !/^\r?\n/.test(source.slice(boundary));output.push(block.prefix + renderBlock(block.info,- normalizeContainerCode(code, block.container),+ normalizeContainerCode(code, block.container, block.fenceIndentation),) + (needsBoundaryNewline ? '\n' : ''));cursor = boundary;block = null;@@ -330,6 +343,7 @@ export function replaceMarkdownCodeFences(value, renderBlock, { streaming = falscontainer: openingContainer,start,openingEndsWithNewline: /\r?\n$/.test(match.raw),+ fenceIndentation: fenceIndentationInContainer(container, openingContainer),boundary: unfinishedContainerEnd(source, start, openingContainer),};stack.push({ fence, markdown, container: openingContainer });@@ -344,7 +358,7 @@ export function replaceMarkdownCodeFences(value, renderBlock, { streaming = falsif (!stack.length) {output.push(block.prefix + renderBlock(block.info,- normalizeContainerCode(source.slice(block.start, match.index), block.container),+ normalizeContainerCode(source.slice(block.start, match.index), block.container, block.fenceIndentation),));// Leave the closing line's newline for the surrounding Markdown.cursor = match.index + match.raw.replace(/\r?\n$/, '').length;@@ -368,7 +382,7 @@ export function replaceMarkdownCodeFences(value, renderBlock, { streaming = fals&& !/^\r?\n/.test(tail);output.push(block.prefix + renderBlock(block.info,- normalizeContainerCode(code, block.container),+ normalizeContainerCode(code, block.container, block.fenceIndentation),) + (needsBoundaryNewline ? '\n' : ''));output.push(tail);} else output.push(source.slice(cursor));diff --git a/src/firefox/src/ui/markdown-render.js b/src/firefox/src/ui/markdown-render.jsindex 35c0680aa..79dc6e5ab 100644--- a/src/firefox/src/ui/markdown-render.js+++ b/src/firefox/src/ui/markdown-render.js@@ -115,6 +115,12 @@ function consumeIndentationColumns(line, columns) {return consumed === columns ? line.slice(offset) : null;}+function stripIndentationColumns(line, columns) {+ const stripped = consumeIndentationColumns(line, columns);+ if (stripped != null) return stripped;+ return String(line).replace(/^[ \t]*/, '');+}+function stripContainerPrefix(line, container) {let remainder = String(line);for (let quoteIndex = 0; quoteIndex < container.quoteDepth; quoteIndex += 1) {@@ -129,6 +135,12 @@ function stripContainerPrefix(line, container) {return consumeIndentationColumns(remainder, container.listIndentGroups.at(-1));}+function fenceIndentationInContainer(fence, container) {+ const indentation = indentationColumns(fence.indentation);+ if (fence.listPrefix) return indentation;+ return Math.max(0, indentation - container.listIndentGroups.at(-1));+}+function fenceCloserInContainer(opener, candidate) {if (!opener.quoteDepth && !opener.listPrefix) {return !candidate.quoteDepth && !candidate.listPrefix@@ -228,17 +240,18 @@ function unfinishedContainerEnd(source, start, container) {return source.length;}-function normalizeContainerCode(code, prefixOrContainer) {+function normalizeContainerCode(code, prefixOrContainer, fenceIndentation = 0) {const container = typeof prefixOrContainer === 'object'? prefixOrContainer: fenceContainer(prefixOrContainer);const { quoteDepth, listPrefix } = container;- if (!quoteDepth && !listPrefix) return code;+ if (!quoteDepth && !listPrefix && !fenceIndentation) return code;const lines = String(code).split(/(\r?\n)/);for (let index = 0; index < lines.length; index += 2) {const normalized = stripContainerPrefix(lines[index], container);- if (normalized != null) lines[index] = normalized;+ const line = normalized != null ? normalized : lines[index];+ lines[index] = fenceIndentation ? stripIndentationColumns(line, fenceIndentation) : line;}return lines.join('');}@@ -296,7 +309,7 @@ export function replaceMarkdownCodeFences(value, renderBlock, { streaming = fals&& !/^\r?\n/.test(source.slice(boundary));output.push(block.prefix + renderBlock(block.info,- normalizeContainerCode(code, block.container),+ normalizeContainerCode(code, block.container, block.fenceIndentation),) + (needsBoundaryNewline ? '\n' : ''));cursor = boundary;block = null;@@ -330,6 +343,7 @@ export function replaceMarkdownCodeFences(value, renderBlock, { streaming = falscontainer: openingContainer,start,openingEndsWithNewline: /\r?\n$/.test(match.raw),+ fenceIndentation: fenceIndentationInContainer(container, openingContainer),boundary: unfinishedContainerEnd(source, start, openingContainer),};stack.push({ fence, markdown, container: openingContainer });@@ -344,7 +358,7 @@ export function replaceMarkdownCodeFences(value, renderBlock, { streaming = falsif (!stack.length) {output.push(block.prefix + renderBlock(block.info,- normalizeContainerCode(source.slice(block.start, match.index), block.container),+ normalizeContainerCode(source.slice(block.start, match.index), block.container, block.fenceIndentation),));// Leave the closing line's newline for the surrounding Markdown.cursor = match.index + match.raw.replace(/\r?\n$/, '').length;@@ -368,7 +382,7 @@ export function replaceMarkdownCodeFences(value, renderBlock, { streaming = fals&& !/^\r?\n/.test(tail);output.push(block.prefix + renderBlock(block.info,- normalizeContainerCode(code, block.container),+ normalizeContainerCode(code, block.container, block.fenceIndentation),) + (needsBoundaryNewline ? '\n' : ''));output.push(tail);} else output.push(source.slice(cursor));diff --git a/test/markdown-render.mjs b/test/markdown-render.mjsindex 817c91abd..b57ea95c2 100644--- a/test/markdown-render.mjs+++ b/test/markdown-render.mjs@@ -79,6 +79,16 @@ for (const build of ['chrome', 'firefox']) {}});+ test(`${build}: opening fence indentation is excluded from code`, () => {+ const source = ' ~~~text\n hello\n ~~~\nAfter';+ const blocks = [];+ assert.equal(helpers.replaceMarkdownCodeFences(source, (info, code) => {+ blocks.push({ info, code });+ return 'BLOCK';+ }), 'BLOCK\nAfter');+ assert.deepEqual(blocks, [{ info: 'text', code: 'hello\n' }]);+ });+test(`${build}: container-prefixed markers do not close an outer fenced block`, () => {const source = '~~~markdown\n> ~~~\n- ~~~\n> ```text\n~~~\n## Outside';const blocks = [];@@ -235,7 +245,7 @@ for (const build of ['chrome', 'firefox']) {extraIndentBlocks.push({ info, code });return 'BLOCK';}), '10. item\n continuation\n BLOCK');- assert.deepEqual(extraIndentBlocks, [{ info: 'js', code: ' code\n' }]);+ assert.deepEqual(extraIndentBlocks, [{ info: 'js', code: 'code\n' }]);});test(`${build}: fences in nested list and quote containers preserve following content`, () => {