synapse-react-client
Version:
[](https://badge.fury.io/js/synapse-react-client) [](https://github.com/prettier/prettie
187 lines (186 loc) • 4.74 kB
JavaScript
import c from "katex";
import l from "markdown-it";
import { truncateString as d } from "../../utils/functions/StringUtils.js";
function k(e) {
if (!e.current)
return;
const t = e.current.querySelectorAll('[id^="mathjax-"]'), n = new RegExp(/\\[()[\]]/, "g");
t.forEach((r) => {
if (r.textContent && !r.getAttribute("processed")) {
r.setAttribute("processed", "true");
const o = r.textContent.replace(n, "");
return c.render(o, r, {
output: "html",
throwOnError: !1
});
}
});
}
function I(e) {
const t = {
"&": "&",
">": ">",
"<": "<",
""": '"'
};
return e.replace(
/("|<|>|&)/g,
(n, r) => t[r]
);
}
function x(e) {
const t = /<span.*?id="wikiReference.*?<span.*? data-widgetparams.*?span>/g;
let n = 1;
return e.replace(t, () => {
const r = n;
return n += 1, `<a href="" id="ref${r}">[${r}]</a>`;
});
}
function y(e) {
const t = "SRC-header-";
let n = 1;
const r = /<h[1-6] toc="true">.*<\/h[1-6]>/gm;
return e.replace(r, (o) => {
const i = n;
return n += 1, `${o.substring(
0,
3
)} id="${t}${i}"${o.substring(3)}`;
});
}
function L(e) {
const t = e.target, n = e.currentTarget;
if (t.tagName === "A" || t.tagName === "BUTTON") {
const r = e.target;
if (r.id.substring(0, 3) === "ref") {
e.preventDefault();
const o = Number(n.id.substring(3)), i = n.querySelector(`#bookmark${o}`);
try {
i.scrollIntoView({
behavior: "smooth",
block: "center",
inline: "center"
});
} catch (s) {
console.log("error on scroll", s);
}
} else if (n.id !== null && r.getAttribute("data-anchor")) {
e.preventDefault();
const o = r.getAttribute("data-anchor"), i = n.querySelector(`[id="${o}"]`);
try {
i.scrollIntoView({
behavior: "smooth",
block: "center",
inline: "center"
});
} catch (s) {
console.log("error on scroll", s);
}
}
}
}
function u(e) {
const t = document.createElement("div");
return t.innerHTML = e, t.textContent || t.innerText || "";
}
function A(e, t) {
const n = {};
if (t)
n.markdown = e;
else if (e.includes("/wiki/")) {
const r = e.split("/"), o = r[0], i = r[2];
n.ownerId = o, n.wikiId = i;
} else e && (n.ownerId = e);
return n;
}
const f = new l(), M = (e, t) => {
if (!e || e.length === 0)
return "";
const n = f.render(e), r = u(n).replace(/\|/g, "").replace(/#/g, "").replace(/<Synapse widget>/g, "");
return t ? d(r, t) : r;
}, g = [
"address",
"article",
"aside",
"blockquote",
"canvas",
"dd",
"div",
"dl",
"dt",
"fieldset",
"figcaption",
"figure",
"footer",
"form",
"h1",
"h2",
"h3",
"h4",
"h5",
"h6",
"header",
"hr",
"li",
"main",
"nav",
"noscript",
"ol",
"p",
"pre",
"section",
"table",
"tfoot",
"ul",
"video"
], p = ["span", "a", "em", "strong", "b", "i", "u", "sub", "sup"], m = ["table", "thead", "tbody", "tr", "tfoot"], h = ["badge", "image", "imageLink", "buttonlink"], T = (e) => {
if (e.nodeType !== Node.ELEMENT_NODE)
return !1;
const n = e.tagName.toLowerCase();
return p.includes(n);
}, a = (e) => {
if (e.nodeType !== Node.ELEMENT_NODE)
return !1;
const t = e, n = t.tagName.toLowerCase(), r = g.includes(n), o = t.getAttribute("data-widgetparams"), i = o !== null && !h.includes(o.split("?")[0]);
return r || i;
};
function b(e, t) {
return !(a(e) && T(t) || t.tagName.toLowerCase() === "button" && e.tagName.toLowerCase() === "button" || t.tagName.toLowerCase() === "p" && a(e));
}
function E(e, t = []) {
if (e.nodeType === Node.TEXT_NODE) {
const r = e.parentNode?.tagName?.toLowerCase();
m.includes(r) && !e.textContent?.trim() && e.parentNode?.removeChild(e);
return;
}
if (e.nodeType !== Node.ELEMENT_NODE)
return;
const n = e;
for (let r = t.length - 1; r >= 0; r--) {
const o = t[r];
if (!b(n, o)) {
const i = n.ownerDocument.createElement("div");
for (Array.from(o.attributes).forEach(
(s) => i.setAttribute(s.name, s.value)
); o.firstChild; )
i.appendChild(o.firstChild);
o.parentNode && o.parentNode.replaceChild(i, o), t[r] = i;
}
}
t.push(n), Array.from(n.childNodes).forEach((r) => {
E(r, t);
}), t.pop();
}
export {
x as addIdsToReferenceWidgets,
y as addIdsToTocWidgets,
I as decodeXml,
E as fixInvalidNesting,
L as handleLinkClicks,
a as isBlockLevelElement,
M as markdownToPlainText,
k as processMath,
u as stripHTML,
A as transformStringIntoMarkdownProps
};
//# sourceMappingURL=MarkdownUtils.js.map