From 26ec217ed78bbbd86ca8e952bc568eb9aaba51b1 Mon Sep 17 00:00:00 2001 From: alina sireneva Date: Sun, 23 Mar 2025 23:06:46 +0300 Subject: [PATCH] feat(markdown-parser)!: support quotes --- packages/markdown-parser/src/index.ts | 32 +++++++++++++++++++ .../src/markdown-parser.test.ts | 14 ++++++++ 2 files changed, 46 insertions(+) diff --git a/packages/markdown-parser/src/index.ts b/packages/markdown-parser/src/index.ts index 5d06d3b6..b2d9e4b3 100644 --- a/packages/markdown-parser/src/index.ts +++ b/packages/markdown-parser/src/index.ts @@ -144,6 +144,8 @@ function parse( let insideCode = false let insidePre = false let insideLink = false + let currentBlockquoteStart: number | null = null + let prevBlockquote: tl.Mutable | null = null let insideLinkUrl = false let pendingLinkUrl = '' @@ -392,6 +394,34 @@ function parse( } else { pos = len } + + if (currentBlockquoteStart != null) { + const prevEnd = prevBlockquote ? prevBlockquote.offset + prevBlockquote.length : Number.NaN + if (currentBlockquoteStart - 1 === prevEnd) { + // extend the previous entity + prevBlockquote!.length += result.length - currentBlockquoteStart + } else { + if (prevBlockquote) entities.push(prevBlockquote) + prevBlockquote = { + _: 'messageEntityBlockquote', + offset: currentBlockquoteStart, + length: result.length - currentBlockquoteStart - 1, + } + } + currentBlockquoteStart = null + } + continue + } + + if (c === '>' && (result.length === 0 || result[result.length - 1] === '\n')) { + currentBlockquoteStart = result.length + pos += 1 + continue + } + + if (c === ' ' && currentBlockquoteStart === result.length) { + // ignore spaces after `>` + pos += 1 continue } @@ -443,6 +473,8 @@ function parse( feed(strings[strings.length - 1]) + if (prevBlockquote) entities.push(prevBlockquote) + function adjustOffsets(from: number, by: number): void { for (const ent of entities) { if (ent.offset >= from) ent.offset += by diff --git a/packages/markdown-parser/src/markdown-parser.test.ts b/packages/markdown-parser/src/markdown-parser.test.ts index 8ba8392e..032f2e33 100644 --- a/packages/markdown-parser/src/markdown-parser.test.ts +++ b/packages/markdown-parser/src/markdown-parser.test.ts @@ -620,6 +620,20 @@ ${'meowww'} ) }) + it('should handle quotes', () => { + test( + md_` +some text :3 +> btw i like ${'quotes'} +> and also **formatting** +> :3 +some more text +`, + [createEntity('messageEntityBold', 40, 10), createEntity('messageEntityBlockquote', 13, 40)], + 'some text :3\nbtw i like quotes\nand also formatting\n:3\nsome more text', + ) + }) + it('should interpolate inside links', () => { test( md_`[${'link'}](${'https'}://example.com/\\)${'foo'}/bar/${'baz'}?foo=bar&baz=${'egg'})`,