Skip to content

Commit 4306d6b

Browse files
committed
feat(web): block-level Markdown in the rich text composer
The rich text composer styled words but not blocks: fences, lists, quotes, rules and headings stayed literal text. Each is now a node that keeps its exact Markdown delimiters as attributes, so the stored draft stays byte-identical to what was typed. Fences get chat's code block frame, syntax highlighting (skipped above 20k characters), a language picker in the header, indentation handling and verbatim paste. Blocks form only at a top-level paragraph, and Tiptap's block chords are removed, because the serializer has no line for a block nested anywhere else.
1 parent a3abb52 commit 4306d6b

21 files changed

Lines changed: 2772 additions & 137 deletions

‎apps/web/package.json‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@
3232
"@tanstack/react-pacer": "^0.19.4",
3333
"@tanstack/react-router": "^1.160.2",
3434
"@tiptap/core": "^3.31.3",
35+
"@tiptap/extension-blockquote": "^3.31.3",
3536
"@tiptap/extension-code": "^3.31.3",
37+
"@tiptap/extension-code-block": "^3.31.3",
38+
"@tiptap/extension-heading": "^3.31.3",
39+
"@tiptap/extension-horizontal-rule": "^3.31.3",
40+
"@tiptap/extension-list": "^3.31.3",
3641
"@tiptap/extension-task-item": "^3.31.3",
3742
"@tiptap/extension-task-list": "^3.31.3",
3843
"@tiptap/pm": "^3.31.3",

‎apps/web/src/components/ChatMarkdown.tsx‎

Lines changed: 66 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,63 @@ function MarkdownDetails({
898898
* icon (redundant next to its own name) and fall back to the language text
899899
* when no specific icon exists or it fails to load.
900900
*/
901+
/**
902+
* The code block frame, shared with the composer so an editable fence and a
903+
* rendered one cannot drift apart: the wrapper, its header row with the
904+
* language title, an optional actions slot, and the body as children. The
905+
* composer passes Tiptap's node view wrapper as the outer element.
906+
*/
907+
export function MarkdownCodeBlockFrame({
908+
as: Wrapper = "div",
909+
language,
910+
fenceTitle,
911+
theme,
912+
wrapped = true,
913+
title,
914+
actions,
915+
headerProps,
916+
children,
917+
}: {
918+
as?: React.ElementType;
919+
language: string;
920+
fenceTitle: string | null;
921+
theme: "light" | "dark";
922+
wrapped?: boolean;
923+
/** Replaces the language title, as the composer does with its language picker. */
924+
title?: React.ReactNode;
925+
actions?: React.ReactNode;
926+
headerProps?: React.HTMLAttributes<HTMLDivElement>;
927+
children: React.ReactNode;
928+
}) {
929+
return (
930+
<Wrapper
931+
className="chat-markdown-codeblock my-[0.65rem] overflow-hidden rounded-lg border border-border/70 bg-secondary leading-snug dark:border-transparent dark:bg-input/32"
932+
data-language={language}
933+
data-wrap={wrapped ? "true" : "false"}
934+
>
935+
<div
936+
{...headerProps}
937+
className={cn(
938+
"chat-markdown-codeblock-header flex items-center justify-between gap-2 pt-1.5 pr-1.5 pb-0 pl-3 select-none",
939+
headerProps?.className,
940+
)}
941+
>
942+
{title ?? (
943+
<span className="inline-flex min-w-0 items-center gap-1.5 font-mono text-2xs">
944+
<MarkdownCodeBlockTitleContent
945+
fenceTitle={fenceTitle}
946+
language={language}
947+
theme={theme}
948+
/>
949+
</span>
950+
)}
951+
{actions}
952+
</div>
953+
{children}
954+
</Wrapper>
955+
);
956+
}
957+
901958
function MarkdownCodeBlockTitleContent({
902959
fenceTitle,
903960
language,
@@ -1007,19 +1064,12 @@ function MarkdownCodeBlock({
10071064
);
10081065

10091066
return (
1010-
<div
1011-
className="chat-markdown-codeblock my-[0.65rem] overflow-hidden rounded-lg border border-border/70 bg-secondary leading-snug dark:border-transparent dark:bg-input/32"
1012-
data-language={language}
1013-
data-wrap={wrapped ? "true" : "false"}
1014-
>
1015-
<div className="chat-markdown-codeblock-header flex items-center justify-between gap-2 pt-1.5 pr-1.5 pb-0 pl-3 select-none">
1016-
<span className="inline-flex min-w-0 items-center gap-1.5 font-mono text-2xs">
1017-
<MarkdownCodeBlockTitleContent
1018-
fenceTitle={fenceTitle}
1019-
language={language}
1020-
theme={theme}
1021-
/>
1022-
</span>
1067+
<MarkdownCodeBlockFrame
1068+
language={language}
1069+
fenceTitle={fenceTitle}
1070+
theme={theme}
1071+
wrapped={wrapped}
1072+
actions={
10231073
<span className="flex items-center gap-0.5" role="toolbar" aria-label="Code block actions">
10241074
<Tooltip>
10251075
<TooltipTrigger
@@ -1073,9 +1123,10 @@ function MarkdownCodeBlock({
10731123
<TooltipPopup side="top">{copyLabel}</TooltipPopup>
10741124
</Tooltip>
10751125
</span>
1076-
</div>
1126+
}
1127+
>
10771128
{children}
1078-
</div>
1129+
</MarkdownCodeBlockFrame>
10791130
);
10801131
}
10811132

0 commit comments

Comments
 (0)