(?:(?!\$.*?\$)[\s\S])*)/gm;
if (enableMathBlockInHtml) {
md.core.ruler.push("math_block_in_html_block", (state) => {
return handleMathInHtml(state, "math_block", "$$", math_block_within_html_regex);
});
}
if (enableMathInlineInHtml) {
md.core.ruler.push("math_inline_in_html_block", (state) => {
return handleMathInHtml(state, "math_inline", "$", math_inline_within_html_regex);
});
}
const katexInline = (latex) => {
const displayMode = /\\begin\{(align|equation|gather|cd|alignat)\}/ig.test(latex);
try {
return katex.renderToString(latex, { ...options, displayMode });
} catch (error2) {
if (options == null ? void 0 : options.throwOnError) {
console.log(error2);
}
return `${escapeHtml2(error2 + "")}`;
}
};
const inlineRenderer = (tokens, idx) => {
const content = tokens[idx].content;
const hasBacktick = content.length > 2 && content[0] === "`" && content[content.length - 1] === "`";
const sanitized = hasBacktick ? content.slice(1, -1) : content;
return katexInline(sanitized);
};
const katexBlockRenderer = (latex) => {
try {
return `${katex.renderToString(latex, { ...options, displayMode: true })}
`;
} catch (error2) {
if (options == null ? void 0 : options.throwOnError) {
console.log(error2);
}
return `${escapeHtml2(error2 + "")}
`;
}
};
const blockRenderer = (tokens, idx) => {
return katexBlockRenderer(tokens[idx].content) + "\n";
};
md.renderer.rules.math_inline = inlineRenderer;
md.renderer.rules.math_inline_block = blockRenderer;
md.renderer.rules.math_inline_bare_block = blockRenderer;
md.renderer.rules.math_block = blockRenderer;
if (enableFencedBlocks) {
const mathLanguageId = "math";
const originalFenceRenderer = md.renderer.rules.fence;
md.renderer.rules.fence = function(tokens, idx, options2, env, self) {
const token = tokens[idx];
if (token.info.trim().toLowerCase() === mathLanguageId && enableFencedBlocks) {
return katexBlockRenderer(token.content) + "\n";
} else {
return (originalFenceRenderer == null ? void 0 : originalFenceRenderer.call(this, tokens, idx, options2, env, self)) || "";
}
};
}
}
dist.default = default_1;
return dist;
}
var distExports = requireDist();
const katexPluginModule = /* @__PURE__ */ getDefaultExportFromCjs(distExports);
function interop(mod) {
return mod.default || mod;
}
const katexPlugin = interop(katexPluginModule);
const plugin$1 = definePlugin({
name: name$2,
config,
transform(transformHooks) {
var _a2, _b, _c, _d;
let loading;
const preloadScripts2 = ((_b = (_a2 = plugin$1.config) == null ? void 0 : _a2.preloadScripts) == null ? void 0 : _b.map(
(item) => patchJSItem(transformHooks.transformer.urlBuilder, item)
)) || [];
const autoload = () => {
loading || (loading = loadJS(preloadScripts2));
return loading;
};
const renderKatex = (source, displayMode) => {
const { katex } = window;
if (katex) {
return katex.renderToString(source, {
displayMode,
throwOnError: false
});
}
autoload().then(() => {
transformHooks.retransform.call();
});
return source;
};
let enableFeature = noop;
transformHooks.parser.tap((md) => {
md.use(katexPlugin);
["math_block", "math_inline"].forEach((key) => {
const fn = (tokens, idx) => {
enableFeature();
const result = renderKatex(tokens[idx].content, !!tokens[idx].block);
return result;
};
md.renderer.rules[key] = fn;
});
});
transformHooks.beforeParse.tap((_, context) => {
enableFeature = () => {
context.features[name$2] = true;
};
});
transformHooks.afterParse.tap((_, context) => {
var _a3;
const markmap = (_a3 = context.frontmatter) == null ? void 0 : _a3.markmap;
if (markmap) {
["extraJs", "extraCss"].forEach((key) => {
var _a4, _b2;
const value = markmap[key];
if (value) {
markmap[key] = addDefaultVersions(
value,
name$2,
((_b2 = (_a4 = plugin$1.config) == null ? void 0 : _a4.versions) == null ? void 0 : _b2.katex) || ""
);
}
});
}
});
return {
styles: (_c = plugin$1.config) == null ? void 0 : _c.styles,
scripts: (_d = plugin$1.config) == null ? void 0 : _d.scripts
};
}
});
const name$1 = "npmUrl";
const pluginNpmUrl = definePlugin({
name: name$1,
transform(transformHooks) {
transformHooks.afterParse.tap((_, context) => {
const { frontmatter } = context;
const markmap = frontmatter == null ? void 0 : frontmatter.markmap;
if (markmap) {
["extraJs", "extraCss"].forEach((key) => {
const value = markmap[key];
if (value) {
markmap[key] = value.map((path) => {
if (path.startsWith("npm:")) {
return transformHooks.transformer.urlBuilder.getFullUrl(
path.slice(4)
);
}
return path;
});
}
});
}
});
return {};
}
});
const name = "sourceLines";
const plugin = definePlugin({
name,
transform(transformHooks) {
let frontmatterLines = 0;
transformHooks.beforeParse.tap((_md, context) => {
var _a2;
frontmatterLines = ((_a2 = context.frontmatterInfo) == null ? void 0 : _a2.lines) || 0;
});
transformHooks.parser.tap((md) => {
md.renderer.renderAttrs = wrapFunction(
md.renderer.renderAttrs,
(renderAttrs, token) => {
if (token.block && token.map) {
const lineRange = token.map.map((line) => line + frontmatterLines);
token.attrSet("data-lines", lineRange.join(","));
}
return renderAttrs(token);
}
);
if (md.renderer.rules.fence) {
md.renderer.rules.fence = wrapFunction(
md.renderer.rules.fence,
(fence2, tokens, idx, ...rest) => {
let result = fence2(tokens, idx, ...rest);
const token = tokens[idx];
if (result.startsWith("") && token.map) {
const lineRange = token.map.map(
(line) => line + frontmatterLines
);
result = result.slice(0, 4) + ` data-lines="${lineRange.join(",")}"` + result.slice(4);
}
return result;
}
);
}
});
return {};
}
});
const plugins = [
pluginFrontmatter,
plugin$1,
plugin$2,
pluginNpmUrl,
plugin$3,
plugin
];
const builtInPlugins = plugins;
function cleanNode(node) {
while (!node.content && node.children.length === 1) {
node = node.children[0];
}
while (node.children.length === 1 && !node.children[0].content) {
node = {
...node,
children: node.children[0].children
};
}
return {
...node,
children: node.children.map(cleanNode)
};
}
class Transformer {
constructor(plugins2 = builtInPlugins) {
this.assetsMap = {};
this.urlBuilder = new UrlBuilder();
this.hooks = createTransformHooks(this);
this.plugins = plugins2.map(
(plugin2) => typeof plugin2 === "function" ? plugin2() : plugin2
);
const assetsMap = {};
for (const { name: name2, transform } of this.plugins) {
assetsMap[name2] = transform(this.hooks);
}
this.assetsMap = assetsMap;
const md = initializeMarkdownIt();
this.md = md;
this.hooks.parser.call(md);
}
transform(content, fallbackParserOptions) {
var _a2;
const context = {
content,
features: {},
parserOptions: fallbackParserOptions
};
this.hooks.beforeParse.call(this.md, context);
let { content: rawContent } = context;
if (context.frontmatterInfo)
rawContent = rawContent.slice(context.frontmatterInfo.offset);
const html2 = this.md.render(rawContent, {});
this.hooks.afterParse.call(this.md, context);
const root2 = cleanNode(buildTree(html2, context.parserOptions));
root2.content || (root2.content = `${((_a2 = context.frontmatter) == null ? void 0 : _a2.title) || ""}`);
return { ...context, root: root2 };
}
resolveJS(item) {
return patchJSItem(this.urlBuilder, item);
}
resolveCSS(item) {
return patchCSSItem(this.urlBuilder, item);
}
/**
* Get all assets from enabled plugins or filter them by plugin names as keys.
*/
getAssets(keys) {
const styles2 = [];
const scripts = [];
keys ?? (keys = this.plugins.map((plugin2) => plugin2.name));
for (const assets of keys.map((key) => this.assetsMap[key])) {
if (assets) {
if (assets.styles) styles2.push(...assets.styles);
if (assets.scripts) scripts.push(...assets.scripts);
}
}
return {
styles: styles2.map((item) => this.resolveCSS(item)),
scripts: scripts.map((item) => this.resolveJS(item))
};
}
/**
* Get used assets by features object returned by `transform`.
*/
getUsedAssets(features) {
const keys = this.plugins.map((plugin2) => plugin2.name).filter((name2) => features[name2]);
return this.getAssets(keys);
}
}
const transformerVersions = {
"markmap-lib": "0.18.12"
};
exports.Transformer = Transformer;
exports.builtInPlugins = builtInPlugins;
exports.patchCSSItem = patchCSSItem;
exports.patchJSItem = patchJSItem;
exports.transformerVersions = transformerVersions;
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
})(this.markmap = this.markmap || {}, window.katex);