mirror of
https://codeberg.org/emersion/gamja.git
synced 2024-11-15 03:15:01 -05:00
405bc51c26
References: https://todo.sr.ht/~emersion/gamja/71
68 lines
1.6 KiB
JavaScript
68 lines
1.6 KiB
JavaScript
import { linkifyjs, html } from "./index.js";
|
|
|
|
linkifyjs.options.defaults.defaultProtocol = "https";
|
|
|
|
linkifyjs.registerCustomProtocol("irc");
|
|
linkifyjs.registerCustomProtocol("ircs");
|
|
|
|
linkifyjs.registerPlugin("ircChannel", ({ scanner, parser, utils }) => {
|
|
const { POUND, DOMAIN, TLD, LOCALHOST, UNDERSCORE, DOT, HYPHEN } = scanner.tokens;
|
|
const START_STATE = parser.start;
|
|
|
|
const Channel = utils.createTokenClass("ircChannel", {
|
|
isLink: true,
|
|
toHref() {
|
|
return "irc:///" + this.toString();
|
|
},
|
|
});
|
|
|
|
const HASH_STATE = START_STATE.tt(POUND);
|
|
|
|
const CHAN_STATE = HASH_STATE.tt(DOMAIN, Channel);
|
|
HASH_STATE.tt(TLD, CHAN_STATE);
|
|
HASH_STATE.tt(LOCALHOST, CHAN_STATE);
|
|
HASH_STATE.tt(POUND, CHAN_STATE);
|
|
|
|
CHAN_STATE.tt(UNDERSCORE, CHAN_STATE);
|
|
CHAN_STATE.tt(DOMAIN, CHAN_STATE);
|
|
CHAN_STATE.tt(TLD, CHAN_STATE);
|
|
CHAN_STATE.tt(LOCALHOST, CHAN_STATE);
|
|
|
|
const CHAN_DIVIDER_STATE = CHAN_STATE.tt(DOT);
|
|
|
|
CHAN_DIVIDER_STATE.tt(UNDERSCORE, CHAN_STATE);
|
|
CHAN_DIVIDER_STATE.tt(DOMAIN, CHAN_STATE);
|
|
CHAN_DIVIDER_STATE.tt(TLD, CHAN_STATE);
|
|
CHAN_DIVIDER_STATE.tt(LOCALHOST, CHAN_STATE);
|
|
});
|
|
|
|
export default function linkify(text, onClick) {
|
|
let links = linkifyjs.find(text);
|
|
|
|
let children = [];
|
|
let last = 0;
|
|
links.forEach((match) => {
|
|
if (!match.isLink) {
|
|
return;
|
|
}
|
|
|
|
const prefix = text.substring(last, match.start)
|
|
children.push(prefix);
|
|
|
|
children.push(html`
|
|
<a
|
|
href=${match.href}
|
|
target="_blank"
|
|
rel="noreferrer noopener"
|
|
onClick=${onClick}
|
|
>${match.value}</a>
|
|
`);
|
|
|
|
last = match.end;
|
|
});
|
|
|
|
const suffix = text.substring(last)
|
|
children.push(suffix);
|
|
|
|
return children;
|
|
}
|