33 lines
681 B
JavaScript
33 lines
681 B
JavaScript
// nip30.ts
|
|
var EMOJI_SHORTCODE_REGEX = /:(\w+):/;
|
|
var regex = () => new RegExp(`\\B${EMOJI_SHORTCODE_REGEX.source}\\B`, "g");
|
|
function* matchAll(content) {
|
|
const matches = content.matchAll(regex());
|
|
for (const match of matches) {
|
|
try {
|
|
const [shortcode, name] = match;
|
|
yield {
|
|
shortcode,
|
|
name,
|
|
start: match.index,
|
|
end: match.index + shortcode.length
|
|
};
|
|
} catch (_e) {
|
|
}
|
|
}
|
|
}
|
|
function replaceAll(content, replacer) {
|
|
return content.replaceAll(regex(), (shortcode, name) => {
|
|
return replacer({
|
|
shortcode,
|
|
name
|
|
});
|
|
});
|
|
}
|
|
export {
|
|
EMOJI_SHORTCODE_REGEX,
|
|
matchAll,
|
|
regex,
|
|
replaceAll
|
|
};
|