UNPKG

quill2-smart-links

Version:

Autodetects links with format "http(s)://..." that were entered or pasted into Quill 2.0 and converts them into <a> elements.

22 lines (21 loc) 796 B
//#region src/main.ts var e = /https?:\/\/[^\s]+/, t = class { constructor(t, n) { t.on("text-change", function(r) { let i = t.getSelection(!1)?.index; if (i == null) return; let [a] = t.getLeaf(i); if (a == null) return; let [o] = r?.ops?.filter((e) => e.insert)?.map((e) => e.insert) ?? [null], s = a.value(); if (!s || typeof s != "string" || a.parent.domNode.localName === "a") return; let c = ["\n", " "].find((e) => e === o), l = n.linkRegex ?? e, [u] = s.match(l) ?? [null]; if (u === null) return; let d = s.indexOf(u); if (d === null) return; let f = t.getIndex(a), p = f + d, m = p + u.length; !c && i <= m && i > p || (t.deleteText(f + d, u.length, "api"), t.insertText(f + d, u, "link", u)); }); } }; //#endregion export { t as SmartLinks };