synapse-react-client
Version:
[](https://badge.fury.io/js/synapse-react-client) [](https://github.com/prettier/prettie
236 lines (235 loc) • 7.17 kB
JavaScript
import { jsx as n, jsxs as C, Fragment as O } from "react/jsx-runtime";
import { handleLinkClicks as _, processMath as R, addIdsToReferenceWidgets as j, addIdsToTocWidgets as A, fixInvalidNesting as B, decodeXml as D } from "./MarkdownUtils.js";
import { useGetWikiPage as H, useGetWikiAttachments as F } from "../../synapse-queries/wiki/useWiki.js";
import { sanitize as G } from "../../utils/functions/SanitizeHtmlUtils.js";
import { Typography as p, Link as J } from "@mui/material";
import X from "@sage-bionetworks/markdown-it-container";
import { ObjectType as Y } from "@sage-bionetworks/synapse-types";
import q from "markdown-it";
import z from "markdown-it-br";
import K from "markdown-it-center-text";
import U from "markdown-it-inline-comments";
import Q from "markdown-it-strikethrough-alt";
import V from "markdown-it-sub-alt";
import Z from "markdown-it-sup-alt";
import * as b from "markdown-it-synapse";
import $ from "markdown-it-synapse";
import ee from "markdown-it-synapse-heading";
import te from "markdown-it-synapse-math";
import ne from "markdown-it-synapse-table";
import { useRef as re, useEffect as I, useMemo as N } from "react";
import { ErrorBanner as oe } from "../error/ErrorBanner.js";
import "@mui/material/Skeleton";
import { SkeletonTable as ie } from "../Skeleton/SkeletonTable.js";
import "lodash-es";
import E from "./MarkdownWidget.js";
import { SynapseWikiContextProvider as ae } from "./SynapseWikiContext.js";
import se from "./widget/Bookmarks.js";
const de = "There is no content.", y = q({ html: !0 });
b.init_markdown_it(
y,
V,
Z,
K,
ee,
ne,
Q,
X,
U,
z
);
const S = "";
y.use($, S, "https://synapse.org").use(
te,
S
);
function me(m) {
const { markdown: t, renderInline: o } = m, e = N(() => (b.resetFootnotes(), T(o, t), T(o, b.footnotes()).__html), [t, o]);
return e.length > 0 ? /* @__PURE__ */ n("div", { children: /* @__PURE__ */ n(se, { footnotes: e }) }) : null;
}
function T(m, t) {
if (!t)
return { __html: "" };
const o = m ? y.renderInline(t) : y.render(t);
return { __html: G(o) };
}
function ce(m) {
const { widgetParams: t, originalMarkup: o } = m, e = D(t), c = e.indexOf("?");
if (c === -1)
return /* @__PURE__ */ n(
E,
{
widgetType: e,
widgetParamsMapped: {},
originalMarkup: o
},
JSON.stringify(e)
);
const i = e.substring(0, c), a = {};
return e.substring(c + 1).split("&").forEach((s) => {
let [l, r] = s.split("=");
r = decodeURIComponent(r), a[l] = r;
}), /* @__PURE__ */ n(
E,
{
widgetType: i,
widgetParamsMapped: a,
originalMarkup: o
},
JSON.stringify(e)
);
}
function le(m) {
const { renderInline: t, markdown: o, isLoading: e, showPlaceholderIfNoWikiContent: c } = m, i = N(() => {
let s = T(t, o).__html;
return s = j(s), s = A(s), s;
}, [o, t]), a = N(() => {
if (i.length > 0) {
const l = new DOMParser().parseFromString(i, "text/html");
return B(l.body), l.body;
}
return null;
}, [i]);
if (a)
return /* @__PURE__ */ n(W, { element: a, markdown: i });
if (!e && c && i === "")
return /* @__PURE__ */ n(p, { variant: "body1Italic", sx: { mb: 1 }, children: de });
}
function W(m) {
const { element: t, markdown: o } = m;
if (t.nodeType === Node.TEXT_NODE)
return /* @__PURE__ */ n(O, { children: t.textContent });
if (t.nodeType === Node.ELEMENT_NODE && t instanceof HTMLElement) {
const e = t.tagName.toLowerCase(), c = t.getAttribute("data-widgetparams");
if (c)
return /* @__PURE__ */ n(
ce,
{
widgetParams: c,
originalMarkup: o
}
);
const i = t.attributes, a = {};
for (let f = 0; f < i.length; f++) {
let k = "", w = "";
const g = i.item(f);
g && (k = g.name, w = g.value), k && w && (a[k] = w);
}
const { style: s, class: l, ...r } = a;
if (r.className = l, s && (r.STYLE = s), t.childNodes.length === 0)
return /* @__PURE__ */ n(e, { ...r });
const d = Array.from(t.childNodes).map((f, k) => /* @__PURE__ */ n(W, { element: f, markdown: o }, k));
switch (e) {
case "p":
return /* @__PURE__ */ n(p, { variant: "body1", ...r, component: e, children: d });
case "span":
return /* @__PURE__ */ n(p, { variant: "body1", ...r, component: e, children: d });
case "h1":
return /* @__PURE__ */ n(p, { variant: "headline1", ...r, component: e, children: d });
case "h2":
return /* @__PURE__ */ n(p, { variant: "headline2", ...r, component: e, children: d });
case "h3":
return /* @__PURE__ */ n(p, { variant: "headline3", ...r, component: e, children: d });
case "ol":
return /* @__PURE__ */ n(p, { variant: "body1", ...r, component: e, children: d });
case "ul":
return /* @__PURE__ */ n(p, { variant: "body1", ...r, component: e, children: d });
case "a":
return /* @__PURE__ */ n(J, { ...r, component: e, children: d });
default:
return e === "body" ? d : /* @__PURE__ */ n(e, { ...r, children: d });
}
}
return null;
}
function Be(m) {
const {
ownerId: t,
wikiId: o,
objectType: e = Y.ENTITY,
markdown: c,
onMarkdownProcessingDone: i,
renderInline: a = !1,
loadingSkeletonRowCount: s,
showPlaceholderIfNoWikiContent: l = !1
} = m, r = re(null), {
data: d,
isLoading: f,
error: k
} = H(
{
ownerObjectId: t,
wikiPageId: o,
ownerObjectType: e
},
{ enabled: !!t }
), {
data: w,
isLoading: g,
error: x
} = F(
{
ownerObjectId: t,
wikiPageId: o,
ownerObjectType: e
},
{
enabled: !!t
}
), P = f || g, M = k || x;
I(() => {
const h = r.current;
return h && h.addEventListener("click", _), () => {
h && h.removeEventListener("click", _);
};
}, []);
const u = c || d?.markdown || "";
I(() => {
u && i && i(T(a, u).__html);
}, [d, i, a]), I(() => {
u && R(r);
}, [u]);
const L = s ? /* @__PURE__ */ n(ie, { numCols: 1, numRows: s }) : /* @__PURE__ */ n("span", { className: "spinner" });
if (M)
return /* @__PURE__ */ n(oe, { error: M });
const v = /* @__PURE__ */ C(
ae,
{
wikiContext: {
ownerId: m.ownerId,
wikiId: m.wikiId,
wikiPage: d,
fileHandles: w
},
children: [
P && L,
(u || l) && /* @__PURE__ */ n(
le,
{
markdown: u,
renderInline: a,
isLoading: P,
showPlaceholderIfNoWikiContent: l
}
),
u && /* @__PURE__ */ n(me, { renderInline: a, markdown: u })
]
}
);
return a ? /* @__PURE__ */ n(
"span",
{
"data-testid": "markdown",
className: "markdown markdown-inline",
ref: r,
children: v
}
) : /* @__PURE__ */ n("div", { "data-testid": "markdown", className: "markdown", ref: r, children: v });
}
export {
Be as Markdown,
Be as MarkdownSynapse,
de as NO_WIKI_CONTENT,
Be as default
};
//# sourceMappingURL=MarkdownSynapse.js.map