Tune regular expressions

Signed-off-by: Anton Alexeyev <alexeyev.anton@gmail.com>
This commit is contained in:
Anton Alexeyev 2025-04-30 16:38:10 +07:00
parent 9bfb7ebf06
commit 752638f448
2 changed files with 6 additions and 9 deletions

View File

@ -26,11 +26,11 @@ import type { EmojiWithGroup, EmojiHierarchy } from '.'
export const emojiRegex = EMOJI_REGEX export const emojiRegex = EMOJI_REGEX
export const emojiGlobalRegex = new RegExp(EMOJI_REGEX.source, EMOJI_REGEX.flags + 'g') export const emojiGlobalRegex = new RegExp(EMOJI_REGEX.source, EMOJI_REGEX.flags + 'g')
export const emoticonRegex = EMOTICON_REGEX export const emoticonRegex = new RegExp(`(?:^|\\s)(${EMOTICON_REGEX.source})$`)
export const emoticonGlobalRegex = new RegExp(EMOTICON_REGEX.source, EMOTICON_REGEX.flags + 'g') export const emoticonGlobalRegex = new RegExp(`(?<!\\S)${EMOTICON_REGEX.source}(?!\\S)`, EMOTICON_REGEX.flags + 'g')
export const shortcodeRegex = SHORTCODE_REGEX export const shortcodeRegex = new RegExp(`(?:^|\\s)(${SHORTCODE_REGEX.source})$`)
export const shortcodeGlobalRegex = new RegExp(SHORTCODE_REGEX.source, SHORTCODE_REGEX.flags + 'g') export const shortcodeGlobalRegex = new RegExp(`(?<!\\S)${SHORTCODE_REGEX.source}(?!\\S)`, SHORTCODE_REGEX.flags + 'g')
let availableEmojis: Emoji[] let availableEmojis: Emoji[]
@ -82,12 +82,9 @@ export async function updateEmojis (lang?: string): Promise<void> {
} }
export function getEmojiForShortCode (shortcode: string | undefined): string | undefined { export function getEmojiForShortCode (shortcode: string | undefined): string | undefined {
console.log('getEmojiForShortCode', shortcode) shortcode = shortcode?.slice(1, -1)
if (shortcode === undefined) return undefined if (shortcode === undefined) return undefined
shortcode = shortcode.slice(1, -1)
console.log(shortcode)
const result = availableEmojis.find(e => e.shortcodes?.includes(shortcode)) const result = availableEmojis.find(e => e.shortcodes?.includes(shortcode))
console.log(result)
return result === undefined ? undefined : result.emoji return result === undefined ? undefined : result.emoji
} }

View File

@ -28,7 +28,7 @@ function handleEmoji (
range: Range, range: Range,
match: ExtendedRegExpMatchArray, match: ExtendedRegExpMatchArray,
commands: SingleCommands, commands: SingleCommands,
getEmojiFunction: (emoticon: string | undefined) => string | undefined getEmojiFunction: (key: string | undefined) => string | undefined
): void { ): void {
const $from = state.doc.resolve(range.from) const $from = state.doc.resolve(range.from)
if (!isValidEmojiPosition($from)) { if (!isValidEmojiPosition($from)) {