Skip to content

Commit c2b8ad0

Browse files
ComputelessComputerwarp-agent
andcommitted
persist widgets to markdown via renderMarkdown
Co-Authored-By: Warp <agent@warp.dev>
1 parent 480eaa2 commit c2b8ad0

1 file changed

Lines changed: 26 additions & 3 deletions

File tree

src/components/editor/extensions/widget/WidgetExtension.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
import { mergeAttributes, Node, } from "@tiptap/core";
2+
import type { JSONContent, } from "@tiptap/core";
23
import { ReactNodeViewRenderer, } from "@tiptap/react";
34
import { generateWidget, } from "../../../../services/generate";
45
import { WidgetView, } from "./WidgetView";
56

7+
function escapeAttr(s: string,): string {
8+
return s
9+
.replace(/&/g, "&amp;",)
10+
.replace(/"/g, "&quot;",)
11+
.replace(/</g, "&lt;",)
12+
.replace(/>/g, "&gt;",);
13+
}
14+
615
export interface WidgetAttributes {
716
id: string;
817
/** JSON-stringified Spec from json-render, or empty string */
@@ -49,14 +58,28 @@ export const WidgetExtension = Node.create({
4958
atom: true,
5059
draggable: true,
5160

61+
renderMarkdown(node: JSONContent,) {
62+
const a = node.attrs || {};
63+
const parts = ['<div data-widget=""',];
64+
if (a.id) parts.push(` data-id="${escapeAttr(String(a.id,),)}"`,);
65+
if (a.spec) parts.push(` data-spec="${escapeAttr(String(a.spec,),)}"`,);
66+
if (a.prompt) parts.push(` data-prompt="${escapeAttr(String(a.prompt,),)}"`,);
67+
if (a.saved) parts.push(' data-saved="true"',);
68+
parts.push("></div>",);
69+
return parts.join("",) + "\n\n";
70+
},
71+
5272
addAttributes() {
5373
return {
5474
id: { default: null, },
5575
spec: { default: "", },
5676
prompt: { default: "", },
57-
saved: { default: false, },
58-
loading: { default: false, },
59-
error: { default: "", },
77+
saved: {
78+
default: false,
79+
parseHTML: (el: HTMLElement,) => el.getAttribute("data-saved",) === "true",
80+
},
81+
loading: { default: false, rendered: false, },
82+
error: { default: "", rendered: false, },
6083
};
6184
},
6285

0 commit comments

Comments
 (0)