@diplodoc/transform
Version:
A simple transformer of text in YFM (Yandex Flavored Markdown) to HTML
42 lines • 1.45 kB
JavaScript
;
//
// Utils for working with custom ids in heading tokens
//
Object.defineProperty(exports, "__esModule", { value: true });
exports.removeCustomIds = exports.removeCustomId = exports.getCustomIds = exports.CUSTOM_ID_EXCEPTION = exports.CUSTOM_ID_REGEXP = void 0;
exports.CUSTOM_ID_REGEXP = /\[?{ ?#(\S+) ?}]?/g;
exports.CUSTOM_ID_EXCEPTION = '[{#T}]';
const getCustomIds = (content) => {
const ids = [];
content.replace(exports.CUSTOM_ID_REGEXP, (match, customId) => {
if (match !== exports.CUSTOM_ID_EXCEPTION) {
ids.push(customId);
}
return '';
});
return ids.length ? ids : null;
};
exports.getCustomIds = getCustomIds;
const removeCustomId = (content) => {
if (exports.CUSTOM_ID_REGEXP.test(content)) {
return content
.replace(exports.CUSTOM_ID_REGEXP, (match) => {
if (match === exports.CUSTOM_ID_EXCEPTION) {
return match;
}
return '';
})
.trim();
}
return content;
};
exports.removeCustomId = removeCustomId;
const removeCustomIds = (token) => {
var _a;
token.content = (0, exports.removeCustomId)(token.content);
(_a = token.children) === null || _a === void 0 ? void 0 : _a.forEach((child) => {
child.content = (0, exports.removeCustomId)(child.content);
});
};
exports.removeCustomIds = removeCustomIds;
//# sourceMappingURL=custom-id.js.map