Skip to content

Commit 7157316

Browse files
author
Tom Hutman
committed
wrap mentions in a str-chat__message-mention span by default instead of just bolding text
1 parent e286379 commit 7157316

File tree

4 files changed

+39
-39
lines changed

4 files changed

+39
-39
lines changed

src/styles/Message.scss

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -444,12 +444,6 @@
444444
}
445445

446446
&-mention {
447-
margin: 0;
448-
padding: 0;
449-
background: none;
450-
border: none;
451-
font-size: 16px;
452-
color: $primary-color;
453447
font-weight: $heavy-font-weight;
454448
}
455449

src/styles/MessageCommerce.scss

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -183,16 +183,6 @@
183183
text-decoration: none;
184184
}
185185

186-
&-mention {
187-
margin: 0;
188-
padding: 0;
189-
background: none;
190-
border: none;
191-
font-size: 16px;
192-
color: $primary-color;
193-
font-weight: $heavy-font-weight;
194-
}
195-
196186
&--inner {
197187
display: flex;
198188
flex-direction: column;

src/styles/MessageLivestream.scss

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,6 @@
3333
margin: 0;
3434
}
3535

36-
.str-chat__message-mention {
37-
font-size: 13px;
38-
line-height: 20px;
39-
margin: 0;
40-
41-
&:focus {
42-
outline: 1;
43-
}
44-
}
45-
4636
p {
4737
margin: 0;
4838
white-space: pre-line;

src/utils.tsx

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,30 @@ const emojiMarkdownPlugin = () => {
107107
return transform;
108108
};
109109

110+
const mentionsMarkdownPlugin = (mentionedUsersRegex: RegExp) => () => {
111+
function replace(match: RegExpMatchArray | null) {
112+
return {
113+
children: [{ type: 'text', value: match }],
114+
type: 'mention',
115+
};
116+
}
117+
118+
const transform = <T extends unknown>(markdownAST: T) => {
119+
findAndReplace(markdownAST, mentionedUsersRegex, replace);
120+
return markdownAST;
121+
};
122+
123+
return transform;
124+
};
125+
126+
const Mention: React.FC = ({ children }) => (
127+
<span className='str-chat__message-mention'>{children}</span>
128+
);
129+
110130
export const renderText = <Us extends DefaultUserType<Us> = DefaultUserType>(
111131
text?: string,
112132
mentioned_users?: UserResponse<Us>[],
133+
MentionComponent: React.ComponentType = Mention,
113134
) => {
114135
// take the @ mentions and turn them into markdown?
115136
// translate links
@@ -146,27 +167,32 @@ export const renderText = <Us extends DefaultUserType<Us> = DefaultUserType>(
146167
newText = newText.replace(value, `[${displayLink}](${encodeURI(href)})`);
147168
});
148169

149-
if (mentioned_users && mentioned_users.length) {
150-
for (let i = 0; i < mentioned_users.length; i++) {
151-
let username = mentioned_users[i].name || mentioned_users[i].id;
152-
153-
if (username) {
154-
username = escapeRegExp(username);
155-
}
170+
const plugins = [emojiMarkdownPlugin];
156171

157-
const nameMarkdown = `**@${username}**`;
158-
const nameRegex = new RegExp(`@${username}`, 'g');
172+
const mentioned_usernames = mentioned_users
173+
?.map((user) => user.name || user.id)
174+
.filter(Boolean)
175+
.map(escapeRegExp);
159176

160-
newText = newText.replace(nameRegex, nameMarkdown);
161-
}
177+
if (mentioned_usernames?.length) {
178+
const mentionedUsersRegex = new RegExp(
179+
mentioned_usernames.map((username) => `@${username}`).join('|'),
180+
'g',
181+
);
182+
plugins.push(mentionsMarkdownPlugin(mentionedUsersRegex));
162183
}
163184

185+
const renderers = {
186+
...markDownRenderers,
187+
mention: MentionComponent,
188+
};
189+
164190
return (
165191
<ReactMarkdown
166192
allowedTypes={allowedMarkups}
167193
escapeHtml={true}
168-
plugins={[emojiMarkdownPlugin]}
169-
renderers={markDownRenderers}
194+
plugins={plugins}
195+
renderers={renderers}
170196
source={newText}
171197
transformLinkUri={(uri) =>
172198
uri.startsWith('app://') ? uri : RootReactMarkdown.uriTransformer(uri)

0 commit comments

Comments
 (0)