Files
docmost/packages/editor-ext/src/lib/table-of-contents-node/table-of-contents.ts
T
2025-10-15 12:20:08 +01:00

53 lines
1.0 KiB
TypeScript

import { Node } from "@tiptap/core";
import { ReactNodeViewRenderer } from "@tiptap/react";
export interface TableOfContentsNodeOptions {
HTMLAttributes: Record<string, any>;
view: any;
}
declare module "@tiptap/core" {
interface Commands<ReturnType> {
tableOfContentsNode: {
insertTableOfContents: () => ReturnType;
};
}
}
export const TableOfContentsNode = Node.create<TableOfContentsNodeOptions>({
name: "tableOfContentsNode",
group: "block",
atom: true,
selectable: true,
draggable: true,
inline: false,
parseHTML() {
return [
{
tag: 'div[data-type="table-of-content"]',
},
];
},
renderHTML({ HTMLAttributes }) {
return ["div", { ...HTMLAttributes, "data-type": "table-of-content" }];
},
addNodeView() {
return ReactNodeViewRenderer(this.options.view);
},
addCommands() {
return {
insertTableOfContents:
() =>
({ commands }) => {
return commands.insertContent({
type: this.name,
});
},
};
},
});