Skip to content

Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.

Normalize over-indented markdown list items (#4020)

Julius Marminge54f167e2e0b8
diff --git a/apps/web/src/components/ChatMarkdown.tsx b/apps/web/src/components/ChatMarkdown.tsx
index b04e98a8a..c85340c71 100644
--- a/apps/web/src/components/ChatMarkdown.tsx
+++ b/apps/web/src/components/ChatMarkdown.tsx
@@ -32,7 +32,7 @@ import React, {
useState,
type ReactNode,
} from "react";
-import type { Components } from "react-markdown";
+import type { Components, Options as ReactMarkdownOptions } from "react-markdown";
import ReactMarkdown from "react-markdown";
import { defaultUrlTransform } from "react-markdown";
import rehypeRaw from "rehype-raw";
@@ -60,6 +60,7 @@ import {
serializeTableElementToCsv,
serializeTableElementToMarkdown,
} from "../markdown-clipboard";
+import { remarkNormalizeListItemIndentation } from "../markdown-list-indentation";
import {
normalizeMarkdownLinkDestination,
resolveMarkdownFileLinkMeta,
@@ -164,6 +165,24 @@ const CHAT_MARKDOWN_SANITIZE_SCHEMA = {
},
} satisfies Parameters<typeof rehypeSanitize>[0];
+const CHAT_MARKDOWN_REMARK_PLUGINS = [
+ remarkGfm,
+ remarkNormalizeListItemIndentation,
+ remarkPreserveCodeMeta,
+] satisfies NonNullable<ReactMarkdownOptions["remarkPlugins"]>;
+
+const CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS = [
+ remarkGfm,
+ remarkNormalizeListItemIndentation,
+ remarkBreaks,
+ remarkPreserveCodeMeta,
+] satisfies NonNullable<ReactMarkdownOptions["remarkPlugins"]>;
+
+const CHAT_MARKDOWN_REHYPE_PLUGINS = [
+ rehypeRaw,
+ [rehypeSanitize, CHAT_MARKDOWN_SANITIZE_SCHEMA],
+] satisfies NonNullable<ReactMarkdownOptions["rehypePlugins"]>;
+
function extractFenceLanguage(className: string | undefined): string {
const match = className?.match(CODE_FENCE_LANGUAGE_REGEX);
const raw = match?.[1] ?? "text";
@@ -1545,11 +1564,9 @@ function ChatMarkdown({
>
<ReactMarkdown
remarkPlugins={
- lineBreaks
- ? [remarkGfm, remarkBreaks, remarkPreserveCodeMeta]
- : [remarkGfm, remarkPreserveCodeMeta]
+ lineBreaks ? CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS : CHAT_MARKDOWN_REMARK_PLUGINS
}
- rehypePlugins={[rehypeRaw, [rehypeSanitize, CHAT_MARKDOWN_SANITIZE_SCHEMA]]}
+ rehypePlugins={CHAT_MARKDOWN_REHYPE_PLUGINS}
components={markdownComponents}
urlTransform={markdownUrlTransform}
>
diff --git a/apps/web/src/markdown-list-indentation.test.tsx b/apps/web/src/markdown-list-indentation.test.tsx
new file mode 100644
index 000000000..2c04ed5fa
--- /dev/null
+++ b/apps/web/src/markdown-list-indentation.test.tsx
@@ -0,0 +1,82 @@
+import { renderToStaticMarkup } from "react-dom/server";
+import ReactMarkdown from "react-markdown";
+import remarkGfm from "remark-gfm";
+import { describe, expect, it } from "vite-plus/test";
+
+import { remarkNormalizeListItemIndentation } from "./markdown-list-indentation";
+
+function renderMarkdown(markdown: string): string {
+ return renderToStaticMarkup(
+ <ReactMarkdown remarkPlugins={[remarkGfm, remarkNormalizeListItemIndentation]}>
+ {markdown}
+ </ReactMarkdown>,
+ );
+}
+
+describe("remarkNormalizeListItemIndentation", () => {
+ it("renders same-line over-indented list content as list text", () => {
+ const html = renderMarkdown(`why did you do this?
+
+- for (const step of rest.steps) {
+- if (step.request.body) {
+- step.request.body = "<redacted>";
+- }
+- }`);
+
+ expect(html).not.toContain("<pre>");
+ expect(html).toContain("<li>for (const step of rest.steps) {</li>");
+ expect(html).toContain("<li>if (step.request.body) {</li>");
+ expect(html).toContain("<li>step.request.body = &quot;&lt;redacted&gt;&quot;;</li>");
+ });
+
+ it("parses inline markdown in recovered list content", () => {
+ const html = renderMarkdown(
+ "- **important** [docs](https://example.com) use `inline code`, not ~~plain text~~",
+ );
+
+ expect(html).toContain("<strong>important</strong>");
+ expect(html).toContain('<a href="https://example.com">docs</a>');
+ expect(html).toContain("<code>inline code</code>");
+ expect(html).toContain("<del>plain text</del>");
+ expect(html).not.toContain("**important**");
+ });
+
+ it("preserves every recovered block separated by blank lines", () => {
+ const html = renderMarkdown(`- **first block**
+
+ [second block](https://example.com)`);
+
+ expect(html).toContain("<strong>first block</strong>");
+ expect(html).toContain('<a href="https://example.com">second block</a>');
+ });
+
+ it("recursively normalizes lists in recovered tail blocks", () => {
+ const html = renderMarkdown(`- first block
+
+ - nested block`);
+
+ expect(html).not.toContain("<pre>");
+ expect(html).toContain("<li>nested block</li>");
+ });
+
+ it("preserves fenced code blocks within list items", () => {
+ const html = renderMarkdown(`- \`\`\`ts
+ const value = 1;
+ \`\`\``);
+
+ expect(html).toContain('<pre><code class="language-ts">const value = 1;');
+ });
+
+ it("preserves indented code blocks that start below a list marker", () => {
+ const html = renderMarkdown(`-
+ const value = 1;`);
+
+ expect(html).toContain("<pre><code>const value = 1;");
+ });
+
+ it("preserves same-line code blocks without excess indentation", () => {
+ const html = renderMarkdown("- const value = 1;");
+
+ expect(html).toContain("<pre><code>const value = 1;");
+ });
+});
diff --git a/apps/web/src/markdown-list-indentation.ts b/apps/web/src/markdown-list-indentation.ts
new file mode 100644
index 000000000..bd1728e4d
--- /dev/null
+++ b/apps/web/src/markdown-list-indentation.ts
@@ -0,0 +1,143 @@
+interface MarkdownPosition {
+ readonly start?: {
+ readonly line?: number;
+ readonly offset?: number;
+ };
+}
+
+interface MarkdownAstNode {
+ readonly type: string;
+ readonly value?: unknown;
+ readonly position?: MarkdownPosition;
+ children?: MarkdownAstNode[];
+}
+
+interface MarkdownFile {
+ readonly value?: unknown;
+}
+
+interface MarkdownParser {
+ parse(markdown: string): unknown;
+}
+
+interface RecoveredMarkdown {
+ readonly blocks: MarkdownAstNode[];
+ readonly source: string;
+}
+
+const INLINE_PARSE_PREFIX = "t3-markdown-inline-prefix:";
+
+function isSameLineOverIndentedCode(
+ node: MarkdownAstNode,
+ parent: MarkdownAstNode | undefined,
+ markdown: string,
+): boolean {
+ if (
+ node.type !== "code" ||
+ parent?.type !== "listItem" ||
+ typeof node.value !== "string" ||
+ !/^[\t ]/.test(node.value)
+ ) {
+ return false;
+ }
+
+ const nodeStart = node.position?.start;
+ const parentStart = parent.position?.start;
+ if (
+ nodeStart?.line === undefined ||
+ nodeStart.offset === undefined ||
+ parentStart?.line === undefined ||
+ nodeStart.line !== parentStart.line
+ ) {
+ return false;
+ }
+
+ const sourceCharacter = markdown[nodeStart.offset];
+ return sourceCharacter !== "`" && sourceCharacter !== "~";
+}
+
+function parseRecoveredMarkdown(value: string, parser: MarkdownParser): RecoveredMarkdown {
+ // A text prefix forces block-looking input into a paragraph while preserving
+ // the processor's configured inline extensions (for example, GFM syntax).
+ // Later root children are kept as blocks so blank-line-separated content is
+ // never discarded.
+ const source = `${INLINE_PARSE_PREFIX}${value}`;
+ const document = parser.parse(source) as MarkdownAstNode;
+ const blocks = document.children;
+ const paragraph = blocks?.[0];
+ const children = paragraph?.type === "paragraph" ? paragraph.children : undefined;
+ const first = children?.[0];
+ if (
+ !blocks ||
+ !children ||
+ first?.type !== "text" ||
+ typeof first.value !== "string" ||
+ !first.value.startsWith(INLINE_PARSE_PREFIX)
+ ) {
+ return { blocks: [{ type: "text", value }], source };
+ }
+
+ const firstValue = first.value.slice(INLINE_PARSE_PREFIX.length);
+ return {
+ blocks: [
+ {
+ ...paragraph,
+ type: "paragraph",
+ children: [...(firstValue ? [{ ...first, value: firstValue }] : []), ...children.slice(1)],
+ },
+ ...blocks.slice(1),
+ ],
+ source,
+ };
+}
+
+function blocksFromIndentedCode(node: MarkdownAstNode, parser: MarkdownParser): RecoveredMarkdown {
+ const value = typeof node.value === "string" ? node.value.trim() : "";
+ const recovered = parseRecoveredMarkdown(value, parser);
+ const first = recovered.blocks[0];
+ return {
+ ...recovered,
+ blocks:
+ first && node.position
+ ? [{ ...first, position: node.position }, ...recovered.blocks.slice(1)]
+ : recovered.blocks,
+ };
+}
+
+/**
+ * CommonMark treats four or more spaces after a list marker as an indented
+ * code block. In chat output, excessive spacing is commonly accidental
+ * alignment such as `- text`, which otherwise produces a full code card
+ * for every bullet. Only normalize blocks that retain excess indentation and
+ * start on the marker's own line; explicit fences and conventional indented
+ * blocks remain code.
+ */
+function attachListItemIndentationNormalizer(this: MarkdownParser) {
+ return (tree: MarkdownAstNode, file: MarkdownFile) => {
+ if (typeof file.value !== "string") {
+ return;
+ }
+ const markdown = file.value;
+
+ const visit = (node: MarkdownAstNode, source: string) => {
+ if (!node.children) {
+ return;
+ }
+ node.children = node.children.flatMap((child) => {
+ if (isSameLineOverIndentedCode(child, node, source)) {
+ const recovered = blocksFromIndentedCode(child, this);
+ for (const block of recovered.blocks) {
+ visit(block, recovered.source);
+ }
+ return recovered.blocks;
+ }
+ visit(child, source);
+ return [child];
+ });
+ };
+
+ visit(tree, markdown);
+ };
+}
+
+export const remarkNormalizeListItemIndentation = attachListItemIndentationNormalizer;