fix: fix bbcode auto transform logic #196

pull/199/head
moonrailgun 1 year ago
parent 5cf5bb738f
commit 5f814594e8

@ -42,26 +42,6 @@ regMessageTextDecorators(() => ({
return `[${h}]${plain}[/card]`; return `[${h}]${plain}[/card]`;
}, },
mention: (userId, userName) => `[at=${userId}]${userName}[/at]`, mention: (userId, userName) => `[at=${userId}]${userName}[/at]`,
emoji: (emojiCode) => `[emoji]${stripColons(emojiCode)}[/emoji]`, emoji: (emojiCode) => `[emoji]${emojiCode}[/emoji]`,
serialize: (plain: string) => (serialize ? serialize(plain) : plain), serialize: (plain: string) => (serialize ? serialize(plain) : plain),
})); }));
/**
* Removes colons on either side
* of the string if present
*/
function stripColons(str: string): string {
const colonIndex = str.indexOf(':');
if (colonIndex > -1) {
// :emoji: (http://www.emoji-cheat-sheet.com/)
if (colonIndex === str.length - 1) {
str = str.substring(0, colonIndex);
return stripColons(str);
} else {
str = str.substr(colonIndex + 1);
return stripColons(str);
}
}
return str;
}

@ -1,4 +1,5 @@
import { getMessageTextDecorators } from '@/plugin/common'; import { getMessageTextDecorators } from '@/plugin/common';
import { isEmojiCode, stripColons } from '../Emoji/utils';
const emojiNameRegex = /:([a-zA-Z0-9_\-\+]+):/g; const emojiNameRegex = /:([a-zA-Z0-9_\-\+]+):/g;
@ -11,9 +12,14 @@ export function preprocessMessage(message: string): string {
/** /**
* emoji * emoji
*/ */
message = message.replace(emojiNameRegex, (code) => message = message.replace(emojiNameRegex, (matched) => {
getMessageTextDecorators().emoji(code) const code = stripColons(matched);
); if (isEmojiCode(code)) {
return getMessageTextDecorators().emoji(code);
} else {
return matched;
}
});
return message; return message;
} }

@ -0,0 +1,25 @@
import { emojiData } from './const';
export function isEmojiCode(code: string): boolean {
return Object.keys(emojiData.emojis).includes(code);
}
/**
* Removes colons on either side
* of the string if present
*/
export function stripColons(str: string): string {
const colonIndex = str.indexOf(':');
if (colonIndex > -1) {
// :emoji: (http://www.emoji-cheat-sheet.com/)
if (colonIndex === str.length - 1) {
str = str.substring(0, colonIndex);
return stripColons(str);
} else {
str = str.substr(colonIndex + 1);
return stripColons(str);
}
}
return str;
}
Loading…
Cancel
Save