{"version":3,"file":"index.cjs","names":["Node","mergeAttributes","textblockTypeInputRule"],"sources":["../src/heading.ts","../src/index.ts"],"sourcesContent":["import { mergeAttributes, Node, textblockTypeInputRule } from '@tiptap/core'\n\n/**\n * The heading level options.\n */\nexport type Level = 1 | 2 | 3 | 4 | 5 | 6\n\nexport interface HeadingOptions {\n /**\n * The available heading levels.\n * @default [1, 2, 3, 4, 5, 6]\n * @example [1, 2, 3]\n */\n levels: Level[]\n\n /**\n * The HTML attributes for a heading node.\n * @default {}\n * @example { class: 'foo' }\n */\n HTMLAttributes: Record\n}\n\ndeclare module '@tiptap/core' {\n interface Commands {\n heading: {\n /**\n * Set a heading node\n * @param attributes The heading attributes\n * @example editor.commands.setHeading({ level: 1 })\n */\n setHeading: (attributes: { level: Level }) => ReturnType\n /**\n * Toggle a heading node\n * @param attributes The heading attributes\n * @example editor.commands.toggleHeading({ level: 1 })\n */\n toggleHeading: (attributes: { level: Level }) => ReturnType\n }\n }\n}\n\n/**\n * This extension allows you to create headings.\n * @see https://www.tiptap.dev/api/nodes/heading\n */\nexport const Heading = Node.create({\n name: 'heading',\n\n addOptions() {\n return {\n levels: [1, 2, 3, 4, 5, 6],\n HTMLAttributes: {},\n }\n },\n\n content: 'inline*',\n\n group: 'block',\n\n defining: true,\n\n addAttributes() {\n return {\n level: {\n default: 1,\n rendered: false,\n },\n }\n },\n\n parseHTML() {\n return this.options.levels.map((level: Level) => ({\n tag: `h${level}`,\n attrs: { level },\n }))\n },\n\n renderHTML({ node, HTMLAttributes }) {\n const hasLevel = this.options.levels.includes(node.attrs.level)\n const level = hasLevel ? node.attrs.level : this.options.levels[0]\n\n return [`h${level}`, mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0]\n },\n\n parseMarkdown: (token, helpers) => {\n // Convert 'heading' token to heading node\n // marked provides 'depth' property (1-6) for heading level\n return helpers.createNode(\n 'heading',\n { level: token.depth || 1 },\n helpers.parseInline(token.tokens || []),\n )\n },\n\n renderMarkdown: (node, h) => {\n const level = node.attrs?.level ? parseInt(node.attrs.level as string, 10) : 1\n const headingChars = '#'.repeat(level)\n\n if (!node.content) {\n return ''\n }\n\n // Use current context for proper joining/spacing\n return `${headingChars} ${h.renderChildren(node.content)}`\n },\n\n addCommands() {\n return {\n setHeading:\n attributes =>\n ({ commands }) => {\n if (!this.options.levels.includes(attributes.level)) {\n return false\n }\n\n return commands.setNode(this.name, attributes)\n },\n toggleHeading:\n attributes =>\n ({ commands }) => {\n if (!this.options.levels.includes(attributes.level)) {\n return false\n }\n\n return commands.toggleNode(this.name, 'paragraph', attributes)\n },\n }\n },\n\n addKeyboardShortcuts() {\n return this.options.levels.reduce(\n (items, level) => ({\n ...items,\n [`Mod-Alt-${level}`]: () => this.editor.commands.toggleHeading({ level }),\n }),\n {},\n )\n },\n\n addInputRules() {\n return this.options.levels.map(level => {\n return textblockTypeInputRule({\n find: new RegExp(`^(#{${Math.min(...this.options.levels)},${level}})\\\\s$`),\n type: this.type,\n getAttributes: {\n level,\n },\n })\n })\n },\n})\n","import { Heading } from './heading.js'\n\nexport * from './heading.js'\n\nexport default Heading\n"],"mappings":";;;;;;;;;;AA8CA,MAAa,UAAUA,aAAAA,KAAK,OAAuB;CACjD,MAAM;CAEN,aAAa;EACX,OAAO;GACL,QAAQ;IAAC;IAAG;IAAG;IAAG;IAAG;IAAG;GAAC;GACzB,gBAAgB,CAAC;EACnB;CACF;CAEA,SAAS;CAET,OAAO;CAEP,UAAU;CAEV,gBAAgB;EACd,OAAO,EACL,OAAO;GACL,SAAS;GACT,UAAU;EACZ,EACF;CACF;CAEA,YAAY;EACV,OAAO,KAAK,QAAQ,OAAO,KAAK,WAAkB;GAChD,KAAK,IAAI;GACT,OAAO,EAAE,MAAM;EACjB,EAAE;CACJ;CAEA,WAAW,EAAE,MAAM,kBAAkB;EAInC,OAAO;GAAC,IAHS,KAAK,QAAQ,OAAO,SAAS,KAAK,MAAM,KACpC,IAAI,KAAK,MAAM,QAAQ,KAAK,QAAQ,OAA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