@expensify/react-native-live-markdown 0.1.95 → 0.1.96
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.
- package/RNLiveMarkdown.podspec +2 -0
- package/ios/RCTMarkdownUtils.mm +44 -33
- package/package.json +1 -1
package/RNLiveMarkdown.podspec
CHANGED
package/ios/RCTMarkdownUtils.mm
CHANGED
|
@@ -2,7 +2,11 @@
|
|
|
2
2
|
#import "react_native_assert.h"
|
|
3
3
|
#import <React/RCTAssert.h>
|
|
4
4
|
#import <React/RCTFont.h>
|
|
5
|
-
|
|
5
|
+
|
|
6
|
+
#include <jsi/jsi.h>
|
|
7
|
+
#include <hermes/hermes.h>
|
|
8
|
+
|
|
9
|
+
using namespace facebook;
|
|
6
10
|
|
|
7
11
|
@implementation RCTMarkdownUtils {
|
|
8
12
|
NSString *_prevInputString;
|
|
@@ -23,20 +27,26 @@
|
|
|
23
27
|
return _prevAttributedString;
|
|
24
28
|
}
|
|
25
29
|
|
|
26
|
-
static
|
|
27
|
-
|
|
28
|
-
if (ctx == nil) {
|
|
30
|
+
static std::shared_ptr<jsi::Runtime> runtime;
|
|
31
|
+
if (runtime == nullptr) {
|
|
29
32
|
NSString *path = [[NSBundle mainBundle] pathForResource:@"react-native-live-markdown-parser" ofType:@"js"];
|
|
30
33
|
assert(path != nil && "[react-native-live-markdown] Markdown parser bundle not found");
|
|
31
34
|
NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];
|
|
32
35
|
assert(content != nil && "[react-native-live-markdown] Markdown parser bundle is empty");
|
|
33
|
-
|
|
34
|
-
[
|
|
35
|
-
|
|
36
|
+
runtime = facebook::hermes::makeHermesRuntime();
|
|
37
|
+
auto codeBuffer = std::make_shared<const jsi::StringBuffer>([content UTF8String]);
|
|
38
|
+
runtime->evaluateJavaScript(codeBuffer, "evaluateJavaScript");
|
|
36
39
|
}
|
|
37
40
|
|
|
38
|
-
|
|
39
|
-
|
|
41
|
+
jsi::Runtime &rt = *runtime;
|
|
42
|
+
auto text = jsi::String::createFromUtf8(rt, [inputString UTF8String]);
|
|
43
|
+
|
|
44
|
+
auto func = rt.global().getPropertyAsFunction(rt, "parseExpensiMarkToRanges");
|
|
45
|
+
auto output = func.call(rt, text);
|
|
46
|
+
if (output.isUndefined()) {
|
|
47
|
+
return input;
|
|
48
|
+
}
|
|
49
|
+
const auto &ranges = output.asObject(rt).asArray(rt);
|
|
40
50
|
|
|
41
51
|
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:inputString attributes:attributes];
|
|
42
52
|
[attributedString beginEditing];
|
|
@@ -48,42 +58,43 @@
|
|
|
48
58
|
|
|
49
59
|
_blockquoteRangesAndLevels = [NSMutableArray new];
|
|
50
60
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
61
|
+
for (size_t i = 0, n = ranges.size(rt); i < n; ++i) {
|
|
62
|
+
const auto &item = ranges.getValueAtIndex(rt, i).asObject(rt);
|
|
63
|
+
const auto &type = item.getProperty(rt, "type").asString(rt).utf8(rt);
|
|
64
|
+
const auto &location = static_cast<int>(item.getProperty(rt, "start").asNumber());
|
|
65
|
+
const auto &length = static_cast<int>(item.getProperty(rt, "length").asNumber());
|
|
66
|
+
const auto &depth = item.hasProperty(rt, "depth") ? static_cast<int>(item.getProperty(rt, "depth").asNumber()) : 1;
|
|
67
|
+
|
|
57
68
|
NSRange range = NSMakeRange(location, length);
|
|
58
69
|
|
|
59
|
-
if (
|
|
70
|
+
if (type == "bold" || type == "italic" || type == "code" || type == "pre" || type == "h1" || type == "emoji") {
|
|
60
71
|
UIFont *font = [attributedString attribute:NSFontAttributeName atIndex:location effectiveRange:NULL];
|
|
61
|
-
if (
|
|
72
|
+
if (type == "bold") {
|
|
62
73
|
font = [RCTFont updateFont:font withWeight:@"bold"];
|
|
63
|
-
} else if (
|
|
74
|
+
} else if (type == "italic") {
|
|
64
75
|
font = [RCTFont updateFont:font withStyle:@"italic"];
|
|
65
|
-
} else if (
|
|
76
|
+
} else if (type == "code") {
|
|
66
77
|
font = [RCTFont updateFont:font withFamily:_markdownStyle.codeFontFamily
|
|
67
78
|
size:[NSNumber numberWithFloat:_markdownStyle.codeFontSize]
|
|
68
79
|
weight:nil
|
|
69
80
|
style:nil
|
|
70
81
|
variant:nil
|
|
71
82
|
scaleMultiplier:0];
|
|
72
|
-
} else if (
|
|
83
|
+
} else if (type == "pre") {
|
|
73
84
|
font = [RCTFont updateFont:font withFamily:_markdownStyle.preFontFamily
|
|
74
85
|
size:[NSNumber numberWithFloat:_markdownStyle.preFontSize]
|
|
75
86
|
weight:nil
|
|
76
87
|
style:nil
|
|
77
88
|
variant:nil
|
|
78
89
|
scaleMultiplier:0];
|
|
79
|
-
} else if (
|
|
90
|
+
} else if (type == "h1") {
|
|
80
91
|
font = [RCTFont updateFont:font withFamily:nil
|
|
81
92
|
size:[NSNumber numberWithFloat:_markdownStyle.h1FontSize]
|
|
82
93
|
weight:@"bold"
|
|
83
94
|
style:nil
|
|
84
95
|
variant:nil
|
|
85
96
|
scaleMultiplier:0];
|
|
86
|
-
} else if (
|
|
97
|
+
} else if (type == "emoji") {
|
|
87
98
|
font = [RCTFont updateFont:font withFamily:nil
|
|
88
99
|
size:[NSNumber numberWithFloat:_markdownStyle.emojiFontSize]
|
|
89
100
|
weight:nil
|
|
@@ -94,27 +105,27 @@
|
|
|
94
105
|
[attributedString addAttribute:NSFontAttributeName value:font range:range];
|
|
95
106
|
}
|
|
96
107
|
|
|
97
|
-
if (
|
|
108
|
+
if (type == "syntax") {
|
|
98
109
|
[attributedString addAttribute:NSForegroundColorAttributeName value:_markdownStyle.syntaxColor range:range];
|
|
99
|
-
} else if (
|
|
110
|
+
} else if (type == "strikethrough") {
|
|
100
111
|
[attributedString addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:range];
|
|
101
|
-
} else if (
|
|
112
|
+
} else if (type == "code") {
|
|
102
113
|
[attributedString addAttribute:NSForegroundColorAttributeName value:_markdownStyle.codeColor range:range];
|
|
103
114
|
[attributedString addAttribute:NSBackgroundColorAttributeName value:_markdownStyle.codeBackgroundColor range:range];
|
|
104
|
-
} else if (
|
|
115
|
+
} else if (type == "mention-here") {
|
|
105
116
|
[attributedString addAttribute:NSForegroundColorAttributeName value:_markdownStyle.mentionHereColor range:range];
|
|
106
117
|
[attributedString addAttribute:NSBackgroundColorAttributeName value:_markdownStyle.mentionHereBackgroundColor range:range];
|
|
107
|
-
} else if (
|
|
118
|
+
} else if (type == "mention-user") {
|
|
108
119
|
// TODO: change mention color when it mentions current user
|
|
109
120
|
[attributedString addAttribute:NSForegroundColorAttributeName value:_markdownStyle.mentionUserColor range:range];
|
|
110
121
|
[attributedString addAttribute:NSBackgroundColorAttributeName value:_markdownStyle.mentionUserBackgroundColor range:range];
|
|
111
|
-
} else if (
|
|
122
|
+
} else if (type == "mention-report") {
|
|
112
123
|
[attributedString addAttribute:NSForegroundColorAttributeName value:_markdownStyle.mentionReportColor range:range];
|
|
113
124
|
[attributedString addAttribute:NSBackgroundColorAttributeName value:_markdownStyle.mentionReportBackgroundColor range:range];
|
|
114
|
-
} else if (
|
|
125
|
+
} else if (type == "link") {
|
|
115
126
|
[attributedString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:range];
|
|
116
127
|
[attributedString addAttribute:NSForegroundColorAttributeName value:_markdownStyle.linkColor range:range];
|
|
117
|
-
} else if (
|
|
128
|
+
} else if (type == "blockquote") {
|
|
118
129
|
CGFloat indent = (_markdownStyle.blockquoteMarginLeft + _markdownStyle.blockquoteBorderWidth + _markdownStyle.blockquotePaddingLeft) * depth;
|
|
119
130
|
NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
|
|
120
131
|
paragraphStyle.firstLineHeadIndent = indent;
|
|
@@ -124,17 +135,17 @@
|
|
|
124
135
|
@"range": [NSValue valueWithRange:range],
|
|
125
136
|
@"depth": @(depth)
|
|
126
137
|
}];
|
|
127
|
-
} else if (
|
|
138
|
+
} else if (type == "pre") {
|
|
128
139
|
[attributedString addAttribute:NSForegroundColorAttributeName value:_markdownStyle.preColor range:range];
|
|
129
140
|
NSRange rangeForBackground = [inputString characterAtIndex:range.location] == '\n' ? NSMakeRange(range.location + 1, range.length - 1) : range;
|
|
130
141
|
[attributedString addAttribute:NSBackgroundColorAttributeName value:_markdownStyle.preBackgroundColor range:rangeForBackground];
|
|
131
142
|
// TODO: pass background color and ranges to layout manager
|
|
132
|
-
} else if (
|
|
143
|
+
} else if (type == "h1") {
|
|
133
144
|
NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
|
|
134
145
|
NSRange rangeWithHashAndSpace = NSMakeRange(range.location - 2, range.length + 2); // we also need to include prepending "# "
|
|
135
146
|
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:rangeWithHashAndSpace];
|
|
136
147
|
}
|
|
137
|
-
}
|
|
148
|
+
}
|
|
138
149
|
|
|
139
150
|
RCTApplyBaselineOffset(attributedString);
|
|
140
151
|
|
package/package.json
CHANGED