diff --git a/pkgs/id/web/src/editor.test.ts b/pkgs/id/web/src/editor.test.ts index 1d57a6b32b..0e8ca53af2 100644 --- a/pkgs/id/web/src/editor.test.ts +++ b/pkgs/id/web/src/editor.test.ts @@ -250,6 +250,58 @@ describe("richSchema list tight attrs (#1469)", () => { }); }); +// ============================================================================ +// #1347: footnote_reference / footnote_definition round-trip (codec mirror) +// ============================================================================ + +describe("richSchema footnote nodes (#1347)", () => { + it("declares footnote_reference as an inline atom with a label attr", () => { + const spec = richSchema.nodes.footnote_reference.spec; + expect(spec.inline).toBe(true); + expect(spec.atom).toBe(true); + expect(spec.attrs?.label).toEqual({ default: "" }); + }); + + it("declares footnote_definition as a block with a label attr and block content", () => { + const spec = richSchema.nodes.footnote_definition.spec; + expect(spec.group).toBe("block"); + expect(spec.content).toBe("block+"); + expect(spec.attrs?.label).toEqual({ default: "" }); + }); + + it("parses a codec footnote doc through Node.fromJSON without throwing", () => { + const json = { + type: "doc", + content: [ + { + type: "paragraph", + content: [ + { type: "text", text: "Text." }, + { type: "footnote_reference", attrs: { label: "1" } }, + ], + }, + { + type: "footnote_definition", + attrs: { label: "1" }, + content: [ + { type: "paragraph", content: [{ type: "text", text: "Body." }] }, + ], + }, + ], + }; + const doc = richSchema.nodeFromJSON(json); + const out = doc.toJSON(); + expect(out.content[1].type).toBe("footnote_definition"); + expect(out.content[1].attrs).toEqual({ label: "1" }); + expect(out.content[1].content).toEqual([ + { type: "paragraph", content: [{ type: "text", text: "Body." }] }, + ]); + const paragraph = out.content[0]; + const reference = paragraph.content.find((n: { type: string }) => n.type === "footnote_reference"); + expect(reference?.attrs).toEqual({ label: "1" }); + }); +}); + // ============================================================================ // #2005: ordered_list parseDOM start parsing — only non-negative safe // integers survive; everything else falls back to 1 (no reinterpretation) diff --git a/pkgs/id/web/src/editor.ts b/pkgs/id/web/src/editor.ts index 2caa724a86..61e6926c57 100644 --- a/pkgs/id/web/src/editor.ts +++ b/pkgs/id/web/src/editor.ts @@ -362,6 +362,49 @@ export const richSchema = new Schema({ return ["pre", { "data-front-matter": node.attrs.text as string }]; }, }, + // #1347: footnote reference atom carrying `label`; the markdown + // codec reads/writes it (markdown.rs). Display shows the + // reference as inert text — never rendered as live markup. + footnote_reference: { + inline: true, + group: "inline", + atom: true, + selectable: true, + attrs: { label: { default: "" } }, + parseDOM: [ + { + tag: "span[data-footnote-reference]", + getAttrs(dom) { + return { label: (dom as HTMLElement).dataset.footnoteReference ?? "" }; + }, + }, + ], + toDOM(node) { + const label = node.attrs.label as string; + return ["span", { "data-footnote-reference": label }, `[^${label}]`]; + }, + }, + // #1347: footnote definition block carrying `label` plus block + // children (paragraphs, lists, code blocks per #1031); the + // markdown codec reads/writes it. The label travels in the data + // attribute only — children render normally. + footnote_definition: { + group: "block", + content: "block+", + defining: true, + attrs: { label: { default: "" } }, + parseDOM: [ + { + tag: "div[data-footnote-definition]", + getAttrs(dom) { + return { label: (dom as HTMLElement).dataset.footnoteDefinition ?? "" }; + }, + }, + ], + toDOM(node) { + return ["div", { "data-footnote-definition": node.attrs.label as string }, 0]; + }, + }, }), marks: basicSchema.spec.marks.append({ strikethrough: {