"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CodeSpecs = exports.codeType = exports.codeMarkName = void 0;
const schema_1 = require("../../../../utils/schema.js");
exports.codeMarkName = 'code';
exports.codeType = (0, schema_1.markTypeFactory)(exports.codeMarkName);
const CodeSpecs = (builder) => {
    builder.addMark(exports.codeMarkName, () => ({
        spec: {
            code: true,
            parseDOM: [{ tag: 'code' }],
            toDOM() {
                return ['code'];
            },
        },
        toMd: {
            open(_state, _mark, parent, index) {
                return backticksFor(parent.child(index), -1);
            },
            close(_state, _mark, parent, index) {
                return backticksFor(parent.child(index - 1), 1);
            },
            escape: false,
        },
        fromMd: {
            tokenSpec: { name: exports.codeMarkName, type: 'mark', code: true, noCloseToken: true },
            tokenName: 'code_inline',
        },
    }), builder.Priority.Lowest);
};
exports.CodeSpecs = CodeSpecs;
function backticksFor(node, side) {
    const ticks = /`+/g;
    let m;
    let len = 0;
    if (node.isText)
        while ((m = ticks.exec(node.text || '')))
            len = Math.max(len, m[0].length);
    let result = len > 0 && side > 0 ? ' `' : '`';
    for (let i = 0; i < len; i++)
        result += '`';
    if (len > 0 && side < 0)
        result += ' ';
    return result;
}
//# sourceMappingURL=index.js.map
