"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.headingIdsPlugin = void 0;
const custom_id_1 = require("./utils/custom-id.js");
/**
 * MarkdownIt plugin for parsing custom IDs in headings.
 * It replicates the logic of '@diplodoc/transform'.
 * @see https://github.com/diplodoc-platform/transform/blob/master/src/transform/plugins/anchors/index.ts
 */
const headingIdsPlugin = (md) => {
    md.core.ruler.push('heading-attrs', (state) => {
        const { tokens } = state;
        for (let i = 0; i < tokens.length; i++) {
            const token = tokens[i];
            if (token.type !== 'heading_open')
                continue;
            const inlineToken = tokens[i + 1];
            if (inlineToken?.type !== 'inline')
                continue;
            const customIds = (0, custom_id_1.getCustomIds)(inlineToken.content);
            if (customIds) {
                const id = customIds[0];
                token.attrSet('id', id);
                (0, custom_id_1.removeCustomIds)(inlineToken);
            }
        }
    });
};
exports.headingIdsPlugin = headingIdsPlugin;
//# sourceMappingURL=heading-ids.js.map
