@@ -294,24 +294,38 @@ function createTextFormatTransformersIndex(
294
294
) : TextFormatTransformersIndex {
295
295
const transformersByTag : Record < string , TextFormatTransformer > = { } ;
296
296
const fullMatchRegExpByTag : Record < string , RegExp > = { } ;
297
- const openTagsRegExp = [ ] ;
297
+ const openTagsRegExp : string [ ] = [ ] ;
298
298
const escapeRegExp = `(?<![\\\\])` ;
299
299
300
+ // Sort `textTransformers` so that longer tags come before shorter ones
301
+ // (prevents `*` from "stealing" text that should match `**`).
302
+ textTransformers . sort ( ( a , b ) => b . tag . length - a . tag . length ) ;
303
+
300
304
for ( const transformer of textTransformers ) {
301
305
const { tag} = transformer ;
302
306
transformersByTag [ tag ] = transformer ;
307
+
303
308
const tagRegExp = tag . replace ( / ( \* | \^ | \+ ) / g, '\\$1' ) ;
304
309
openTagsRegExp . push ( tagRegExp ) ;
305
310
306
- fullMatchRegExpByTag [ tag ] = new RegExp (
307
- `(?<![\\\\${ tagRegExp } ])(${ tagRegExp } )((\\\\${ tagRegExp } )?.*?[^${ tagRegExp } \\s](\\\\${ tagRegExp } )?)((?<!\\\\)|(?<=\\\\\\\\))(${ tagRegExp } )(?![\\\\${ tagRegExp } ])` ,
308
- ) ;
311
+ // Single-char tag (e.g. "*"),
312
+ if ( tag . length === 1 ) {
313
+ fullMatchRegExpByTag [ tag ] = new RegExp (
314
+ `(?<![\\\\${ tagRegExp } ])(${ tagRegExp } )((\\\\${ tagRegExp } )?.*?[^${ tagRegExp } \\s](\\\\${ tagRegExp } )?)((?<!\\\\)|(?<=\\\\\\\\))(${ tagRegExp } )(?![\\\\${ tagRegExp } ])` ,
315
+ ) ;
316
+ } else {
317
+ // Multi‐char tags (e.g. "**")
318
+ fullMatchRegExpByTag [ tag ] = new RegExp (
319
+ `(?<!\\\\)(${ tagRegExp } )((\\\\${ tagRegExp } )?.*?[^\\s](\\\\${ tagRegExp } )?)((?<!\\\\)|(?<=\\\\\\\\))(${ tagRegExp } )(?!\\\\)` ,
320
+ ) ;
321
+ }
309
322
}
310
323
311
324
return {
312
325
// Reg exp to find open tag + content + close tag
313
326
fullMatchRegExpByTag,
314
- // Reg exp to find opening tags
327
+
328
+ // Regexp to locate *any* potential opening tag (longest first).
315
329
openTagsRegExp : new RegExp (
316
330
`${ escapeRegExp } (${ openTagsRegExp . join ( '|' ) } )` ,
317
331
'g' ,
0 commit comments