@@ -16,6 +16,7 @@ import {
1616} from "./format-profile.js" ;
1717import type { MatrixSpoilerMarkers , MatrixSpoilerProtection } from "./format-profile.js" ;
1818import {
19+ findMatrixMarkdownMetadataRanges ,
1920 findMatrixSpoilerDelimiterOffsets ,
2021 hasMatrixSpoilerMetadataCollision ,
2122} from "./format-spoiler-ranges.js" ;
@@ -54,7 +55,6 @@ type MatrixMentionCandidate = {
5455 userId ?: string ;
5556} ;
5657
57- const ESCAPED_MENTION_SENTINEL = "\uE000" ;
5858const MENTION_PATTERN = / @ [ A - Z a - z 0 - 9 . _ = + \- / : [ \] ] + / g;
5959const MATRIX_MENTION_SERVER_NAME_PATTERN =
6060 / (?: [ A - Z a - z 0 - 9 ] (?: [ A - Z a - z 0 - 9 - ] * [ A - Z a - z 0 - 9 ] ) ? ) (?: \. (?: [ A - Z a - z 0 - 9 ] (?: [ A - Z a - z 0 - 9 - ] * [ A - Z a - z 0 - 9 ] ) ? ) ) * (?: : \d + ) ? / ;
@@ -127,6 +127,7 @@ md.renderer.rules.image = (tokens, idx, options, env, self) => {
127127
128128md . renderer . rules . html_block = ( tokens , idx ) => escapeHtml ( tokens [ idx ] ?. content ?? "" ) ;
129129md . renderer . rules . html_inline = ( tokens , idx ) => escapeHtml ( tokens [ idx ] ?. content ?? "" ) ;
130+ md . renderer . rules . text_special = ( tokens , idx ) => escapeHtml ( tokens [ idx ] ?. content ?? "" ) ;
130131md . renderer . rules . link_open = ( tokens , idx , _options , _env , self ) =>
131132 shouldSuppressAutoLink ( tokens , idx ) ? "" : self . renderToken ( tokens , idx , _options ) ;
132133md . renderer . rules . link_close = ( tokens , idx , _options , _env , self ) => {
@@ -137,10 +138,12 @@ md.renderer.rules.link_close = (tokens, idx, _options, _env, self) => {
137138 return self . renderToken ( tokens , idx , _options ) ;
138139} ;
139140
140- function maskEscapedMentions ( markdown : string ) : string {
141+ function maskEscapedMentions ( markdown : string ) : { markdown : string ; marker ?: string } {
141142 let masked = "" ;
142143 let idx = 0 ;
143144 let codeFenceLength = 0 ;
145+ let marker : string | undefined ;
146+ const metadataRanges = markdown . includes ( "\\@" ) ? findMatrixMarkdownMetadataRanges ( markdown ) : [ ] ;
144147
145148 while ( idx < markdown . length ) {
146149 if ( markdown [ idx ] === "`" && ! isMarkdownEscaped ( markdown , idx ) ) {
@@ -157,30 +160,38 @@ function maskEscapedMentions(markdown: string): string {
157160 idx += runLength ;
158161 continue ;
159162 }
160- if ( codeFenceLength === 0 && markdown [ idx ] === "\\" && markdown [ idx + 1 ] === "@" ) {
161- masked += ESCAPED_MENTION_SENTINEL ;
163+ if (
164+ codeFenceLength === 0 &&
165+ markdown [ idx ] === "\\" &&
166+ markdown [ idx + 1 ] === "@" &&
167+ ! metadataRanges . some ( ( range ) => idx >= range . start && idx < range . end )
168+ ) {
169+ marker ??= createMatrixPrivateMarkers (
170+ markdown ,
171+ "Matrix mention formatting exhausted its private marker pool" ,
172+ ) . open ;
173+ masked += marker ;
162174 idx += 2 ;
163175 continue ;
164176 }
165177 masked += markdown [ idx ] ?? "" ;
166178 idx += 1 ;
167179 }
168180
169- return masked ;
181+ return { markdown : masked , ... ( marker ? { marker } : { } ) } ;
170182}
171183
172- function restoreEscapedMentions ( text : string ) : string {
173- return text . replaceAll ( ESCAPED_MENTION_SENTINEL , "@" ) ;
184+ function restoreEscapedMentions ( text : string , marker ?: string , inCode = false ) : string {
185+ return marker ? text . replaceAll ( marker , inCode ? "\\@" : "@" ) : text ;
174186}
175187
176- function restoreEscapedMentionsInCode ( text : string ) : string {
177- return text . replaceAll ( ESCAPED_MENTION_SENTINEL , "\\@" ) ;
178- }
179-
180- function restoreEscapedMentionsInBlockTokens ( tokens : MarkdownToken [ ] ) : void {
188+ function restoreEscapedMentionsInBlockTokens ( tokens : MarkdownToken [ ] , marker ?: string ) : void {
189+ if ( ! marker ) {
190+ return ;
191+ }
181192 for ( const token of tokens ) {
182193 if ( ( token . type === "fence" || token . type === "code_block" ) && token . content ) {
183- token . content = restoreEscapedMentionsInCode ( token . content ) ;
194+ token . content = restoreEscapedMentions ( token . content , marker , true ) ;
184195 }
185196 }
186197}
@@ -402,8 +413,11 @@ function mutateInlineTokensWithMentions(params: {
402413 userIds : string [ ] ;
403414 seenUserIds : Set < string > ;
404415 selfUserId : string | null ;
416+ escapedMentionMarker ?: string ;
405417} ) : { children : MarkdownInlineToken [ ] ; roomMentioned : boolean } {
406418 const nextChildren : MarkdownInlineToken [ ] = [ ] ;
419+ const restoreMention = ( text : string ) =>
420+ restoreEscapedMentions ( text , params . escapedMentionMarker ) ;
407421 let roomMentioned = false ;
408422 let insideLinkDepth = 0 ;
409423 for ( const child of params . children ) {
@@ -418,11 +432,14 @@ function mutateInlineTokensWithMentions(params: {
418432 continue ;
419433 }
420434 if ( child . type !== "text" || ! child . content ) {
435+ for ( const nested of child . children ?? [ ] ) {
436+ nested . content = restoreMention ( nested . content ) ;
437+ }
421438 nextChildren . push ( child ) ;
422439 continue ;
423440 }
424441
425- const visibleContent = restoreEscapedMentions ( child . content ) ;
442+ const visibleContent = restoreMention ( child . content ) ;
426443 if ( insideLinkDepth > 0 ) {
427444 nextChildren . push ( createTextToken ( child , visibleContent ) ) ;
428445 continue ;
@@ -437,7 +454,7 @@ function mutateInlineTokensWithMentions(params: {
437454 for ( const match of matches ) {
438455 if ( match . start > cursor ) {
439456 nextChildren . push (
440- createTextToken ( child , restoreEscapedMentions ( child . content . slice ( cursor , match . start ) ) ) ,
457+ createTextToken ( child , restoreMention ( child . content . slice ( cursor , match . start ) ) ) ,
441458 ) ;
442459 }
443460 cursor = match . end ;
@@ -465,9 +482,7 @@ function mutateInlineTokensWithMentions(params: {
465482 ) ;
466483 }
467484 if ( cursor < child . content . length ) {
468- nextChildren . push (
469- createTextToken ( child , restoreEscapedMentions ( child . content . slice ( cursor ) ) ) ,
470- ) ;
485+ nextChildren . push ( createTextToken ( child , restoreMention ( child . content . slice ( cursor ) ) ) ) ;
471486 }
472487 }
473488 return { children : nextChildren , roomMentioned } ;
@@ -616,9 +631,9 @@ async function resolveMarkdownMentionState(params: {
616631 client : MatrixClient ;
617632 tableMode ?: MarkdownTableMode ;
618633} ) : Promise < { tokens : MarkdownToken [ ] ; mentions : MatrixMentions } > {
619- const markdown = maskEscapedMentions ( projectMatrixMarkdown ( params . markdown ) ) ;
634+ const { markdown, marker } = maskEscapedMentions ( projectMatrixMarkdown ( params . markdown ) ) ;
620635 const tokens = parseMatrixMarkdown ( markdown , params . tableMode ) ;
621- restoreEscapedMentionsInBlockTokens ( tokens ) ;
636+ restoreEscapedMentionsInBlockTokens ( tokens , marker ) ;
622637 const selfUserId = await resolveMatrixSelfUserId ( params . client ) ;
623638 const userIds : string [ ] = [ ] ;
624639 const seenUserIds = new Set < string > ( ) ;
@@ -633,6 +648,7 @@ async function resolveMarkdownMentionState(params: {
633648 userIds,
634649 seenUserIds,
635650 selfUserId,
651+ escapedMentionMarker : marker ,
636652 } ) ;
637653 token . children = mutated . children ;
638654 roomMentioned ||= mutated . roomMentioned ;
0 commit comments