Browse Source

MarkupParser: Fix bug with double markup chars (#630)

Danilo Bargen 6 năm trước cách đây
mục cha
commit
38484c1a0c
2 tập tin đã thay đổi với 16 bổ sung1 xóa
  1. 8 1
      src/markup_parser.ts
  2. 8 0
      tests/ts/markup_parser.ts

+ 8 - 1
src/markup_parser.ts

@@ -187,7 +187,14 @@ export function parse(tokens: Token[]): string {
                         if (stackTop.kind === TokenType.Text) {
                             textParts.push(stackTop.value);
                         } else if (stackTop.kind === token.kind) {
-                            pushMarkup(textParts, cssClasses[token.kind]);
+                            if (textParts.length > 0) {
+                                pushMarkup(textParts, cssClasses[token.kind]);
+                            } else {
+                                // If this happens, then two markup chars were following each other (e.g. **hello).
+                                // In that case, just keep them as regular text characters, without applying any markup.
+                                const markupChar = markupChars[token.kind];
+                                stack.push({ kind: TokenType.Text, value: markupChar + markupChar });
+                            }
                             hasToken(token.kind, false);
                             break;
                         } else if (isMarkupToken(stackTop.kind)) {

+ 8 - 0
tests/ts/markup_parser.ts

@@ -240,6 +240,12 @@ describe('Markup Parser', () => {
                  'https://example.com/_output_/'],
                 ['https://example.com/*output*/',
                  'https://example.com/*output*/'],
+                ['https://example.com?_twitter_impression=true',
+                 'https://example.com?_twitter_impression=true'],
+                ['https://example.com?__twitter_impression=true',
+                 'https://example.com?__twitter_impression=true'],
+                ['https://example.com?___twitter_impression=true',
+                 'https://example.com?___twitter_impression=true'],
             ]);
         });
 
@@ -247,6 +253,8 @@ describe('Markup Parser', () => {
             testPatterns([
                 ['*invalid markup (do not parse)_', '*invalid markup (do not parse)_'],
                 ['random *asterisk', 'random *asterisk'],
+                ['***three asterisks', '***three asterisks'],
+                ['***three asterisks*', '**<span class="text-bold">three asterisks</span>'],
             ]);
         });