2021-10-13 09:33:41 -04:00
|
|
|
import { linkifyjs, html } from "./index.js";
|
2020-06-25 11:26:40 -04:00
|
|
|
|
2021-10-13 09:33:41 -04:00
|
|
|
linkifyjs.options.defaults.defaultProtocol = "https";
|
2021-05-31 22:39:35 -04:00
|
|
|
|
2021-10-13 09:33:41 -04:00
|
|
|
linkifyjs.registerCustomProtocol("irc");
|
|
|
|
linkifyjs.registerCustomProtocol("ircs");
|
2021-05-31 22:39:35 -04:00
|
|
|
|
2021-10-13 09:33:41 -04:00
|
|
|
linkifyjs.registerPlugin('ircChannel', ({ scanner, parser, utils }) => {
|
|
|
|
const { POUND, DOMAIN, TLD, LOCALHOST, UNDERSCORE, DOT, HYPHEN } = scanner.tokens;
|
|
|
|
const START_STATE = parser.start;
|
2021-05-31 22:39:35 -04:00
|
|
|
|
2021-10-13 09:33:41 -04:00
|
|
|
const Channel = utils.createTokenClass('ircChannel', {
|
|
|
|
isLink: true,
|
|
|
|
toHref() {
|
|
|
|
return "irc:///" + this.toString();
|
|
|
|
},
|
|
|
|
});
|
2021-05-31 22:39:35 -04:00
|
|
|
|
2021-10-13 09:33:41 -04:00
|
|
|
const HASH_STATE = START_STATE.tt(POUND);
|
2021-05-31 22:39:35 -04:00
|
|
|
|
2021-10-13 09:33:41 -04:00
|
|
|
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);
|
2021-05-31 22:39:35 -04:00
|
|
|
|
2021-10-13 09:33:41 -04:00
|
|
|
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, onChannelClick) {
|
|
|
|
let links = linkifyjs.find(text);
|
2020-06-25 11:26:40 -04:00
|
|
|
|
2021-06-10 12:11:11 -04:00
|
|
|
let children = [];
|
|
|
|
let last = 0;
|
2020-06-25 11:26:40 -04:00
|
|
|
links.forEach((match) => {
|
2021-10-13 09:33:41 -04:00
|
|
|
if (!match.isLink) {
|
|
|
|
return;
|
2020-06-25 11:26:40 -04:00
|
|
|
}
|
|
|
|
|
2021-10-13 09:33:41 -04:00
|
|
|
const prefix = text.substring(last, match.start)
|
|
|
|
children.push(prefix);
|
2020-06-25 11:26:40 -04:00
|
|
|
|
2021-10-13 09:33:41 -04:00
|
|
|
// TODO: handle all irc/ircs URLs
|
|
|
|
if (match.href.startsWith("irc:///")) {
|
|
|
|
function onClick(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
onChannelClick(match.value);
|
|
|
|
}
|
|
|
|
children.push(html`
|
|
|
|
<a
|
|
|
|
href="${match.href}"
|
|
|
|
onClick=${onClick}
|
|
|
|
>${match.value}</a>
|
|
|
|
`);
|
|
|
|
} else {
|
|
|
|
children.push(html`
|
|
|
|
<a
|
|
|
|
href=${match.href}
|
|
|
|
target="_blank"
|
|
|
|
rel="noreferrer noopener"
|
|
|
|
>${match.value}</a>
|
|
|
|
`);
|
|
|
|
}
|
2020-06-25 11:26:40 -04:00
|
|
|
|
|
|
|
last = match.end;
|
|
|
|
});
|
2021-05-31 22:39:35 -04:00
|
|
|
|
|
|
|
const suffix = text.substring(last)
|
2021-10-13 09:33:41 -04:00
|
|
|
children.push(suffix);
|
2020-06-25 11:26:40 -04:00
|
|
|
|
|
|
|
return children;
|
|
|
|
}
|