Linkify messages

This commit is contained in:
Simon Ser
2020-06-25 17:26:40 +02:00
parent 78002c22ee
commit 2bb8f68f6f
5 changed files with 41 additions and 3 deletions

View File

@@ -3,3 +3,6 @@ export * from "/node_modules/preact/dist/preact.module.js";
import { h } from "/node_modules/preact/dist/preact.module.js";
import htm from "/node_modules/htm/dist/htm.module.js";
export const html = htm.bind(h);
import "/node_modules/anchorme/dist/browser/anchorme.min.js";
export const anchorme = window.anchorme;

28
lib/linkify.js Normal file
View File

@@ -0,0 +1,28 @@
import { anchorme, html } from "/lib/index.js";
export default function linkify(text) {
var links = anchorme.list(text);
var children = [];
var last = 0;
links.forEach((match) => {
children.push(text.substring(last, match.start));
var proto = match.protocol || "https://";
if (match.isEmail) {
proto = "mailto:";
}
var url = match.string;
if (!url.startsWith(proto)) {
url = proto + url;
}
children.push(html`<a href=${url} target="_blank" rel="noreferrer noopener">${match.string}</a>`);
last = match.end;
});
children.push(text.substring(last));
return children;
}