@@ -77,9 +77,6 @@ export function wrapDisplayNameMentions(
77
77
DISPLAY_NAME_MENTIONS_PAT ,
78
78
( match , precedingChar , displayName ) => {
79
79
const suggestion = usersMap . get ( displayName ) ;
80
-
81
- // TODO Should we still build a mention tag so that it renders as an
82
- // invalid mention instead of plain text?
83
80
if ( ! suggestion ) {
84
81
return `${ precedingChar } ${ displayNameMention ( displayName ) } ` ;
85
82
}
@@ -150,26 +147,27 @@ export function unwrapMentions({
150
147
}
151
148
152
149
/**
153
- * A username for a mention that the backend "discarded" as invalid. Possible
154
- * reasons are: the user does not exist, belongs to a different group, is
155
- * nipsa'd, etc.
150
+ * The content of a mention tag that the backend "discarded" as invalid.
151
+ * Possible reasons are: the user does not exist, belongs to a different group,
152
+ * is nipsa'd, etc.
156
153
*/
157
- export type InvalidUsername = string ;
154
+ export type InvalidMentionContent = string ;
158
155
159
156
/**
160
157
* Replace an unprocessed mention tag with another element that represents the
161
158
* proper type of mention ('link', 'no-link' or 'invalid').
162
159
*/
163
160
function processAndReplaceMention (
164
161
unprocessedMention : HTMLElement ,
165
- mention ?: Mention ,
166
- ) : [ HTMLElement , Mention | InvalidUsername ] {
167
- const username = unprocessedMention . textContent ?? '' ;
168
- const mentionOrUsername = mention ?? username ;
162
+ mention : Mention | undefined ,
163
+ mentionMode : MentionMode ,
164
+ ) : [ HTMLElement , Mention | InvalidMentionContent ] {
165
+ const originalTagContent = unprocessedMention . textContent ?? '' ;
166
+ const mentionOrInvalidContent = mention ?? originalTagContent ;
169
167
170
168
// If this mention element has already been processed, return as is
171
169
if ( unprocessedMention . hasAttribute ( 'data-hyp-mention-type' ) ) {
172
- return [ unprocessedMention , mentionOrUsername ] ;
170
+ return [ unprocessedMention , mentionOrInvalidContent ] ;
173
171
}
174
172
175
173
const type =
@@ -180,7 +178,20 @@ function processAndReplaceMention(
180
178
181
179
processedMention . setAttribute ( 'data-hyp-mention' , '' ) ;
182
180
processedMention . setAttribute ( 'data-hyp-mention-type' , type ) ;
183
- processedMention . textContent = username ;
181
+
182
+ // For valid mentions, resolve the most recent username or display name, in
183
+ // case it has changed since the mention was created.
184
+ // The only exception is when a valid mention with an empty display name is
185
+ // resolved, in which case we fall back to the original tag content.
186
+ if ( ! mention ) {
187
+ processedMention . textContent = originalTagContent ;
188
+ } else if ( mentionMode === 'username' ) {
189
+ processedMention . textContent = `@${ mention . username } ` ;
190
+ } else {
191
+ processedMention . textContent = mention . display_name
192
+ ? `@${ mention . display_name } `
193
+ : originalTagContent ;
194
+ }
184
195
185
196
if ( type === 'link' ) {
186
197
// If the mention exists in the list of mentions and contains a link, render
@@ -197,7 +208,7 @@ function processAndReplaceMention(
197
208
}
198
209
199
210
unprocessedMention . replaceWith ( processedMention ) ;
200
- return [ processedMention , mentionOrUsername ] ;
211
+ return [ processedMention , mentionOrInvalidContent ] ;
201
212
}
202
213
203
214
/**
@@ -212,7 +223,8 @@ function processAndReplaceMention(
212
223
export function processAndReplaceMentionElements (
213
224
element : HTMLElement ,
214
225
mentions : Mention [ ] ,
215
- ) : Map < HTMLElement , Mention | InvalidUsername > {
226
+ mentionMode : MentionMode ,
227
+ ) : Map < HTMLElement , Mention | InvalidMentionContent > {
216
228
const mentionElements = element . querySelectorAll ( '[data-hyp-mention]' ) ;
217
229
const foundMentions = new Map < HTMLElement , Mention | string > ( ) ;
218
230
@@ -223,7 +235,9 @@ export function processAndReplaceMentionElements(
223
235
? mentions . find ( m => m . userid === mentionUserId )
224
236
: undefined ;
225
237
226
- foundMentions . set ( ...processAndReplaceMention ( htmlMentionElement , mention ) ) ;
238
+ foundMentions . set (
239
+ ...processAndReplaceMention ( htmlMentionElement , mention , mentionMode ) ,
240
+ ) ;
227
241
}
228
242
229
243
return foundMentions ;
0 commit comments