Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions android/src/main/java/com/richtext/renderer/NodeRenderer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.richtext.spans.RichTextLinkSpan
import com.richtext.spans.RichTextHeadingSpan
import com.richtext.spans.RichTextParagraphSpan
import com.richtext.spans.RichTextTextSpan
import com.richtext.utils.addSpacing
import org.commonmark.node.*

interface NodeRenderer {
Expand Down Expand Up @@ -56,7 +57,7 @@ class ParagraphRenderer : NodeRenderer {
)
}

builder.append("\n")
builder.addSpacing()
}
}

Expand Down Expand Up @@ -84,7 +85,8 @@ class HeadingRenderer : NodeRenderer {
android.text.SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE
)
}
builder.append("\n")

builder.addSpacing()
}
}

Expand Down
14 changes: 14 additions & 0 deletions android/src/main/java/com/richtext/utils/Utils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.richtext.utils

import android.text.SpannableStringBuilder

/**
* Adds Zero Width Space spacing between markdown elements.
*
* Uses \u200B (Zero Width Space) characters for spacing because:
* - Invisible but takes up space, providing consistent visual spacing
* - Doesn't interfere with text rendering or font metrics
*/
fun SpannableStringBuilder.addSpacing() {
append("\u200B\n\u200B\n")
}
17 changes: 3 additions & 14 deletions ios/renderer/AttributedRenderer.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#import "NodeRenderer.h"
#import "RenderContext.h"
#import "MarkdownASTNode.h"
#import "SpacingUtils.h"

@interface ParagraphRenderer : NSObject <NodeRenderer>
@end
Expand Down Expand Up @@ -59,15 +60,8 @@ - (void)renderNodeRecursive:(MarkdownASTNode *)node
for (NSUInteger i = 0; i < node.children.count; i++) {
MarkdownASTNode *child = node.children[i];
[self renderNodeRecursive:child into:out font:font color:color context:context isTopLevel:NO];
// Add spacing between paragraphs (MD4C doesn't provide empty lines between blocks)
// This is intentional rendering behavior to match markdown visual expectations
if (child.type == MarkdownNodeTypeParagraph && i < node.children.count - 1) {
NSAttributedString *spacing = [[NSAttributedString alloc]
initWithString:@"\n\n"
attributes:@{
NSFontAttributeName: font,
NSForegroundColorAttributeName: color
}];
NSAttributedString *spacing = createSpacing();
[out appendAttributedString:spacing];
}
}
Expand Down Expand Up @@ -255,12 +249,7 @@ - (void)renderNode:(MarkdownASTNode *)node
}
}

NSAttributedString *spacing = [[NSAttributedString alloc]
initWithString:@"\n\n"
attributes:@{
NSFontAttributeName: font,
NSForegroundColorAttributeName: color
}];
NSAttributedString *spacing = createSpacing();
[output appendAttributedString:spacing];
}
@end
Expand Down
15 changes: 15 additions & 0 deletions ios/utils/SpacingUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

/**
* Creates NSAttributedString with Zero Width Space spacing.
*
* Uses \u200B (Zero Width Space) characters for spacing because:
* - Invisible but takes up space, providing consistent visual spacing
* - Doesn't interfere with text rendering or font metrics
*/
extern NSAttributedString *createSpacing(void);

NS_ASSUME_NONNULL_END
5 changes: 5 additions & 0 deletions ios/utils/SpacingUtils.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#import "SpacingUtils.h"

NSAttributedString *createSpacing(void) {
return [[NSAttributedString alloc] initWithString:@"\u200B\n\u200B\n"];
}