From 414150ccb39645148296d642412b78c5de8a9b5d Mon Sep 17 00:00:00 2001 From: daledah Date: Mon, 6 Jan 2025 17:43:58 +0700 Subject: [PATCH 1/2] fix: return empty array instead of throwing error if text !== markdown --- src/parseExpensiMark.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/parseExpensiMark.ts b/src/parseExpensiMark.ts index cbf69845..7657efd9 100644 --- a/src/parseExpensiMark.ts +++ b/src/parseExpensiMark.ts @@ -281,11 +281,12 @@ function parseExpensiMark(markdown: string): MarkdownRange[] { const tree = parseTokensToTree(tokens); const [text, ranges] = parseTreeToTextAndRanges(tree); if (text !== markdown) { - throw new Error( + console.error( `[react-native-live-markdown] Parsing error: the processed text does not match the original Markdown input. This may be caused by incorrect parsing functions or invalid input Markdown.\nProcessed input: '${JSON.stringify( text, )}'\nOriginal input: '${JSON.stringify(markdown)}'`, ); + return []; } const sortedRanges = sortRanges(ranges); const groupedRanges = groupRanges(sortedRanges); From 946b2e92b116689cea344391bd7dfb5f847b5948 Mon Sep 17 00:00:00 2001 From: daledah Date: Tue, 7 Jan 2025 00:37:52 +0700 Subject: [PATCH 2/2] feat: add test for parsing error case --- src/__tests__/parseExpensiMark.test.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/__tests__/parseExpensiMark.test.ts b/src/__tests__/parseExpensiMark.test.ts index b49f46fa..1d6a0436 100644 --- a/src/__tests__/parseExpensiMark.test.ts +++ b/src/__tests__/parseExpensiMark.test.ts @@ -31,6 +31,10 @@ test('no formatting', () => { expect('Hello, world!').toBeParsedAs([]); }); +describe('parsing error', () => { + expect(`> [exa\nmple.com](https://example.com)`).toBeParsedAs([]); +}); + test('bold', () => { expect('Hello, *world*!').toBeParsedAs([ {type: 'syntax', start: 7, length: 1},