UNPKG

markdown-it-alert

Version:
57 lines 2.15 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const container_1 = require("./container"); function default_1(md, options) { let containerOpenCount = 0; const links = options && options.links ? options.links : true; const bem = options && options.bem ? options.bem : false; const role = options && options.role ? options.role : true; const tag = options && options.tag ? options.tag : "div"; const alertTypes = options && options.types ? options.types : ["info", "warning", "error", "danger", "tip", "success"]; init(alertTypes); return; function setupContainer(name) { md.use(container_1.Container, name, { render: function (tokens, idx) { if (tokens[idx].nesting === 1) { containerOpenCount += 1; const roleHtml = role ? `role="alert"` : ``; const classHtml = bem ? `alert alert--${name}` : `alert alert-${name}`; return `<${tag} class="${classHtml}" ${roleHtml}>\n`; } else { containerOpenCount -= 1; return `</${tag}>\n`; } }, }); } function isContainerOpen() { return containerOpenCount > 0; } function selfRender(tokens, idx, options, env, self) { return self.renderToken(tokens, idx, options); } function setupLinks() { var defaultRender = md.renderer.rules.link_open || selfRender; md.renderer.rules.link_open = function (tokens, idx, options, env, self) { const alertClass = bem ? `alert--link` : `alert-link`; if (isContainerOpen()) { tokens[idx].attrPush(["class", alertClass]); } return defaultRender(tokens, idx, options, env, self); }; } function init(alertTypes) { alertTypes.forEach((el) => setupContainer(el)); if (links) { setupLinks(); } } } exports.default = default_1; //# sourceMappingURL=index.js.map