Skip to content

Commit

Permalink
fix: fix bbcode auto transform logic #196
Browse files Browse the repository at this point in the history
  • Loading branch information
moonrailgun committed Dec 30, 2023
1 parent 5cf5bb7 commit 5f81459
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 24 deletions.
22 changes: 1 addition & 21 deletions client/web/plugins/com.msgbyte.bbcode/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,6 @@ regMessageTextDecorators(() => ({
return `[${h}]${plain}[/card]`;
},
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),
}));

/**
* 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;
}
12 changes: 9 additions & 3 deletions client/web/src/components/ChatBox/preprocessMessage.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getMessageTextDecorators } from '@/plugin/common';
import { isEmojiCode, stripColons } from '../Emoji/utils';

const emojiNameRegex = /:([a-zA-Z0-9_\-\+]+):/g;

Expand All @@ -11,9 +12,14 @@ export function preprocessMessage(message: string): string {
/**
* 预加工emoji
*/
message = message.replace(emojiNameRegex, (code) =>
getMessageTextDecorators().emoji(code)
);
message = message.replace(emojiNameRegex, (matched) => {
const code = stripColons(matched);
if (isEmojiCode(code)) {
return getMessageTextDecorators().emoji(code);
} else {
return matched;
}
});

return message;
}
25 changes: 25 additions & 0 deletions client/web/src/components/Emoji/utils.ts
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 5f81459

Please sign in to comment.