"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.YfmHeadingSpecs = exports.renderYfmHeadingMarkup = exports.renderYfmHeadingAttributes = exports.YfmHeadingAttr = void 0;
const HeadingSpecs_1 = require("../../../markdown/Heading/HeadingSpecs/index.js");
const const_1 = require("./const.js");
const heading_ids_1 = require("./markdown/heading-ids.js");
const utils_1 = require("./utils.js");
var const_2 = require("./const.js");
Object.defineProperty(exports, "YfmHeadingAttr", { enumerable: true, get: function () { return const_2.YfmHeadingAttr; } });
var utils_2 = require("./utils.js");
Object.defineProperty(exports, "renderYfmHeadingAttributes", { enumerable: true, get: function () { return utils_2.renderYfmHeadingAttributes; } });
Object.defineProperty(exports, "renderYfmHeadingMarkup", { enumerable: true, get: function () { return utils_2.renderYfmHeadingMarkup; } });
/** YfmHeading extension needs markdown-it-attrs plugin */
const YfmHeadingSpecs = (builder, opts) => {
    if (!builder.hasNodeSpec(const_1.headingNodeName)) {
        builder.use(HeadingSpecs_1.HeadingSpecs, opts);
    }
    builder.configureMd((md) => md.use(heading_ids_1.headingIdsPlugin));
    builder.overrideNodeSpec(const_1.headingNodeName, (prev) => ({
        ...prev,
        attrs: {
            ...prev.attrs,
            [const_1.YfmHeadingAttr.Id]: { default: '' },
            [const_1.YfmHeadingAttr.Folding]: { default: null },
        },
        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],
            ];
        },
    }));
    builder.overrideMarkdownTokenParserSpec(const_1.headingNodeName, (prev) => ({
        ...prev,
        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,
            };
        },
    }));
    builder.overrideNodeSerializerSpec(const_1.headingNodeName, () => (0, HeadingSpecs_1.headingToMarkdown)({
        renderMarkup: utils_1.renderYfmHeadingMarkup,
        renderAttributes: utils_1.renderYfmHeadingAttributes,
    }));
};
exports.YfmHeadingSpecs = YfmHeadingSpecs;
//# sourceMappingURL=index.js.map
