@expensify/react-native-live-markdown 0.1.202 → 0.1.203

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,18 @@
1
+ #import <Foundation/Foundation.h>
2
+ #import <RNLiveMarkdown/MarkdownRange.h>
3
+ #import <RNLiveMarkdown/RCTMarkdownStyle.h>
4
+
5
+ NS_ASSUME_NONNULL_BEGIN
6
+
7
+ const NSAttributedStringKey RCTLiveMarkdownBlockquoteDepthAttributeName = @"RCTLiveMarkdownBlockquoteDepth";
8
+
9
+ @interface MarkdownFormatter : NSObject
10
+
11
+ - (nonnull NSAttributedString *)format:(nonnull NSString *)text
12
+ withAttributes:(nullable NSDictionary<NSAttributedStringKey, id>*)attributes
13
+ withMarkdownRanges:(nonnull NSArray<MarkdownRange *> *)markdownRanges
14
+ withMarkdownStyle:(nonnull RCTMarkdownStyle *)markdownStyle;
15
+
16
+ NS_ASSUME_NONNULL_END
17
+
18
+ @end
@@ -0,0 +1,158 @@
1
+ #import "MarkdownFormatter.h"
2
+ #import <React/RCTFont.h>
3
+
4
+ @implementation MarkdownFormatter
5
+
6
+ - (nonnull NSAttributedString *)format:(nonnull NSString *)text
7
+ withAttributes:(nullable NSDictionary<NSAttributedStringKey, id> *)attributes
8
+ withMarkdownRanges:(nonnull NSArray<MarkdownRange *> *)markdownRanges
9
+ withMarkdownStyle:(nonnull RCTMarkdownStyle *)markdownStyle
10
+ {
11
+ NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text attributes:attributes];
12
+
13
+ [attributedString beginEditing];
14
+
15
+ // If the attributed string ends with underlined text, blurring the single-line input imprints the underline style across the whole string.
16
+ // It looks like a bug in iOS, as there is no underline style to be found in the attributed string, especially after formatting.
17
+ // This is a workaround that applies the NSUnderlineStyleNone to the string before iterating over ranges which resolves this problem.
18
+ [attributedString addAttribute:NSUnderlineStyleAttributeName
19
+ value:[NSNumber numberWithInteger:NSUnderlineStyleNone]
20
+ range:NSMakeRange(0, attributedString.length)];
21
+
22
+ for (MarkdownRange *markdownRange in markdownRanges) {
23
+ [self applyRangeToAttributedString:attributedString
24
+ type:std::string([markdownRange.type UTF8String])
25
+ range:markdownRange.range
26
+ depth:markdownRange.depth
27
+ markdownStyle:markdownStyle];
28
+ }
29
+
30
+ RCTApplyBaselineOffset(attributedString);
31
+
32
+ [attributedString endEditing];
33
+
34
+ return attributedString;
35
+ }
36
+
37
+ - (void)applyRangeToAttributedString:(NSMutableAttributedString *)attributedString
38
+ type:(const std::string)type
39
+ range:(const NSRange)range
40
+ depth:(const int)depth
41
+ markdownStyle:(nonnull RCTMarkdownStyle *)markdownStyle {
42
+ if (type == "bold" || type == "italic" || type == "code" || type == "pre" || type == "h1" || type == "emoji") {
43
+ UIFont *font = [attributedString attribute:NSFontAttributeName atIndex:range.location effectiveRange:NULL];
44
+ if (type == "bold") {
45
+ font = [RCTFont updateFont:font withWeight:@"bold"];
46
+ } else if (type == "italic") {
47
+ font = [RCTFont updateFont:font withStyle:@"italic"];
48
+ } else if (type == "code") {
49
+ font = [RCTFont updateFont:font withFamily:markdownStyle.codeFontFamily
50
+ size:[NSNumber numberWithFloat:markdownStyle.codeFontSize]
51
+ weight:nil
52
+ style:nil
53
+ variant:nil
54
+ scaleMultiplier:0];
55
+ } else if (type == "pre") {
56
+ font = [RCTFont updateFont:font withFamily:markdownStyle.preFontFamily
57
+ size:[NSNumber numberWithFloat:markdownStyle.preFontSize]
58
+ weight:nil
59
+ style:nil
60
+ variant:nil
61
+ scaleMultiplier:0];
62
+ } else if (type == "h1") {
63
+ font = [RCTFont updateFont:font withFamily:nil
64
+ size:[NSNumber numberWithFloat:markdownStyle.h1FontSize]
65
+ weight:@"bold"
66
+ style:nil
67
+ variant:nil
68
+ scaleMultiplier:0];
69
+ } else if (type == "emoji") {
70
+ font = [RCTFont updateFont:font withFamily:nil
71
+ size:[NSNumber numberWithFloat:markdownStyle.emojiFontSize]
72
+ weight:nil
73
+ style:nil
74
+ variant:nil
75
+ scaleMultiplier:0];
76
+ }
77
+ [attributedString addAttribute:NSFontAttributeName value:font range:range];
78
+ }
79
+
80
+ if (type == "syntax") {
81
+ [attributedString addAttribute:NSForegroundColorAttributeName value:markdownStyle.syntaxColor range:range];
82
+ } else if (type == "strikethrough") {
83
+ [attributedString addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:range];
84
+ } else if (type == "code") {
85
+ [attributedString addAttribute:NSForegroundColorAttributeName value:markdownStyle.codeColor range:range];
86
+ [attributedString addAttribute:NSBackgroundColorAttributeName value:markdownStyle.codeBackgroundColor range:range];
87
+ } else if (type == "mention-here") {
88
+ [attributedString addAttribute:NSForegroundColorAttributeName value:markdownStyle.mentionHereColor range:range];
89
+ [attributedString addAttribute:NSBackgroundColorAttributeName value:markdownStyle.mentionHereBackgroundColor range:range];
90
+ } else if (type == "mention-user") {
91
+ // TODO: change mention color when it mentions current user
92
+ [attributedString addAttribute:NSForegroundColorAttributeName value:markdownStyle.mentionUserColor range:range];
93
+ [attributedString addAttribute:NSBackgroundColorAttributeName value:markdownStyle.mentionUserBackgroundColor range:range];
94
+ } else if (type == "mention-report") {
95
+ [attributedString addAttribute:NSForegroundColorAttributeName value:markdownStyle.mentionReportColor range:range];
96
+ [attributedString addAttribute:NSBackgroundColorAttributeName value:markdownStyle.mentionReportBackgroundColor range:range];
97
+ } else if (type == "link") {
98
+ [attributedString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:range];
99
+ [attributedString addAttribute:NSForegroundColorAttributeName value:markdownStyle.linkColor range:range];
100
+ } else if (type == "blockquote") {
101
+ CGFloat indent = (markdownStyle.blockquoteMarginLeft + markdownStyle.blockquoteBorderWidth + markdownStyle.blockquotePaddingLeft) * depth;
102
+ NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
103
+ paragraphStyle.firstLineHeadIndent = indent;
104
+ paragraphStyle.headIndent = indent;
105
+ [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:range];
106
+ [attributedString addAttribute:RCTLiveMarkdownBlockquoteDepthAttributeName value:@(depth) range:range];
107
+ } else if (type == "pre") {
108
+ [attributedString addAttribute:NSForegroundColorAttributeName value:markdownStyle.preColor range:range];
109
+ NSRange rangeForBackground = [[attributedString string] characterAtIndex:range.location] == '\n' ? NSMakeRange(range.location + 1, range.length - 1) : range;
110
+ [attributedString addAttribute:NSBackgroundColorAttributeName value:markdownStyle.preBackgroundColor range:rangeForBackground];
111
+ // TODO: pass background color and ranges to layout manager
112
+ }
113
+ }
114
+
115
+ static void RCTApplyBaselineOffset(NSMutableAttributedString *attributedText)
116
+ {
117
+ __block CGFloat maximumLineHeight = 0;
118
+
119
+ [attributedText enumerateAttribute:NSParagraphStyleAttributeName
120
+ inRange:NSMakeRange(0, attributedText.length)
121
+ options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired
122
+ usingBlock:^(NSParagraphStyle *paragraphStyle, __unused NSRange range, __unused BOOL *stop) {
123
+ if (!paragraphStyle) {
124
+ return;
125
+ }
126
+
127
+ maximumLineHeight = MAX(paragraphStyle.maximumLineHeight, maximumLineHeight);
128
+ }];
129
+
130
+ if (maximumLineHeight == 0) {
131
+ // `lineHeight` was not specified, nothing to do.
132
+ return;
133
+ }
134
+
135
+ __block CGFloat maximumFontLineHeight = 0;
136
+
137
+ [attributedText enumerateAttribute:NSFontAttributeName
138
+ inRange:NSMakeRange(0, attributedText.length)
139
+ options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired
140
+ usingBlock:^(UIFont *font, NSRange range, __unused BOOL *stop) {
141
+ if (!font) {
142
+ return;
143
+ }
144
+
145
+ maximumFontLineHeight = MAX(font.lineHeight, maximumFontLineHeight);
146
+ }];
147
+
148
+ if (maximumLineHeight < maximumFontLineHeight) {
149
+ return;
150
+ }
151
+
152
+ CGFloat baseLineOffset = (maximumLineHeight - maximumFontLineHeight) / 2.0;
153
+ [attributedText addAttribute:NSBaselineOffsetAttributeName
154
+ value:@(baseLineOffset)
155
+ range:NSMakeRange(0, attributedText.length)];
156
+ }
157
+
158
+ @end
@@ -1,5 +1,6 @@
1
1
  #import <UIKit/UIKit.h>
2
2
  #import <RNLiveMarkdown/RCTMarkdownUtils.h>
3
+ #import <RNLiveMarkdown/MarkdownFormatter.h>
3
4
 
4
5
  NS_ASSUME_NONNULL_BEGIN
5
6
 
@@ -3,8 +3,6 @@
3
3
 
4
4
  NS_ASSUME_NONNULL_BEGIN
5
5
 
6
- const NSAttributedStringKey RCTLiveMarkdownBlockquoteDepthAttributeName = @"RCTLiveMarkdownBlockquoteDepth";
7
-
8
6
  @interface RCTMarkdownUtils : NSObject
9
7
 
10
8
  @property (nonatomic) RCTMarkdownStyle *markdownStyle;
@@ -1,13 +1,10 @@
1
1
  #import <RNLiveMarkdown/RCTMarkdownUtils.h>
2
- #import <RNLiveMarkdown/MarkdownGlobal.h>
3
2
  #import <RNLiveMarkdown/MarkdownParser.h>
4
- #import "react_native_assert.h"
5
- #import <React/RCTAssert.h>
6
- #import <React/RCTFont.h>
7
- #include <jsi/jsi.h>
3
+ #import <RNLiveMarkdown/MarkdownFormatter.h>
8
4
 
9
5
  @implementation RCTMarkdownUtils {
10
6
  MarkdownParser *_markdownParser;
7
+ MarkdownFormatter *_markdownFormatter;
11
8
  NSString *_prevInputString;
12
9
  NSAttributedString *_prevAttributedString;
13
10
  NSDictionary<NSAttributedStringKey, id> *_prevTextAttributes;
@@ -19,6 +16,7 @@
19
16
  {
20
17
  if (self = [super init]) {
21
18
  _markdownParser = [MarkdownParser new];
19
+ _markdownFormatter = [MarkdownFormatter new];
22
20
  }
23
21
 
24
22
  return self;
@@ -26,162 +24,30 @@
26
24
 
27
25
  - (NSAttributedString *)parseMarkdown:(nullable NSAttributedString *)input withAttributes:(nullable NSDictionary<NSAttributedStringKey,id> *)attributes
28
26
  {
29
- @synchronized (self) {
30
- if (input == nil) {
31
- return nil;
32
- }
33
-
34
- NSString *inputString = [input string];
35
- if ([inputString isEqualToString:_prevInputString] && [attributes isEqualToDictionary:_prevTextAttributes] && [_markdownStyle isEqual:_prevMarkdownStyle] && [_parserId isEqualToNumber:_prevParserId]) {
36
- return _prevAttributedString;
37
- }
38
-
39
- NSArray<MarkdownRange *> *markdownRanges = [_markdownParser parse:inputString withParserId:_parserId];
40
-
41
- NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:inputString attributes:attributes];
42
- [attributedString beginEditing];
43
-
44
- // If the attributed string ends with underlined text, blurring the single-line input imprints the underline style across the whole string.
45
- // It looks like a bug in iOS, as there is no underline style to be found in the attributed string, especially after formatting.
46
- // This is a workaround that applies the NSUnderlineStyleNone to the string before iterating over ranges which resolves this problem.
47
- [attributedString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleNone] range:NSMakeRange(0, attributedString.length)];
48
-
49
- for (MarkdownRange *markdownRange in markdownRanges) {
50
- [self applyRangeToAttributedString:attributedString
51
- type:std::string([markdownRange.type UTF8String])
52
- range:markdownRange.range
53
- depth:markdownRange.depth];
54
- }
55
-
56
- RCTApplyBaselineOffset(attributedString);
57
-
58
- [attributedString endEditing];
59
-
60
- _prevInputString = inputString;
61
- _prevAttributedString = attributedString;
62
- _prevTextAttributes = attributes;
63
- _prevMarkdownStyle = _markdownStyle;
64
- _prevParserId = _parserId;
65
-
66
- return attributedString;
27
+ @synchronized (self) {
28
+ if (input == nil) {
29
+ return nil;
67
30
  }
68
- }
69
31
 
70
- - (void)applyRangeToAttributedString:(NSMutableAttributedString *)attributedString type:(const std::string)type range:(NSRange)range depth:(const int)depth {
71
- if (type == "bold" || type == "italic" || type == "code" || type == "pre" || type == "h1" || type == "emoji") {
72
- UIFont *font = [attributedString attribute:NSFontAttributeName atIndex:range.location effectiveRange:NULL];
73
- if (type == "bold") {
74
- font = [RCTFont updateFont:font withWeight:@"bold"];
75
- } else if (type == "italic") {
76
- font = [RCTFont updateFont:font withStyle:@"italic"];
77
- } else if (type == "code") {
78
- font = [RCTFont updateFont:font withFamily:_markdownStyle.codeFontFamily
79
- size:[NSNumber numberWithFloat:_markdownStyle.codeFontSize]
80
- weight:nil
81
- style:nil
82
- variant:nil
83
- scaleMultiplier:0];
84
- } else if (type == "pre") {
85
- font = [RCTFont updateFont:font withFamily:_markdownStyle.preFontFamily
86
- size:[NSNumber numberWithFloat:_markdownStyle.preFontSize]
87
- weight:nil
88
- style:nil
89
- variant:nil
90
- scaleMultiplier:0];
91
- } else if (type == "h1") {
92
- font = [RCTFont updateFont:font withFamily:nil
93
- size:[NSNumber numberWithFloat:_markdownStyle.h1FontSize]
94
- weight:@"bold"
95
- style:nil
96
- variant:nil
97
- scaleMultiplier:0];
98
- } else if (type == "emoji") {
99
- font = [RCTFont updateFont:font withFamily:nil
100
- size:[NSNumber numberWithFloat:_markdownStyle.emojiFontSize]
101
- weight:nil
102
- style:nil
103
- variant:nil
104
- scaleMultiplier:0];
105
- }
106
- [attributedString addAttribute:NSFontAttributeName value:font range:range];
32
+ NSString *inputString = [input string];
33
+ if ([inputString isEqualToString:_prevInputString] && [attributes isEqualToDictionary:_prevTextAttributes] && [_markdownStyle isEqual:_prevMarkdownStyle] && [_parserId isEqualToNumber:_prevParserId]) {
34
+ return _prevAttributedString;
107
35
  }
108
36
 
109
- if (type == "syntax") {
110
- [attributedString addAttribute:NSForegroundColorAttributeName value:_markdownStyle.syntaxColor range:range];
111
- } else if (type == "strikethrough") {
112
- [attributedString addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:range];
113
- } else if (type == "code") {
114
- [attributedString addAttribute:NSForegroundColorAttributeName value:_markdownStyle.codeColor range:range];
115
- [attributedString addAttribute:NSBackgroundColorAttributeName value:_markdownStyle.codeBackgroundColor range:range];
116
- } else if (type == "mention-here") {
117
- [attributedString addAttribute:NSForegroundColorAttributeName value:_markdownStyle.mentionHereColor range:range];
118
- [attributedString addAttribute:NSBackgroundColorAttributeName value:_markdownStyle.mentionHereBackgroundColor range:range];
119
- } else if (type == "mention-user") {
120
- // TODO: change mention color when it mentions current user
121
- [attributedString addAttribute:NSForegroundColorAttributeName value:_markdownStyle.mentionUserColor range:range];
122
- [attributedString addAttribute:NSBackgroundColorAttributeName value:_markdownStyle.mentionUserBackgroundColor range:range];
123
- } else if (type == "mention-report") {
124
- [attributedString addAttribute:NSForegroundColorAttributeName value:_markdownStyle.mentionReportColor range:range];
125
- [attributedString addAttribute:NSBackgroundColorAttributeName value:_markdownStyle.mentionReportBackgroundColor range:range];
126
- } else if (type == "link") {
127
- [attributedString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:range];
128
- [attributedString addAttribute:NSForegroundColorAttributeName value:_markdownStyle.linkColor range:range];
129
- } else if (type == "blockquote") {
130
- CGFloat indent = (_markdownStyle.blockquoteMarginLeft + _markdownStyle.blockquoteBorderWidth + _markdownStyle.blockquotePaddingLeft) * depth;
131
- NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
132
- paragraphStyle.firstLineHeadIndent = indent;
133
- paragraphStyle.headIndent = indent;
134
- [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:range];
135
- [attributedString addAttribute:RCTLiveMarkdownBlockquoteDepthAttributeName value:@(depth) range:range];
136
- } else if (type == "pre") {
137
- [attributedString addAttribute:NSForegroundColorAttributeName value:_markdownStyle.preColor range:range];
138
- NSRange rangeForBackground = [[attributedString string] characterAtIndex:range.location] == '\n' ? NSMakeRange(range.location + 1, range.length - 1) : range;
139
- [attributedString addAttribute:NSBackgroundColorAttributeName value:_markdownStyle.preBackgroundColor range:rangeForBackground];
140
- // TODO: pass background color and ranges to layout manager
141
- }
142
- }
37
+ NSArray<MarkdownRange *> *markdownRanges = [_markdownParser parse:inputString withParserId:_parserId];
143
38
 
144
- static void RCTApplyBaselineOffset(NSMutableAttributedString *attributedText)
145
- {
146
- __block CGFloat maximumLineHeight = 0;
147
-
148
- [attributedText enumerateAttribute:NSParagraphStyleAttributeName
149
- inRange:NSMakeRange(0, attributedText.length)
150
- options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired
151
- usingBlock:^(NSParagraphStyle *paragraphStyle, __unused NSRange range, __unused BOOL *stop) {
152
- if (!paragraphStyle) {
153
- return;
154
- }
39
+ NSAttributedString *attributedString = [_markdownFormatter format:inputString
40
+ withAttributes:attributes
41
+ withMarkdownRanges:markdownRanges
42
+ withMarkdownStyle:_markdownStyle];
43
+ _prevInputString = inputString;
44
+ _prevAttributedString = attributedString;
45
+ _prevTextAttributes = attributes;
46
+ _prevMarkdownStyle = _markdownStyle;
47
+ _prevParserId = _parserId;
155
48
 
156
- maximumLineHeight = MAX(paragraphStyle.maximumLineHeight, maximumLineHeight);
157
- }];
158
-
159
- if (maximumLineHeight == 0) {
160
- // `lineHeight` was not specified, nothing to do.
161
- return;
49
+ return attributedString;
162
50
  }
163
-
164
- __block CGFloat maximumFontLineHeight = 0;
165
-
166
- [attributedText enumerateAttribute:NSFontAttributeName
167
- inRange:NSMakeRange(0, attributedText.length)
168
- options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired
169
- usingBlock:^(UIFont *font, NSRange range, __unused BOOL *stop) {
170
- if (!font) {
171
- return;
172
- }
173
-
174
- maximumFontLineHeight = MAX(font.lineHeight, maximumFontLineHeight);
175
- }];
176
-
177
- if (maximumLineHeight < maximumFontLineHeight) {
178
- return;
179
- }
180
-
181
- CGFloat baseLineOffset = (maximumLineHeight - maximumFontLineHeight) / 2.0;
182
- [attributedText addAttribute:NSBaselineOffsetAttributeName
183
- value:@(baseLineOffset)
184
- range:NSMakeRange(0, attributedText.length)];
185
51
  }
186
52
 
187
53
  @end
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@expensify/react-native-live-markdown",
3
- "version": "0.1.202",
3
+ "version": "0.1.203",
4
4
  "description": "Drop-in replacement for React Native's TextInput component with Markdown formatting.",
5
5
  "main": "lib/commonjs/index",
6
6
  "module": "lib/module/index",