Skip to content

Commit 6a6e29b

Browse files
authored
Merge pull request #4 from software-mansion-labs/@hryhoriiK97/use-consistent-spacers-between-parsed-elements
Use consistent spacing between parsed elements.
2 parents 8543782 + 6e91860 commit 6a6e29b

5 files changed

Lines changed: 41 additions & 16 deletions

File tree

android/src/main/java/com/richtext/renderer/NodeRenderer.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.richtext.spans.RichTextLinkSpan
66
import com.richtext.spans.RichTextHeadingSpan
77
import com.richtext.spans.RichTextParagraphSpan
88
import com.richtext.spans.RichTextTextSpan
9+
import com.richtext.utils.addSpacing
910
import org.commonmark.node.*
1011

1112
interface NodeRenderer {
@@ -56,7 +57,7 @@ class ParagraphRenderer : NodeRenderer {
5657
)
5758
}
5859

59-
builder.append("\n")
60+
builder.addSpacing()
6061
}
6162
}
6263

@@ -84,7 +85,8 @@ class HeadingRenderer : NodeRenderer {
8485
android.text.SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE
8586
)
8687
}
87-
builder.append("\n")
88+
89+
builder.addSpacing()
8890
}
8991
}
9092

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.richtext.utils
2+
3+
import android.text.SpannableStringBuilder
4+
5+
/**
6+
* Adds Zero Width Space spacing between markdown elements.
7+
*
8+
* Uses \u200B (Zero Width Space) characters for spacing because:
9+
* - Invisible but takes up space, providing consistent visual spacing
10+
* - Doesn't interfere with text rendering or font metrics
11+
*/
12+
fun SpannableStringBuilder.addSpacing() {
13+
append("\u200B\n\u200B\n")
14+
}

ios/renderer/AttributedRenderer.m

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#import "NodeRenderer.h"
33
#import "RenderContext.h"
44
#import "MarkdownASTNode.h"
5+
#import "SpacingUtils.h"
56

67
@interface ParagraphRenderer : NSObject <NodeRenderer>
78
@end
@@ -59,15 +60,8 @@ - (void)renderNodeRecursive:(MarkdownASTNode *)node
5960
for (NSUInteger i = 0; i < node.children.count; i++) {
6061
MarkdownASTNode *child = node.children[i];
6162
[self renderNodeRecursive:child into:out font:font color:color context:context isTopLevel:NO];
62-
// Add spacing between paragraphs (MD4C doesn't provide empty lines between blocks)
63-
// This is intentional rendering behavior to match markdown visual expectations
6463
if (child.type == MarkdownNodeTypeParagraph && i < node.children.count - 1) {
65-
NSAttributedString *spacing = [[NSAttributedString alloc]
66-
initWithString:@"\n\n"
67-
attributes:@{
68-
NSFontAttributeName: font,
69-
NSForegroundColorAttributeName: color
70-
}];
64+
NSAttributedString *spacing = createSpacing();
7165
[out appendAttributedString:spacing];
7266
}
7367
}
@@ -255,12 +249,7 @@ - (void)renderNode:(MarkdownASTNode *)node
255249
}
256250
}
257251

258-
NSAttributedString *spacing = [[NSAttributedString alloc]
259-
initWithString:@"\n\n"
260-
attributes:@{
261-
NSFontAttributeName: font,
262-
NSForegroundColorAttributeName: color
263-
}];
252+
NSAttributedString *spacing = createSpacing();
264253
[output appendAttributedString:spacing];
265254
}
266255
@end

ios/utils/SpacingUtils.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#import <Foundation/Foundation.h>
2+
#import <UIKit/UIKit.h>
3+
4+
NS_ASSUME_NONNULL_BEGIN
5+
6+
/**
7+
* Creates NSAttributedString with Zero Width Space spacing.
8+
*
9+
* Uses \u200B (Zero Width Space) characters for spacing because:
10+
* - Invisible but takes up space, providing consistent visual spacing
11+
* - Doesn't interfere with text rendering or font metrics
12+
*/
13+
extern NSAttributedString *createSpacing(void);
14+
15+
NS_ASSUME_NONNULL_END

ios/utils/SpacingUtils.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#import "SpacingUtils.h"
2+
3+
NSAttributedString *createSpacing(void) {
4+
return [[NSAttributedString alloc] initWithString:@"\u200B\n\u200B\n"];
5+
}

0 commit comments

Comments
 (0)