You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
681 B
32 lines
681 B
// 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 |
|
};
|
|
|