react-native-enriched-markdown-scaffold 1.0.2-scaffold.10 → 1.0.2-scaffold.11
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/android/src/main/java/com/swmansion/enriched/markdown/EnrichedMarkdown.kt +4 -0
- package/android/src/main/java/com/swmansion/enriched/markdown/utils/common/RenderedSegment.kt +5 -1
- package/android/src/math/java/com/swmansion/enriched/markdown/views/MathContainerView.kt +10 -0
- package/cpp/parser/MD4CParser.cpp +24 -2
- package/ios/EnrichedMarkdown.mm +5 -1
- package/ios/utils/RenderedMarkdownSegment.h +2 -1
- package/ios/utils/RenderedMarkdownSegment.m +2 -1
- package/ios/utils/SegmentRenderer.m +5 -1
- package/ios/views/ENRMMathContainerView.h +3 -0
- package/ios/views/ENRMMathContainerView.m +6 -0
- package/package.json +1 -1
|
@@ -666,6 +666,9 @@ class EnrichedMarkdown
|
|
|
666
666
|
mathContainerClass
|
|
667
667
|
?.getMethod("applyLatex", String::class.java)
|
|
668
668
|
?.invoke(view, segment.latex)
|
|
669
|
+
mathContainerClass
|
|
670
|
+
?.getMethod("setHighlighted", Boolean::class.javaPrimitiveType)
|
|
671
|
+
?.invoke(view, segment.highlighted)
|
|
669
672
|
}
|
|
670
673
|
|
|
671
674
|
is RenderedSegment.CodeBlock -> {
|
|
@@ -807,6 +810,7 @@ class EnrichedMarkdown
|
|
|
807
810
|
resolvedClass.getMethod("setOnMenuSuppressed", Function2::class.java).invoke(view, onMenuSuppressed)
|
|
808
811
|
}.onFailure { Log.e(TAG, "Failed to set onMenuSuppressed on math view", it) }
|
|
809
812
|
resolvedClass.getMethod("applyLatex", String::class.java).invoke(view, segment.latex)
|
|
813
|
+
resolvedClass.getMethod("setHighlighted", Boolean::class.javaPrimitiveType).invoke(view, segment.highlighted)
|
|
810
814
|
view
|
|
811
815
|
} catch (e: Exception) {
|
|
812
816
|
Log.e(TAG, "Failed to create math view", e)
|
package/android/src/main/java/com/swmansion/enriched/markdown/utils/common/RenderedSegment.kt
CHANGED
|
@@ -25,6 +25,7 @@ sealed interface RenderedSegment {
|
|
|
25
25
|
|
|
26
26
|
data class Math(
|
|
27
27
|
val latex: String,
|
|
28
|
+
val highlighted: Boolean,
|
|
28
29
|
override val signature: Long,
|
|
29
30
|
) : RenderedSegment
|
|
30
31
|
|
|
@@ -56,7 +57,10 @@ object MarkdownSegmentRenderer {
|
|
|
56
57
|
is MarkdownSegment.Math -> {
|
|
57
58
|
var signature = SegmentSignature.signatureForNode(null) xor SegmentSignature.MATH_KIND_SALT
|
|
58
59
|
signature = SegmentSignature.fnvMixString(signature, segment.latex)
|
|
59
|
-
|
|
60
|
+
val highlighted = segment.node.getAttribute("highlight") == "true"
|
|
61
|
+
// A highlight toggle keeps the latex, so it must change the signature or the view is skipped.
|
|
62
|
+
if (highlighted) signature = SegmentSignature.fnvMixString(signature, "highlight")
|
|
63
|
+
RenderedSegment.Math(segment.latex, highlighted, signature)
|
|
60
64
|
}
|
|
61
65
|
|
|
62
66
|
is MarkdownSegment.CodeBlock -> {
|
|
@@ -4,6 +4,7 @@ import android.content.ClipData
|
|
|
4
4
|
import android.content.ClipboardManager
|
|
5
5
|
import android.content.Context
|
|
6
6
|
import android.graphics.Canvas
|
|
7
|
+
import android.graphics.Color
|
|
7
8
|
import android.util.Log
|
|
8
9
|
import android.view.Gravity
|
|
9
10
|
import android.view.View
|
|
@@ -60,6 +61,15 @@ class MathContainerView(
|
|
|
60
61
|
|
|
61
62
|
private val mathView = RaTeXCanvasView(context, mathStyle.glyphOverflow)
|
|
62
63
|
|
|
64
|
+
private val highlightBackgroundColor = styleConfig.highlightStyle.backgroundColor
|
|
65
|
+
|
|
66
|
+
// Set reflectively: washes the formula's own frame for a `==$$…$$==` block.
|
|
67
|
+
var highlighted: Boolean = false
|
|
68
|
+
set(value) {
|
|
69
|
+
field = value
|
|
70
|
+
mathView.setBackgroundColor(if (value) highlightBackgroundColor else Color.TRANSPARENT)
|
|
71
|
+
}
|
|
72
|
+
|
|
63
73
|
init {
|
|
64
74
|
setBackgroundColor(mathStyle.backgroundColor)
|
|
65
75
|
|
|
@@ -386,12 +386,31 @@ bool isSeparatorNode(const MarkdownASTNode &node) {
|
|
|
386
386
|
(node.type == NodeType::Text && node.content.find_first_not_of(" \t\n\r") == std::string::npos);
|
|
387
387
|
}
|
|
388
388
|
|
|
389
|
+
// The display math a paragraph child stands for: the node itself, or the one display-math
|
|
390
|
+
// node a `==$$x$$==` highlight wraps. The highlight cannot survive promotion as a span
|
|
391
|
+
// (a block is not inline text), so the caller carries it over as the `highlight` attribute.
|
|
392
|
+
std::shared_ptr<MarkdownASTNode> displayMathOf(const std::shared_ptr<MarkdownASTNode> &node) {
|
|
393
|
+
if (isDisplayMathNode(*node))
|
|
394
|
+
return node;
|
|
395
|
+
if (node->type != NodeType::Highlight)
|
|
396
|
+
return nullptr;
|
|
397
|
+
std::shared_ptr<MarkdownASTNode> math;
|
|
398
|
+
for (const auto &child : node->children) {
|
|
399
|
+
if (isSeparatorNode(*child))
|
|
400
|
+
continue;
|
|
401
|
+
if (math || !isDisplayMathNode(*child))
|
|
402
|
+
return nullptr;
|
|
403
|
+
math = child;
|
|
404
|
+
}
|
|
405
|
+
return math;
|
|
406
|
+
}
|
|
407
|
+
|
|
389
408
|
// A display-math node is "isolated" — i.e. block-level — when at least one of its
|
|
390
409
|
// neighbours is a boundary: the paragraph edge, or a separator (LineBreak / blank
|
|
391
410
|
// text). Display math flanked by real text on BOTH sides (e.g. `a $$x$$ b`) is
|
|
392
411
|
// genuinely inline and is left in place.
|
|
393
412
|
bool isIsolatedDisplayMath(const std::vector<std::shared_ptr<MarkdownASTNode>> &children, size_t i) {
|
|
394
|
-
if (!
|
|
413
|
+
if (!displayMathOf(children[i]))
|
|
395
414
|
return false;
|
|
396
415
|
bool leftBoundary = (i == 0) || isSeparatorNode(*children[i - 1]);
|
|
397
416
|
bool rightBoundary = (i + 1 >= children.size()) || isSeparatorNode(*children[i + 1]);
|
|
@@ -446,7 +465,10 @@ splitParagraphAroundDisplayMath(const std::vector<std::shared_ptr<MarkdownASTNod
|
|
|
446
465
|
for (size_t i = 0; i < children.size(); ++i) {
|
|
447
466
|
if (isIsolatedDisplayMath(children, i)) {
|
|
448
467
|
flushInlineRun();
|
|
449
|
-
|
|
468
|
+
auto math = displayMathOf(children[i]);
|
|
469
|
+
if (math != children[i])
|
|
470
|
+
math->setAttribute("highlight", "true");
|
|
471
|
+
result.push_back(math);
|
|
450
472
|
} else {
|
|
451
473
|
inlineRun.push_back(children[i]);
|
|
452
474
|
}
|
package/ios/EnrichedMarkdown.mm
CHANGED
|
@@ -405,7 +405,10 @@ static char kENRMSegmentFadeAnimatorKey;
|
|
|
405
405
|
return view;
|
|
406
406
|
}
|
|
407
407
|
updateView:^(RCTUIView *view, ENRMRenderedSegment *segment) {
|
|
408
|
-
|
|
408
|
+
ENRMMathSegment *mathSegment = (ENRMMathSegment *)segment.mathSegment;
|
|
409
|
+
ENRMMathContainerView *mathView = (ENRMMathContainerView *)view;
|
|
410
|
+
[mathView applyLatex:mathSegment.latex];
|
|
411
|
+
mathView.highlighted = mathSegment.highlighted;
|
|
409
412
|
}]];
|
|
410
413
|
#endif
|
|
411
414
|
|
|
@@ -1021,6 +1024,7 @@ static char kENRMSegmentFadeAnimatorKey;
|
|
|
1021
1024
|
mathView.onCopyRequest = [self blockCopyRequestForSource:@"math"];
|
|
1022
1025
|
mathView.onMenuSuppressed = [self blockMenuSuppressedForSource:@"math" view:mathView];
|
|
1023
1026
|
[mathView applyLatex:mathSegment.latex];
|
|
1027
|
+
mathView.highlighted = mathSegment.highlighted;
|
|
1024
1028
|
return mathView;
|
|
1025
1029
|
}
|
|
1026
1030
|
#endif
|
|
@@ -26,7 +26,8 @@ typedef NS_ENUM(NSInteger, ENRMSegmentKind) {
|
|
|
26
26
|
|
|
27
27
|
@interface ENRMMathSegment : NSObject
|
|
28
28
|
@property (nonatomic, strong) NSString *latex;
|
|
29
|
-
|
|
29
|
+
@property (nonatomic, assign) BOOL highlighted;
|
|
30
|
+
+ (instancetype)segmentWithLatex:(NSString *)latex highlighted:(BOOL)highlighted;
|
|
30
31
|
@end
|
|
31
32
|
|
|
32
33
|
@interface ENRMCodeBlockSegment : NSObject
|
|
@@ -22,11 +22,12 @@
|
|
|
22
22
|
@end
|
|
23
23
|
|
|
24
24
|
@implementation ENRMMathSegment
|
|
25
|
-
+ (instancetype)segmentWithLatex:(NSString *)latex
|
|
25
|
+
+ (instancetype)segmentWithLatex:(NSString *)latex highlighted:(BOOL)highlighted
|
|
26
26
|
{
|
|
27
27
|
NSParameterAssert(latex != nil);
|
|
28
28
|
ENRMMathSegment *segment = [[ENRMMathSegment alloc] init];
|
|
29
29
|
segment.latex = latex;
|
|
30
|
+
segment.highlighted = highlighted;
|
|
30
31
|
return segment;
|
|
31
32
|
}
|
|
32
33
|
@end
|
|
@@ -26,7 +26,8 @@ static NSArray *ENRMSplitASTIntoSegments(MarkdownASTNode *root)
|
|
|
26
26
|
[currentTextNodes removeAllObjects];
|
|
27
27
|
}
|
|
28
28
|
NSString *latex = child.children.count > 0 ? child.children.firstObject.content : child.content;
|
|
29
|
-
|
|
29
|
+
BOOL highlighted = [child.attributes[@"highlight"] isEqualToString:@"true"];
|
|
30
|
+
[segments addObject:[ENRMMathSegment segmentWithLatex:latex ?: @"" highlighted:highlighted]];
|
|
30
31
|
#else
|
|
31
32
|
// TODO: Fix block math rendering on macOS. Adding ENRMMathContainerView as a
|
|
32
33
|
// segment causes all preceding text segments to become invisible.
|
|
@@ -86,6 +87,9 @@ NSArray<ENRMRenderedSegment *> *ENRMRenderSegmentsFromAST(MarkdownASTNode *ast,
|
|
|
86
87
|
signature ^= (uint8_t)*utf8++;
|
|
87
88
|
signature *= 1099511628211ULL;
|
|
88
89
|
}
|
|
90
|
+
// A highlight toggle keeps the latex, so it must change the signature or the view is skipped.
|
|
91
|
+
signature ^= mathSegment.highlighted ? 1ULL : 0ULL;
|
|
92
|
+
signature *= 1099511628211ULL;
|
|
89
93
|
[renderedSegments addObject:[ENRMRenderedSegment mathSegmentWithSegment:mathSegment signature:signature]];
|
|
90
94
|
}
|
|
91
95
|
#endif
|
|
@@ -24,6 +24,9 @@ NS_ASSUME_NONNULL_BEGIN
|
|
|
24
24
|
+ (CGFloat)measureHeightForLatex:(NSString *)latex config:(StyleConfig *)config maxWidth:(CGFloat)maxWidth;
|
|
25
25
|
|
|
26
26
|
@property (nonatomic, strong) StyleConfig *config;
|
|
27
|
+
|
|
28
|
+
/// Washes the formula's own frame in the highlight background (a `==$$…$$==` block).
|
|
29
|
+
@property (nonatomic, assign) BOOL highlighted;
|
|
27
30
|
@property (nonatomic, copy, readonly) NSString *cachedLatex;
|
|
28
31
|
@property (nonatomic, strong, nullable) ENRMAccessibilityLabels *accessibilityLabels;
|
|
29
32
|
|
|
@@ -94,6 +94,12 @@
|
|
|
94
94
|
return self;
|
|
95
95
|
}
|
|
96
96
|
|
|
97
|
+
- (void)setHighlighted:(BOOL)highlighted
|
|
98
|
+
{
|
|
99
|
+
_highlighted = highlighted;
|
|
100
|
+
_mathView.backgroundColor = highlighted ? self.config.highlightBackgroundColor : [RCTUIColor clearColor];
|
|
101
|
+
}
|
|
102
|
+
|
|
97
103
|
- (void)applyLatex:(NSString *)latex
|
|
98
104
|
{
|
|
99
105
|
_cachedLatex = [latex copy];
|
package/package.json
CHANGED