@nextcloud/vue
Version:
Nextcloud vue components
48 lines (47 loc) • 1.33 kB
JavaScript
import escapeHTML from "escape-html";
import { Options, tokenize } from "linkifyjs";
function linkifyString(str) {
const options = new Options({
defaultProtocol: "https",
target: "_blank",
className: "external linkified",
attributes: {
rel: "nofollow noopener noreferrer"
}
}, defaultRender);
const tokens = tokenize(str);
const result = [];
for (const token of tokens) {
if (token.t === "nl" && options.get("nl2br")) {
result.push("<br>\n");
} else if (!token.isLink || !options.check(token)) {
result.push(escapeHTML(token.toString()));
} else {
result.push(options.render(token));
}
}
return result.join("");
}
function escapeAttr(href) {
return href.replace(/"/g, """);
}
function attributesToString(attributes) {
const result = [];
for (const attr in attributes) {
const val = attributes[attr] + "";
result.push(`${attr}="${escapeAttr(val)}"`);
}
return result.join(" ");
}
function defaultRender({ tagName, attributes, content }) {
return `<${tagName} ${attributesToString(attributes)}>${escapeHTML(content)}</${tagName}>`;
}
const directive = function(el, { value }) {
if (value?.linkify === true) {
el.innerHTML = linkifyString(value.text);
}
};
export {
directive as default
};
//# sourceMappingURL=index.mjs.map