showdown-mathjax
Version:
Showdown Extensions for mathjax
71 lines (69 loc) • 1.83 kB
JavaScript
import Showdown from '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.extension("showdownMathjax", showdownMathjax());
var src_default = showdownMathjax;
export { src_default as default };
//# sourceMappingURL=out.js.map
//# sourceMappingURL=index.js.map