showdown-mathjax
Version:
Showdown Extensions for mathjax
77 lines (72 loc) • 2.01 kB
JavaScript
;
var Showdown = require('showdown');
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
var Showdown__default = /*#__PURE__*/_interopDefault(Showdown);
// src/index.ts
function showdownMathjax() {
const ext = [
{
type: "lang",
filter: (text) => {
return text.replace(/\\\((.*?)\\\)/g, (match, p1) => {
return "<mathxxxjax>" + encode("\\(" + escapehtml(p1) + "\\)") + "</mathxxxjax>";
});
}
},
{
type: "lang",
filter: (text) => {
return text.replace(/\\\[([\s\S]*?)\\\]/g, (match, p1) => {
return "<mathxxxjax>" + encode("\\[" + escapehtml(p1) + "\\]") + "</mathxxxjax>";
});
}
},
{
type: "output",
filter: (text) => {
return text.replace(/<mathxxxjax>(.*?)<\/mathxxxjax>/g, (match, p1) => {
return decode(p1);
});
}
},
{
type: "output",
filter: (text) => {
const scriptTag = `
<script>
var script = document.createElement("script");
script.id = "MathJax-script";
script.async = true;
script.src =
"https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js";
document.head.appendChild(script);
</script>
`;
return scriptTag + text;
}
}
];
return ext;
}
function escapehtml(str) {
return str.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
}
function encode(text) {
if (typeof Buffer === "function") {
return Buffer.from(text).toString("base64");
} else {
return btoa(text);
}
}
function decode(text) {
if (typeof Buffer === "function") {
return Buffer.from(text, "base64").toString();
} else {
return atob(text);
}
}
Showdown__default.default.extension("showdownMathjax", showdownMathjax());
var src_default = showdownMathjax;
module.exports = src_default;
//# sourceMappingURL=out.js.map
//# sourceMappingURL=index.cjs.map