Skip to content

Commit

Permalink
Obtain blockquotes ranges and levels from markdownRanges on iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
tomekzaw committed Dec 5, 2024
1 parent 01be83f commit 69acd78
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
17 changes: 11 additions & 6 deletions apple/RCTMarkdownUtils.mm
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ - (NSAttributedString *)parseMarkdown:(nullable NSAttributedString *)input withA

NSArray<MarkdownRange *> *markdownRanges = [_markdownParser parse:inputString withParserId:_parserId];

// TODO: use custom attribute to store blockquote depth instead
_blockquoteRangesAndLevels = [NSMutableArray new];
for (MarkdownRange *markdownRange in markdownRanges) {
if ([markdownRange.type isEqualToString:@"blockquote"]) {
[_blockquoteRangesAndLevels addObject:@{
@"range": [NSValue valueWithRange:markdownRange.range],
@"depth": @(markdownRange.depth)
}];
}
}

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:inputString attributes:attributes];
[attributedString beginEditing];

Expand All @@ -46,8 +57,6 @@ - (NSAttributedString *)parseMarkdown:(nullable NSAttributedString *)input withA
// This is a workaround that applies the NSUnderlineStyleNone to the string before iterating over ranges which resolves this problem.
[attributedString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleNone] range:NSMakeRange(0, attributedString.length)];

_blockquoteRangesAndLevels = [NSMutableArray new];

for (MarkdownRange *markdownRange in markdownRanges) {
[self applyRangeToAttributedString:attributedString
type:std::string([markdownRange.type UTF8String])
Expand Down Expand Up @@ -134,10 +143,6 @@ - (void)applyRangeToAttributedString:(NSMutableAttributedString *)attributedStri
paragraphStyle.firstLineHeadIndent = indent;
paragraphStyle.headIndent = indent;
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:range];
[_blockquoteRangesAndLevels addObject:@{
@"range": [NSValue valueWithRange:range],
@"depth": @(depth)
}];
} else if (type == "pre") {
[attributedString addAttribute:NSForegroundColorAttributeName value:_markdownStyle.preColor range:range];
NSRange rangeForBackground = [[attributedString string] characterAtIndex:range.location] == '\n' ? NSMakeRange(range.location + 1, range.length - 1) : range;
Expand Down
8 changes: 4 additions & 4 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1497,7 +1497,7 @@ PODS:
- React-logger (= 0.75.3)
- React-perflogger (= 0.75.3)
- React-utils (= 0.75.3)
- RNLiveMarkdown (0.1.190):
- RNLiveMarkdown (0.1.195):
- DoubleConversion
- glog
- hermes-engine
Expand All @@ -1517,10 +1517,10 @@ PODS:
- ReactCodegen
- ReactCommon/turbomodule/bridging
- ReactCommon/turbomodule/core
- RNLiveMarkdown/newarch (= 0.1.190)
- RNLiveMarkdown/newarch (= 0.1.195)
- RNReanimated/worklets
- Yoga
- RNLiveMarkdown/newarch (0.1.190):
- RNLiveMarkdown/newarch (0.1.195):
- DoubleConversion
- glog
- hermes-engine
Expand Down Expand Up @@ -1897,7 +1897,7 @@ SPEC CHECKSUMS:
React-utils: f2afa6acd905ca2ce7bb8ffb4a22f7f8a12534e8
ReactCodegen: e35c23cdd36922f6d2990c6c1f1b022ade7ad74d
ReactCommon: 289214026502e6a93484f4a46bcc0efa4f3f2864
RNLiveMarkdown: a210cbb45b6cb9db0b28ef09aafdc9c77424dd38
RNLiveMarkdown: 18b4dec85110bc61b02f53501cd9e7aa08066b7f
RNReanimated: ab6c33a61e90c4cbe5dbcbe65bd6c7cb3be167e6
SocketRocket: abac6f5de4d4d62d24e11868d7a2f427e0ef940d
Yoga: 1354c027ab07c7736f99a3bef16172d6f1b12b47
Expand Down

0 comments on commit 69acd78

Please sign in to comment.