table of contents node - WIP

This commit is contained in:
Philipinho
2025-10-15 12:20:08 +01:00
parent 042836cb6d
commit 2b6ac81c61
11 changed files with 212 additions and 4 deletions

View File

@ -20,3 +20,4 @@ export * from "./lib/markdown";
export * from "./lib/search-and-replace";
export * from "./lib/embed-provider";
export * from "./lib/subpages";
export * from "./lib/table-of-contents-node";

View File

@ -0,0 +1 @@
export { TableOfContentsNode } from "./table-of-contents";

View File

@ -0,0 +1,52 @@
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,
});
},
};
},
});