From 028e331cb683485e6f18bcc505965ce6899c5468 Mon Sep 17 00:00:00 2001 From: teidesu <86301490+teidesu@users.noreply.github.com> Date: Wed, 23 Jun 2021 17:08:23 +0300 Subject: [PATCH] fix(markdown): properly handle square brackets that are not links --- packages/markdown-parser/README.md | 2 +- packages/markdown-parser/src/index.ts | 3 ++- packages/markdown-parser/tests/markdown-parser.spec.ts | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/markdown-parser/README.md b/packages/markdown-parser/README.md index c6a39849..6b853bf1 100644 --- a/packages/markdown-parser/README.md +++ b/packages/markdown-parser/README.md @@ -95,7 +95,7 @@ where `1234567` is the ID of the user you want to mention | `[Google](https://google.com)` | [Google](https://google.com) | `Google` | `[__Google__](https://google.com)` | [_Google_](https://google.com) | `Google` | `[empty link]()` | empty link | `empty link` -| `[empty link]` | empty link | `empty link` +| `[empty link]` | [empty link] | `[empty link]` | `[User](tg://user?id=1234567)` | N/A | N/A ### Nested and overlapping entities diff --git a/packages/markdown-parser/src/index.ts b/packages/markdown-parser/src/index.ts index edcef7ea..a63855ad 100644 --- a/packages/markdown-parser/src/index.ts +++ b/packages/markdown-parser/src/index.ts @@ -102,7 +102,8 @@ export class MarkdownMessageEntityParser implements IMessageEntityParser { if (text[pos + 1] !== '(') { // [link text] - // ignore this + // ignore this, and add opening [ + result = `${result.substr(0, ent.offset)}[${result.substr(ent.offset)}]` pos += 1 continue } diff --git a/packages/markdown-parser/tests/markdown-parser.spec.ts b/packages/markdown-parser/tests/markdown-parser.spec.ts index 01aca9c2..6d91d2d2 100644 --- a/packages/markdown-parser/tests/markdown-parser.spec.ts +++ b/packages/markdown-parser/tests/markdown-parser.spec.ts @@ -587,7 +587,7 @@ describe('MarkdownMessageEntityParser', () => { }) it('should ignore empty urls', () => { - test('[link]() [link]', [], 'link link') + test('[link]() [link]', [], 'link [link]') }) it('should ignore unclosed tags', () => {