Linkify messages

This commit is contained in:
Simon Ser 2020-06-25 17:26:40 +02:00
parent 78002c22ee
commit 2bb8f68f6f
No known key found for this signature in database
GPG key ID: 0FDE7BE0E88F5E48
5 changed files with 41 additions and 3 deletions

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;
}