"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.YfmHeadingSpecs = exports.YfmHeadingAttr = void 0;
const const_1 = require("./const.js");
const heading_attrs_1 = require("./markdown/heading-attrs.js");
const utils_1 = require("./utils.js");
const DEFAULT_PLACEHOLDER = (node) => 'Heading ' + node.attrs[const_1.YfmHeadingAttr.Level];
var const_2 = require("./const.js");
Object.defineProperty(exports, "YfmHeadingAttr", { enumerable: true, get: function () { return const_2.YfmHeadingAttr; } });
/** YfmHeading extension needs markdown-it-attrs plugin */
const YfmHeadingSpecs = (builder, opts) => {
    builder.configureMd((md) => md.use(heading_attrs_1.headingAttrsPlugin));
    builder.addNode(const_1.headingNodeName, () => ({
        spec: {
            attrs: {
                [const_1.YfmHeadingAttr.Id]: { default: '' },
                [const_1.YfmHeadingAttr.Level]: { default: 1 },
                [const_1.YfmHeadingAttr.DataLine]: { default: null },
                [const_1.YfmHeadingAttr.Folding]: { default: null },
            },
            content: '(text | inline)*',
            group: 'block',
            defining: true,
            selectable: true,
            parseDOM: [
                { tag: 'h1', getAttrs: (0, utils_1.getNodeAttrs)(1), priority: 100, consuming: true },
                { tag: 'h2', getAttrs: (0, utils_1.getNodeAttrs)(2), priority: 100, consuming: true },
                { tag: 'h3', getAttrs: (0, utils_1.getNodeAttrs)(3), priority: 100, consuming: true },
                { tag: 'h4', getAttrs: (0, utils_1.getNodeAttrs)(4), priority: 100, consuming: true },
                { tag: 'h5', getAttrs: (0, utils_1.getNodeAttrs)(5), priority: 100, consuming: true },
                { tag: 'h6', getAttrs: (0, utils_1.getNodeAttrs)(6), priority: 100, consuming: true },
                {
                    // ignore anchor link inside headings
                    tag: 'a.yfm-anchor',
                    context: `${const_1.headingNodeName}/`,
                    skip: true,
                    ignore: true,
                    priority: 1000,
                },
            ],
            toDOM(node) {
                const id = node.attrs[const_1.YfmHeadingAttr.Id];
                const lineNumber = node.attrs[const_1.YfmHeadingAttr.DataLine];
                const folding = node.attrs[const_1.YfmHeadingAttr.Folding];
                return [
                    'h' + node.attrs[const_1.YfmHeadingAttr.Level],
                    {
                        id: id || null,
                        [const_1.YfmHeadingAttr.DataLine]: lineNumber,
                        [`data-${const_1.YfmHeadingAttr.Folding}`]: folding,
                    },
                    0,
                    // [
                    //     'a',
                    //     {
                    //         href: `#${node.attrs[YfmHeadingAttr.Id]}`,
                    //         class: 'yfm-anchor',
                    //         'aria-hidden': 'true',
                    //         contenteditable: 'false',
                    //     },
                    // ],
                    // ['span', 0],
                ];
            },
            placeholder: {
                content: builder.context.get('placeholder')?.heading ??
                    opts.headingPlaceholder ??
                    DEFAULT_PLACEHOLDER,
                alwaysVisible: true,
            },
        },
        fromMd: {
            tokenSpec: {
                name: const_1.headingNodeName,
                type: 'block',
                getAttrs: (token) => {
                    if (token.type.endsWith('_close'))
                        return {};
                    const attrs = Object.fromEntries(token.attrs || []);
                    // if (!attrs[YfmHeadingAttr.Id]) {
                    //     // calculate id if it was not specified
                    //     // tokens[index + 1] is child inline token
                    //     attrs[YfmHeadingAttr.Id] = slugify(tokens[index + 1].content);
                    // }
                    // attrs have id only if it explicitly specified manually
                    return {
                        [const_1.YfmHeadingAttr.Level]: Number(token.tag.slice(1)),
                        [const_1.YfmHeadingAttr.Folding]: token.meta?.folding === true ? true : null,
                        ...attrs,
                    };
                },
            },
        },
        toMd: (state, node) => {
            const folding = node.attrs[const_1.YfmHeadingAttr.Folding];
            const level = node.attrs[const_1.YfmHeadingAttr.Level];
            state.write(state.repeat('#', level) + (typeof folding === 'boolean' ? '+' : '') + ' ');
            state.renderInline(node);
            const anchor = node.attrs[const_1.YfmHeadingAttr.Id];
            if (anchor /*&& anchor !== node.firstChild?.textContent*/) {
                state.write(` {#${anchor}}`);
            }
            state.closeBlock(node);
        },
    }));
};
exports.YfmHeadingSpecs = YfmHeadingSpecs;
//# sourceMappingURL=index.js.map
