Use RegExp match indices in linkifyChannel

This commit is contained in:
Simon Ser 2021-06-01 09:53:11 +02:00
parent 0bcd044f10
commit 74d9dea5bb

View file

@ -3,18 +3,18 @@ import { anchorme, html } from "./index.js";
function linkifyChannel(text, transformChannel) {
var children = [];
// TODO: Don't match punctuation
const channelRegex = /(^|\s)(#[^\s]+)/gid;
const channelRegex = /(?:^|\s)(#[^\s]+)/gid;
let match;
var last = 0;
while ((match = channelRegex.exec(text)) !== null) {
const [_, spaces, channel] = match;
var channel = match[1];
var [start, end] = match.indices[1];
const start = match.index + spaces.length;
children.push(text.substring(last, start));
children.push(transformChannel(channel));
last = match.index + spaces.length + channel.length;
last = end;
}
children.push(text.substring(last));