mirror of
https://codeberg.org/emersion/gamja.git
synced 2024-11-14 19:05:01 -05:00
42e0c939f3
Co-authored-by: Simon Ser <contact@emersion.fr> Closes: https://todo.sr.ht/~emersion/gamja/90
65 lines
1.5 KiB
JavaScript
65 lines
1.5 KiB
JavaScript
import { anchorme, html } from "./index.js";
|
|
|
|
function linkifyChannel(text, transformChannel) {
|
|
// Don't match punctuation at the end of the channel name
|
|
const channelRegex = /(^|\s)(#[^\s]+[^\s.?!…():;,])/gi;
|
|
|
|
let children = [];
|
|
let match;
|
|
let last = 0;
|
|
while ((match = channelRegex.exec(text)) !== null) {
|
|
let channel = match[2];
|
|
let start = match.index + match[1].length;
|
|
let end = start + match[2].length;
|
|
|
|
children.push(text.substring(last, start));
|
|
children.push(transformChannel(channel));
|
|
|
|
last = end;
|
|
}
|
|
children.push(text.substring(last));
|
|
|
|
return children;
|
|
}
|
|
|
|
export default function linkify(text, onChannelClick) {
|
|
function transformChannel(channel) {
|
|
function onClick(event) {
|
|
event.preventDefault();
|
|
onChannelClick(channel);
|
|
}
|
|
return html`
|
|
<a
|
|
href="irc:///${encodeURIComponent(channel)}"
|
|
onClick=${onClick}
|
|
>${channel}</a>`;
|
|
}
|
|
|
|
let links = anchorme.list(text);
|
|
|
|
let children = [];
|
|
let last = 0;
|
|
links.forEach((match) => {
|
|
const prefix = text.substring(last, match.start)
|
|
children.push(...linkifyChannel(prefix, transformChannel));
|
|
|
|
let proto = match.protocol || "https://";
|
|
if (match.isEmail) {
|
|
proto = "mailto:";
|
|
}
|
|
|
|
let 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;
|
|
});
|
|
|
|
const suffix = text.substring(last)
|
|
children.push(...linkifyChannel(suffix, transformChannel));
|
|
|
|
return children;
|
|
}
|