Skip to content
TypeScript

fix(web,mobile): markdown previews no longer show YAML frontmatter as a giant heading (#25804)

Theo Brownef81c4cd79f9a
diff --git a/apps/mobile/src/features/files/FileMarkdownPreview.tsx b/apps/mobile/src/features/files/FileMarkdownPreview.tsx
index bbd805ca574..f9f81459bbf 100644
--- a/apps/mobile/src/features/files/FileMarkdownPreview.tsx
+++ b/apps/mobile/src/features/files/FileMarkdownPreview.tsx
@@ -2,6 +2,7 @@ import { settlePromise } from "@t3tools/client-runtime/state/runtime";
import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
import { resolveMobileMarkdownMediaSource } from "../../lib/markdownMediaSource";
import { getBrowseDirectoryPath } from "@t3tools/client-runtime/state/projects";
+import { maskMarkdownFrontmatter } from "@t3tools/shared/markdownFrontmatter";
import { useState } from "react";
import { StackActions, useNavigation } from "@react-navigation/native";
import { RefreshControl, ScrollView, View } from "react-native";
@@ -235,7 +236,7 @@ export function FileMarkdownPreview(props: {
>
<View className="mx-auto w-full max-w-[760px]">
<SelectableMarkdownText
- markdown={props.markdown}
+ markdown={maskMarkdownFrontmatter(props.markdown)}
fileEmbedLabel={(href) =>
markdownFileEmbedLabel(
href,
diff --git a/apps/web/src/components/files/AttachmentFilePreview.tsx b/apps/web/src/components/files/AttachmentFilePreview.tsx
index d582bfa1591..5ab933a565c 100644
--- a/apps/web/src/components/files/AttachmentFilePreview.tsx
+++ b/apps/web/src/components/files/AttachmentFilePreview.tsx
@@ -16,6 +16,7 @@ import { lazy, Suspense, useEffect, useRef, useState } from "react";
import { useAssetUrlRefresh } from "~/assets/assetUrls";
import ChatMarkdown from "~/components/ChatMarkdown";
+import { maskMarkdownFrontmatter } from "@t3tools/shared/markdownFrontmatter";
import { MorphIcon } from "~/components/MorphIcon";
import { ScrollArea } from "~/components/ui/scroll-area";
import { toastManager } from "~/components/ui/toast";
@@ -281,7 +282,11 @@ export function AttachmentFilePreview(props: {
</Suspense>
) : kind === "markdown" && rendered ? (
<ScrollArea className="min-h-0 flex-1">
- <ChatMarkdown text={content.text} cwd={undefined} className="mx-auto max-w-4xl px-6 py-5" />
+ <ChatMarkdown
+ text={maskMarkdownFrontmatter(content.text)}
+ cwd={undefined}
+ className="mx-auto max-w-4xl px-6 py-5"
+ />
</ScrollArea>
) : (
<ReadOnlySourcePreview name={props.name} text={content.text} />
diff --git a/apps/web/src/components/files/FileMarkdownPreview.tsx b/apps/web/src/components/files/FileMarkdownPreview.tsx
index 62f1dc794a1..19df3f66870 100644
--- a/apps/web/src/components/files/FileMarkdownPreview.tsx
+++ b/apps/web/src/components/files/FileMarkdownPreview.tsx
@@ -2,6 +2,7 @@ import type { ScopedThreadRef } from "@t3tools/contracts";
import ChatMarkdown from "~/components/ChatMarkdown";
import { resolvePathLinkTarget } from "@t3tools/shared/fileLinks";
+import { maskMarkdownFrontmatter } from "@t3tools/shared/markdownFrontmatter";
export function FileMarkdownPreview(props: {
readonly cwd: string;
@@ -24,7 +25,7 @@ export function FileMarkdownPreview(props: {
return (
<ChatMarkdown
- text={props.text}
+ text={maskMarkdownFrontmatter(props.text)}
cwd={props.cwd}
imageBaseDir={imageBaseDir}
threadRef={props.threadRef}
diff --git a/packages/shared/package.json b/packages/shared/package.json
index f968d276883..0bfb6cdf081 100644
--- a/packages/shared/package.json
+++ b/packages/shared/package.json
@@ -447,6 +447,10 @@
"types": "./src/markdownGithubAlerts.ts",
"import": "./src/markdownGithubAlerts.ts"
},
+ "./markdownFrontmatter": {
+ "types": "./src/markdownFrontmatter.ts",
+ "import": "./src/markdownFrontmatter.ts"
+ },
"./markdownListIndentation": {
"types": "./src/markdownListIndentation.ts",
"import": "./src/markdownListIndentation.ts"
diff --git a/packages/shared/src/markdownFrontmatter.test.ts b/packages/shared/src/markdownFrontmatter.test.ts
new file mode 100644
index 00000000000..a7ac53657d4
--- /dev/null
+++ b/packages/shared/src/markdownFrontmatter.test.ts
@@ -0,0 +1,25 @@
+import { describe, expect, it } from "vite-plus/test";
+
+import { maskMarkdownFrontmatter } from "./markdownFrontmatter.ts";
+
+describe("maskMarkdownFrontmatter", () => {
+ it("blanks leading frontmatter without moving later offsets", () => {
+ const markdown = "---\r\nname: skill\r\n---\r\n# Skill\n- [ ] item\n";
+ const masked = maskMarkdownFrontmatter(markdown);
+
+ expect(masked).toBe(" \r\n \r\n \r\n# Skill\n- [ ] item\n");
+ expect(masked.indexOf("[ ]")).toBe(markdown.indexOf("[ ]"));
+ });
+
+ it("handles a byte order mark, CR line endings, and a ... closing fence", () => {
+ expect(maskMarkdownFrontmatter("\uFEFF---\na: 1\n---\n# Body")).toBe(" \n \n \n# Body");
+ expect(maskMarkdownFrontmatter("---\ra: 1\r...\r# Body")).toBe(" \r \r \r# Body");
+ expect(maskMarkdownFrontmatter("---\n---\n# Body")).toBe(" \n \n# Body");
+ });
+
+ it("leaves unterminated or non-leading fences alone", () => {
+ expect(maskMarkdownFrontmatter("---\na: 1\n")).toBe("---\na: 1\n");
+ expect(maskMarkdownFrontmatter("# A\n---\nb\n---\n")).toBe("# A\n---\nb\n---\n");
+ expect(maskMarkdownFrontmatter("---\na: 1\n----\n")).toBe("---\na: 1\n----\n");
+ });
+});
diff --git a/packages/shared/src/markdownFrontmatter.ts b/packages/shared/src/markdownFrontmatter.ts
new file mode 100644
index 00000000000..8bfaf13e939
--- /dev/null
+++ b/packages/shared/src/markdownFrontmatter.ts
@@ -0,0 +1,11 @@
+const LEADING_FRONTMATTER =
+ /^\uFEFF?---[ \t]*(?:\r\n|\r|\n)(?:[\s\S]*?(?:\r\n|\r|\n))?(?:---|\.\.\.)[ \t]*(?=\r\n|\r|\n|$)/;
+
+/**
+ * Blanks a leading YAML frontmatter block so rendered previews skip it instead
+ * of showing a rule and a setext heading. Masked characters become spaces and
+ * line breaks stay, so source offsets (task checkbox edits) stay valid.
+ */
+export function maskMarkdownFrontmatter(markdown: string): string {
+ return markdown.replace(LEADING_FRONTMATTER, (block) => block.replace(/[^\r\n]/g, " "));
+}