UNPKG

@nextcloud/vue

Version:
124 lines (123 loc) 7.53 kB
import { N as y } from "./referencePickerModal-2b16b319.mjs"; import f from "@nextcloud/axios"; import { generateOcsUrl as d } from "@nextcloud/router"; import { n as g } from "./_plugin-vue2_normalizer-5b4c43a4.mjs"; import { visit as b, SKIP as v } from "unist-util-visit"; import { u as l } from "unist-builder"; import { unified as w } from "unified"; import _ from "remark-parse"; import N from "remark-breaks"; import R from "remark-rehype"; import j from "rehype-react"; import A from "rehype-external-links"; const m = /(\s|^)(https?:\/\/)((?:[-A-Z0-9+_]+\.)+[-A-Z]+(?:\/[-A-Z0-9+&@#%?=~_|!:,.;()]*)*)(\s|$)/ig, x = /(\s|\(|^)((https?:\/\/)((?:[-A-Z0-9+_]+\.)+[-A-Z0-9]+(?::[0-9]+)?(?:\/[-A-Z0-9+&@#%?=~_|!:,.;()]*)*))(?=\s|\)|$)/ig, L = { name: "NcReferenceList", components: { NcReferenceWidget: y }, props: { text: { type: String, default: "" }, referenceData: { type: Object, default: null }, limit: { type: Number, default: 1 } }, data() { return { references: null, loading: !0 }; }, computed: { isVisible() { return this.loading || this.displayedReferences; }, values() { return this.referenceData ? this.referenceData : this.references ? Object.values(this.references) : []; }, firstReference() { var e; return (e = this.values[0]) != null ? e : null; }, displayedReferences() { return this.values.slice(0, this.limit); } }, watch: { text: "fetch" }, mounted() { this.fetch(); }, methods: { fetch() { if (this.loading = !0, this.referenceData) { this.loading = !1; return; } if (!new RegExp(m).exec(this.text)) { this.loading = !1; return; } this.resolve().then((e) => { this.references = e.data.ocs.data.references, this.loading = !1; }).catch((e) => { console.error("Failed to extract references", e), this.loading = !1; }); }, resolve() { const e = new RegExp(m).exec(this.text.trim()); return this.limit === 1 && e ? f.get(d("references/resolve", 2) + `?reference=${encodeURIComponent(e[0])}`) : f.post(d("references/extract", 2), { text: this.text, resolve: !0, limit: this.limit }); } } }; var M = function() { var e = this, i = e._self._c; return e.isVisible ? i("div", { staticClass: "widgets--list", class: { "icon-loading": e.loading } }, e._l(e.displayedReferences, function(t) { var r; return i("div", { key: (r = t == null ? void 0 : t.openGraphObject) == null ? void 0 : r.id }, [i("NcReferenceWidget", { attrs: { reference: t } })], 1); }), 0) : e._e(); }, O = [], $ = g(L, M, O, !1, null, "bd1fbb02", null, null); const u = $.exports, D = { name: "NcLink", props: { href: { type: String, required: !0 } }, render(e) { return e("a", { attrs: { href: this.href, rel: "noopener noreferrer", target: "_blank", class: "rich-text--external-link" } }, [this.href.trim()]); } }, Z = function({ autolink: e, useMarkdown: i }) { return function(t) { !i || !e || b(t, (r) => r.type === "text", (r, o, n) => { let s = k(r.value); return s = s.map((a) => typeof a == "string" ? l("text", a) : l("link", { url: a.props.href }, [l("text", a.props.href)])).filter((a) => a), n.children.splice(o, 1, ...s.flat()), [v, o + s.flat().length]; }); }; }, k = (e) => { let i = x.exec(e); const t = []; let r = 0; for (; i !== null; ) { let n = i[2], s, a = e.substring(r, i.index + i[1].length); n[0] === " " && (a += n[0], n = n.substring(1).trim()); const c = n[n.length - 1]; (c === "." || c === "," || c === ";" || i[0][0] === "(" && c === ")") && (n = n.substring(0, n.length - 1), s = c), t.push(a), t.push({ component: D, props: { href: n } }), s && t.push(s), r = i.index + i[0].length, i = x.exec(e); } t.push(e.substring(r)); const o = t.map((n) => typeof n == "string" ? n : n.props.href).join(""); return e === o ? t : (console.error("Failed to reassemble the chunked text: " + e), e); }, S = function() { return function(e) { b(e, (t) => t.type === "text", i); function i(t, r, o) { const n = t.value.split(/(\{[a-z\-_.0-9]+\})/ig).map((s, a, c) => { const h = s.match(/^\{([a-z\-_.0-9]+)\}$/i); if (!h) return l("text", s); const [, p] = h; return l("element", { tagName: `#${p}` }); }); o.children.splice(r, 1, ...n); } }; }, z = ({ h: e, context: i }, t) => (i.autolink && (t = k(t)), Array.isArray(t) ? t.map((r) => { if (typeof r == "string") return r; const { component: o, props: n } = r, s = o.name === "NcLink" ? void 0 : "rich-text--component"; return e(o, { props: n, class: s }); }) : t), C = { name: "NcRichText", components: { NcReferenceList: u }, props: { text: { type: String, default: "" }, arguments: { type: Object, default: () => ({}) }, referenceLimit: { type: Number, default: 0 }, references: { type: Object, default: null }, markdownCssClasses: { type: Object, default: () => ({ a: "rich-text--external-link", ol: "rich-text--ordered-list", ul: "rich-text--un-ordered-list", li: "rich-text--list-item", strong: "rich-text--strong", em: "rich-text--italic", h1: "rich-text--heading rich-text--heading-1", h2: "rich-text--heading rich-text--heading-2", h3: "rich-text--heading rich-text--heading-3", h4: "rich-text--heading rich-text--heading-4", h5: "rich-text--heading rich-text--heading-5", h6: "rich-text--heading rich-text--heading-6", hr: "rich-text--hr", table: "rich-text--table", pre: "rich-text--pre", code: "rich-text--code", blockquote: "rich-text--blockquote" }) }, useMarkdown: { type: Boolean, default: !1 }, autolink: { type: Boolean, default: !0 } }, methods: { renderPlaintext(e) { const i = this, t = this.text.split(/(\{[a-z\-_.0-9]+\})/ig).map(function(r, o, n) { const s = r.match(/^\{([a-z\-_.0-9]+)\}$/i); if (!s) return z({ h: e, context: i }, r); const a = s[1], c = i.arguments[a]; if (typeof c == "object") { const { component: h, props: p } = c; return e(h, { props: p, class: "rich-text--component" }); } return c ? e("span", { class: "rich-text--fallback" }, c) : r; }); return e("div", { class: "rich-text--wrapper" }, [e("div", {}, t.flat()), this.referenceLimit > 0 ? e("div", { class: "rich-text--reference-widget" }, [e(u, { props: { text: this.text, referenceData: this.references } })]) : null]); }, renderMarkdown(e) { const i = w().use(_).use(Z, { autolink: this.autolink, useMarkdown: this.useMarkdown }).use(N).use(R, { handlers: { component(t, r) { return t(r, r.component, { value: r.value }); } } }).use(S).use(A, { target: "_blank", rel: ["noopener noreferrer"] }).use(j, { createElement: (t, r, o) => { if (o = o == null ? void 0 : o.map((s) => typeof s == "string" ? s.replace(/&lt;/gmi, "<") : s), !t.startsWith("#")) return e(t, r, o); const n = this.arguments[t.slice(1)]; return n ? n.component ? e(n.component, { attrs: r, props: n.props, class: "rich-text--component" }, o) : e("span", r, [n]) : e("span", { attrs: r, class: "rich-text--fallback" }, [`{${t.slice(1)}}`]); }, prefix: !1 }).processSync(this.text.replace(/</gmi, "&lt;").replace(/&gt;/gmi, ">")).result; return e("div", { class: "rich-text--wrapper rich-text--wrapper-markdown" }, [i, this.referenceLimit > 0 ? e("div", { class: "rich-text--reference-widget" }, [e(u, { props: { text: this.text, referenceData: this.references } })]) : null]); } }, render(e) { return this.useMarkdown ? this.renderMarkdown(e) : this.renderPlaintext(e); } }, E = null, P = null; var q = g(C, E, P, !1, null, "f9d2c651", null, null); const X = q.exports; export { X as N, u as a };