markmap-lib
Version:
Visualize your Markdown as mindmaps with Markmap
48 lines (41 loc) • 1.28 kB
JavaScript
;
exports.__esModule = true;
exports.escapeHtml = escapeHtml;
exports.escapeScript = escapeScript;
exports.htmlOpen = htmlOpen;
exports.htmlClose = htmlClose;
exports.wrapHtml = wrapHtml;
exports.wrapStyle = wrapStyle;
function escapeHtml(html) {
return html.replace(/[&<"]/g, m => ({
'&': '&',
'<': '<',
'"': '"'
})[m]);
}
function escapeScript(content) {
return content.replace(/<(\/script>)/g, '\\x3c$2');
}
function htmlOpen(tagName, attrs) {
const attrStr = attrs ? Object.entries(attrs).map(([key, value]) => {
if (value == null || value === false) return;
key = ` ${escapeHtml(key)}`;
if (value === true) return key;
return `${key}="${escapeHtml(value)}"`;
}).filter(Boolean).join('') : '';
return `<${tagName}${attrStr}>`;
}
function htmlClose(tagName) {
return `</${tagName}>`;
}
function wrapHtml(tagName, content, attrs) {
if (content == null) return htmlOpen(tagName, attrs);
return htmlOpen(tagName, attrs) + (content || '') + htmlClose(tagName);
}
function wrapStyle(text, style) {
if (style.code) text = wrapHtml('code', text);
if (style.del) text = wrapHtml('del', text);
if (style.em) text = wrapHtml('em', text);
if (style.strong) text = wrapHtml('strong', text);
return text;
}