From b0f3d574bdb226fd0882e0efda476f42f34c975b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Ska=C5=82ka?= Date: Mon, 23 Dec 2024 11:36:44 +0100 Subject: [PATCH] Add splitting to parseExpensiMark --- src/parseExpensiMark.ts | 10 +++++++++- src/rangeUtils.ts | 3 +-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/parseExpensiMark.ts b/src/parseExpensiMark.ts index cbf69845..dd787fd5 100644 --- a/src/parseExpensiMark.ts +++ b/src/parseExpensiMark.ts @@ -6,6 +6,7 @@ import {unescapeText} from 'expensify-common/dist/utils'; import {decode} from 'html-entities'; import type {WorkletFunction} from 'react-native-reanimated/lib/typescript/commonTypes'; import type {MarkdownType, MarkdownRange} from './commonTypes'; +import {splitRangesOnEmojis} from './rangeUtils'; function isWeb() { return Platform.OS === 'web'; @@ -236,6 +237,8 @@ function parseTreeToTextAndRanges(tree: StackItem): [string, MarkdownRange[]] { // getTagPriority returns a priority for a tag, higher priority means the tag should be processed first function getTagPriority(tag: string) { switch (tag) { + case 'syntax': // syntax has the lowest priority so other styles can be applied to it + return -1; case 'blockquote': return 2; case 'h1': @@ -287,8 +290,13 @@ function parseExpensiMark(markdown: string): MarkdownRange[] { )}'\nOriginal input: '${JSON.stringify(markdown)}'`, ); } - const sortedRanges = sortRanges(ranges); + + let splittedRanges = splitRangesOnEmojis(ranges, 'italic'); + splittedRanges = splitRangesOnEmojis(splittedRanges, 'strikethrough'); + + const sortedRanges = sortRanges(splittedRanges); const groupedRanges = groupRanges(sortedRanges); + return groupedRanges; } diff --git a/src/rangeUtils.ts b/src/rangeUtils.ts index dff50f74..04ab95b6 100644 --- a/src/rangeUtils.ts +++ b/src/rangeUtils.ts @@ -31,9 +31,9 @@ function splitRangesOnEmojis(ranges: MarkdownRange[], type: MarkdownType): Markd if (emojiStart >= currentStart && emojiEnd <= currentEnd) { // Intersection const newRange: MarkdownRange = { + type: currentRange.type, start: currentStart, length: emojiStart - currentStart, - type: currentRange.type, ...(currentRange?.depth && {depth: currentRange?.depth}), }; @@ -53,7 +53,6 @@ function splitRangesOnEmojis(ranges: MarkdownRange[], type: MarkdownType): Markd i++; } } - return newRanges; }