Files
paseo/packages/react-native-enriched-markdown/ios/utils/ParagraphStyleUtils.m
T
Mohamed Boudra 80c44321cc Move the Markdown engine into the repository and fix two Android list layouts (#10499)
* chore(app): vendor react-native-enriched-markdown with the Paseo patch applied

Copy react-native-enriched-markdown@1.2.0-nightly-20261014-766fc0e67 (upstream
software-mansion/enriched-markdown 766fc0e67) into
packages/react-native-enriched-markdown, with
patches/react-native-enriched-markdown+1.2.0-nightly-20261014-766fc0e67.patch
applied. Nothing else changes in this commit; the package is not a workspace yet.

Reproduce:

  npm pack react-native-enriched-markdown@1.2.0-nightly-20261014-766fc0e67
  tar xzf react-native-enriched-markdown-*.tgz
  mkdir -p t/node_modules && mv package t/node_modules/react-native-enriched-markdown
  (cd t && git init -q && git apply ../patches/react-native-enriched-markdown+*.patch)
  diff -r t/node_modules/react-native-enriched-markdown packages/react-native-enriched-markdown

The diff is empty.

* chore(app): consume react-native-enriched-markdown from its workspace

Make packages/react-native-enriched-markdown a private workspace and point the
app at it, then remove the patch-package patch it replaces.

- package.json: private, version follows the root like every workspace, only the
  postinstall script that downloads tree-sitter grammars. Upstream's
  devDependencies would install a second react-native, so they are gone.
- The package keeps its name, so autolinking, the Gradle project
  :react-native-enriched-markdown, the CocoaPods spec and imports are unchanged.
- Lockfile: drop the nested registry copy under packages/app/node_modules; npm
  links node_modules/react-native-enriched-markdown to the workspace.
- Formatting, lint and knip skip the vendored code. CI routes changes to it
  through the app and browser suites.
- The codegen test checks the vendored package's generated props.
- docs/markdown-renderer.md describes the fork and how to edit it.

* fix(app): keep numbered sub-list markers off their text on Android

From #9964, moved from the package patch into the vendored package.

On Android, a numbered list nested in a bulleted item drew "1." over
the first letters of each item. A nested item's text indent summed the
root list's narrow bullet column with a fixed marginLeft, while its
marker was right-aligned to its own wider "99." column, so the number
landed on the text. iOS already indents from the item's own column.

Backport the Android hunks of upstream enriched-markdown #1015 (size
the number column to the widest marker) and #1023 (nested items start
their text from their own marker column).

test:android now also runs the backported ListRenderingTest and
TaskListInteractionTest suites. docs/markdown-renderer.md lists the
fork's backports, this one and #924, which the removed postinstall
comment used to track.

The vendored tree equals the #9964 patch applied to the pristine
package.

* fix(app): keep Android assistant replies inside their turns

From #10380, moved from the package patch into the vendored package.

On Android, an assistant reply with blockquotes nested inside a list
could overlap its "Worked for ..." footer and the next user message:
the displayed text and the layout measurement applied spans in
different orders, and the list-wide line height inflated the blockquote
spacers when it ran after them.

Use the same span order for Android Markdown measurement and display.
Apply list line height before blockquote spacers so nested quotes keep
their configured padding and the reply fits above the turn footer.

test:android now also runs ListBlockquoteMeasurementTest.

Applied on top of #9964; the two changes share only ListRenderer.kt,
where #10380's import and line-height call sit beside #9964's marker
width code. Every other file equals the #10380 patch applied to the
pristine package.

* chore(app): match the vendored Markdown web block renderer source to its build

The package patch changed lib/module/web/renderers/BlockRenderers.js (the
renderers.codeBlock override and the zoom-in cursor on images) but not
src/web/renderers/BlockRenderers.tsx. The app runs lib, so behavior does
not change. Port both changes into src so src stays the source of lib.
2026-11-23 10:15:43 +07:00

486 lines
18 KiB
Objective-C

#import "ParagraphStyleUtils.h"
#import "ENRMFeatureFlags.h"
#import "ENRMImageAttachment.h"
#import "ENRMLinkPillAttachment.h"
#import "LastElementUtils.h"
#import <React/RCTI18nUtil.h>
#if ENRICHED_MARKDOWN_MATH
#import "ENRMMathInlineAttachment.h"
#endif
NSAttributedString *kNewlineAttributedString;
static NSParagraphStyle *kBlockSpacerTemplate;
NSLineBreakStrategy ENRMResolveLineBreakStrategy(NSString *strategy)
{
if ([strategy isEqualToString:@"standard"]) {
return NSLineBreakStrategyStandard;
} else if ([strategy isEqualToString:@"hangul-word"]) {
return NSLineBreakStrategyHangulWordPriority;
} else if ([strategy isEqualToString:@"push-out"]) {
return NSLineBreakStrategyPushOut;
}
return NSLineBreakStrategyNone;
}
NSLineBreakMode ENRMResolveEllipsizeLineBreakMode(NSString *mode)
{
if ([mode isEqualToString:@"head"]) {
return NSLineBreakByTruncatingHead;
} else if ([mode isEqualToString:@"middle"]) {
return NSLineBreakByTruncatingMiddle;
} else if ([mode isEqualToString:@"clip"]) {
return NSLineBreakByClipping;
}
return NSLineBreakByTruncatingTail;
}
__attribute__((constructor)) static void initParagraphStyleUtils(void)
{
kNewlineAttributedString = [[NSAttributedString alloc] initWithString:@"\n"];
NSMutableParagraphStyle *template = [[NSMutableParagraphStyle alloc] init];
template.minimumLineHeight = 1;
template.maximumLineHeight = 1;
kBlockSpacerTemplate = [template copy];
}
ENRMWritingDirectionMode ENRMResolveWritingDirectionMode(NSString *value)
{
if ([value isEqualToString:@"ltr"]) {
return ENRMWritingDirectionModeLTR;
}
if ([value isEqualToString:@"rtl"]) {
return ENRMWritingDirectionModeRTL;
}
if ([value isEqualToString:@"auto"]) {
return ENRMWritingDirectionModeAuto;
}
return ENRMWritingDirectionModeFirstStrong;
}
static BOOL ENRMIsStrongRTLChar(unichar c)
{
return (c >= 0x0590 && c <= 0x08FF) || (c >= 0xFB1D && c <= 0xFDFF) || (c >= 0xFE70 && c <= 0xFEFF);
}
NSWritingDirection ENRMFirstStrongDirection(NSString *text)
{
NSCharacterSet *letters = [NSCharacterSet letterCharacterSet];
NSUInteger length = text.length;
for (NSUInteger i = 0; i < length; i++) {
unichar c = [text characterAtIndex:i];
if (![letters characterIsMember:c]) {
continue;
}
return ENRMIsStrongRTLChar(c) ? NSWritingDirectionRightToLeft : NSWritingDirectionLeftToRight;
}
return NSWritingDirectionNatural;
}
BOOL ENRMParagraphIsRTL(NSParagraphStyle *style)
{
if (style && style.baseWritingDirection != NSWritingDirectionNatural) {
return style.baseWritingDirection == NSWritingDirectionRightToLeft;
}
return [[RCTI18nUtil sharedInstance] isRTL];
}
void ENRMApplyWritingDirectionMode(NSMutableAttributedString *output, ENRMWritingDirectionMode mode,
NSWritingDirection layoutDirection)
{
switch (mode) {
case ENRMWritingDirectionModeAuto:
ENRMApplyWritingDirectionToParagraphStyles(output, NSWritingDirectionNatural);
break;
case ENRMWritingDirectionModeLTR:
ENRMApplyWritingDirectionToParagraphStyles(output, NSWritingDirectionLeftToRight);
break;
case ENRMWritingDirectionModeRTL:
ENRMApplyWritingDirectionToParagraphStyles(output, NSWritingDirectionRightToLeft);
break;
case ENRMWritingDirectionModeFirstStrong:
ENRMApplyFirstStrongParagraphDirections(output, layoutDirection);
break;
}
}
void ENRMApplyFirstStrongParagraphDirections(NSMutableAttributedString *output, NSWritingDirection fallback)
{
if (output.length == 0) {
return;
}
NSString *string = output.string;
NSUInteger length = string.length;
NSUInteger position = 0;
while (position < length) {
NSRange paragraphRange = [string paragraphRangeForRange:NSMakeRange(position, 0)];
if (NSMaxRange(paragraphRange) <= position) {
break;
}
position = NSMaxRange(paragraphRange);
NSNumber *isCodeBlock = [output attribute:CodeBlockAttributeName
atIndex:paragraphRange.location
effectiveRange:nil];
if (isCodeBlock.boolValue) {
continue;
}
NSWritingDirection direction = ENRMFirstStrongDirection([string substringWithRange:paragraphRange]);
if (direction == NSWritingDirectionNatural) {
direction = fallback;
}
if (direction == NSWritingDirectionNatural) {
continue;
}
[output enumerateAttribute:NSParagraphStyleAttributeName
inRange:paragraphRange
options:0
usingBlock:^(NSParagraphStyle *style, NSRange range, BOOL *stop) {
if (style != nil && style.baseWritingDirection == direction) {
return;
}
NSMutableParagraphStyle *updated =
style ? [style mutableCopy] : [[NSMutableParagraphStyle alloc] init];
updated.baseWritingDirection = direction;
[output addAttribute:NSParagraphStyleAttributeName value:updated range:range];
}];
}
}
void ENRMApplyWritingDirectionToParagraphStyles(NSMutableAttributedString *output, NSWritingDirection writingDirection)
{
if (output.length == 0) {
return;
}
[output enumerateAttribute:NSParagraphStyleAttributeName
inRange:NSMakeRange(0, output.length)
options:0
usingBlock:^(NSParagraphStyle *style, NSRange range, BOOL *stop) {
if (!style) {
return;
}
// Skip runs already in the target direction (issue #739),
// mirroring ENRMApplyFirstStrongParagraphDirections.
if (style.baseWritingDirection == writingDirection) {
return;
}
NSNumber *isCodeBlock = [output attribute:CodeBlockAttributeName
atIndex:range.location
effectiveRange:nil];
if (isCodeBlock.boolValue) {
return;
}
NSMutableParagraphStyle *mutable = [style mutableCopy];
mutable.baseWritingDirection = writingDirection;
[output addAttribute:NSParagraphStyleAttributeName value:mutable range:range];
}];
}
NSMutableParagraphStyle *getOrCreateParagraphStyle(NSMutableAttributedString *output, NSUInteger index)
{
NSParagraphStyle *existing = [output attribute:NSParagraphStyleAttributeName atIndex:index effectiveRange:NULL];
NSMutableParagraphStyle *style = existing ? [existing mutableCopy] : [[NSMutableParagraphStyle alloc] init];
return style;
}
void applyParagraphSpacingAfter(NSMutableAttributedString *output, NSUInteger start, CGFloat marginBottom)
{
[output appendAttributedString:kNewlineAttributedString];
NSMutableParagraphStyle *style = getOrCreateParagraphStyle(output, start);
style.paragraphSpacing = marginBottom;
NSRange range = NSMakeRange(start, output.length - start);
[output addAttribute:NSParagraphStyleAttributeName value:style range:range];
}
NSUInteger applyParagraphSpacingBefore(NSMutableAttributedString *output, NSRange range, CGFloat marginTop)
{
if (marginTop <= 0 || range.location == 0)
return 0;
NSUInteger prevIdx = range.location - 1;
if ([output.string characterAtIndex:prevIdx] != '\n')
return 0;
NSParagraphStyle *prevStyle = [output attribute:NSParagraphStyleAttributeName atIndex:prevIdx effectiveRange:NULL];
CGFloat prevMarginBottom = prevStyle.paragraphSpacing;
if (prevMarginBottom >= marginTop)
return 0;
CGFloat extraGap = marginTop - prevMarginBottom;
NSMutableParagraphStyle *spacerStyle = [kBlockSpacerTemplate mutableCopy];
spacerStyle.paragraphSpacing = MAX(0, extraGap - 1.0);
NSDictionary *attrs = @{NSParagraphStyleAttributeName : spacerStyle};
NSAttributedString *spacer = [[NSAttributedString alloc] initWithString:@"\n" attributes:attrs];
[output insertAttributedString:spacer atIndex:range.location];
return 1;
}
NSUInteger applyBlockSpacingBefore(NSMutableAttributedString *output, NSUInteger insertionPoint, CGFloat marginTop)
{
if (marginTop <= 0) {
return 0;
}
CGFloat spacing = marginTop;
// At index 0 the spacer \n produces a 1pt line fragment (minimumLineHeight=1)
// on top of paragraphSpacing. Subtract it so the total equals the desired margin.
// For non-zero positions the 1pt is intentional inter-block spacing.
if (insertionPoint == 0) {
spacing = MAX(0, marginTop - 1.0);
}
NSMutableParagraphStyle *spacerStyle = [kBlockSpacerTemplate mutableCopy];
spacerStyle.paragraphSpacing = spacing;
NSAttributedString *spacer =
[[NSAttributedString alloc] initWithString:@"\n" attributes:@{NSParagraphStyleAttributeName : spacerStyle}];
[output insertAttributedString:spacer atIndex:insertionPoint];
return 1;
}
void applyBlockSpacingAfter(NSMutableAttributedString *output, CGFloat marginBottom)
{
if (marginBottom <= 0) {
return;
}
NSUInteger spacerLocation = output.length;
[output appendAttributedString:kNewlineAttributedString];
NSMutableParagraphStyle *spacerStyle = [kBlockSpacerTemplate mutableCopy];
spacerStyle.paragraphSpacing = marginBottom;
[output addAttribute:NSParagraphStyleAttributeName value:spacerStyle range:NSMakeRange(spacerLocation, 1)];
}
CGFloat ENRMLineHeightWithLinkPills(NSAttributedString *text, NSRange range, CGFloat lineHeight)
{
#if !TARGET_OS_OSX
__block CGFloat fitting = lineHeight;
[text enumerateAttribute:NSAttachmentAttributeName
inRange:range
options:0
usingBlock:^(id value, __unused NSRange subrange, __unused BOOL *stop) {
if ([value isKindOfClass:ENRMLinkPillAttachment.class])
fitting = MAX(fitting, ((ENRMLinkPillAttachment *)value).lineHeight);
}];
return fitting;
#else
return lineHeight;
#endif
}
// UIKit's own key for the pre-substitution font. Not exported by any SDK header, so it is spelled out
// here; it is the same string UIKit writes into a UITextView's storage when it substitutes a font.
static NSString *const ENRMOriginalFontAttributeName = @"NSOriginalFont";
void ENRMPinLineMetricsToStyledFonts(NSMutableAttributedString *output, NSRange range)
{
if (range.length == 0 || NSMaxRange(range) > output.length) {
return;
}
[output enumerateAttribute:NSFontAttributeName
inRange:range
options:0
usingBlock:^(UIFont *font, NSRange fontRange, __unused BOOL *stop) {
if (font) {
[output addAttribute:ENRMOriginalFontAttributeName value:font range:fontRange];
}
}];
}
// Floor, not clamp: minimumLineHeight keeps short lines at lineHeight, while maximumLineHeight = 0 lets
// a line grow to fit a taller run (large inline code, math, images) instead of clipping it, e.g.
// https://github.com/software-mansion/enriched-markdown/issues/827. We can diverge from RN's clamp
// because we measure the real laid-out height, so grown lines are reserved.
void ENRMApplyLineHeightToParagraphStyle(NSMutableParagraphStyle *style, CGFloat lineHeight)
{
if (lineHeight <= 0) {
return;
}
style.minimumLineHeight = lineHeight;
style.maximumLineHeight = 0;
}
void applyLineHeight(NSMutableAttributedString *output, NSRange range, CGFloat lineHeight)
{
lineHeight = ENRMLineHeightWithLinkPills(output, range, lineHeight);
if (lineHeight <= 0) {
return;
}
NSMutableParagraphStyle *style = getOrCreateParagraphStyle(output, range.location);
ENRMApplyLineHeightToParagraphStyle(style, lineHeight);
[output addAttribute:NSParagraphStyleAttributeName value:style range:range];
}
// Calculates the offset needed to center the text within the line height. Returns 0
// when the range has no minimum line height or its content already fills the line.
CGFloat calculateBaselineOffset(NSAttributedString *output, NSRange range)
{
if (range.length == 0) {
return 0;
}
// applyLineHeight floors lines with minimumLineHeight (maximumLineHeight is 0 for grown lines,
// or equal to the minimum for re-clamped code blocks), so the target is the minimum line height.
__block CGFloat targetLineHeight = 0;
[output enumerateAttribute:NSParagraphStyleAttributeName
inRange:range
options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired
usingBlock:^(NSParagraphStyle *paragraphStyle, __unused NSRange subrange, __unused BOOL *stop) {
if (!paragraphStyle) {
return;
}
targetLineHeight = MAX(paragraphStyle.minimumLineHeight, targetLineHeight);
}];
if (targetLineHeight <= 0) {
return 0;
}
// Center on real text; on a math-only line center the math box instead of its font.
__block CGFloat textLineHeight = 0;
#if ENRICHED_MARKDOWN_MATH
__block BOOL hasMath = NO;
#endif
[output enumerateAttributesInRange:range
options:0
usingBlock:^(NSDictionary<NSAttributedStringKey, id> *attrs, __unused NSRange subrange,
__unused BOOL *stop) {
#if ENRICHED_MARKDOWN_MATH
if ([attrs[NSAttachmentAttributeName] isKindOfClass:[ENRMMathInlineAttachment class]]) {
hasMath = YES;
return;
}
#endif
UIFont *font = attrs[NSFontAttributeName];
if (font) {
textLineHeight = MAX(UIFontLineHeight(font), textLineHeight);
}
}];
CGFloat contentLineHeight = textLineHeight;
#if !TARGET_OS_OSX
__block CGFloat pillBoxHeight = 0;
[output enumerateAttribute:NSAttachmentAttributeName
inRange:range
options:0
usingBlock:^(id value, NSRange subrange, BOOL *stop) {
if ([value isKindOfClass:ENRMLinkPillAttachment.class])
pillBoxHeight = MAX(pillBoxHeight, ((ENRMLinkPillAttachment *)value).boxHeight);
}];
contentLineHeight = MAX(contentLineHeight, pillBoxHeight);
#endif
#if ENRICHED_MARKDOWN_MATH
// Math only grows the content height, so measure it (parsing LaTeX) only when text
// alone wouldn't already fill the line.
if (hasMath && targetLineHeight > textLineHeight) {
__block CGFloat mathBoxHeight = 0;
[output enumerateAttribute:NSAttachmentAttributeName
inRange:range
options:0
usingBlock:^(id value, __unused NSRange subrange, __unused BOOL *stop) {
if ([value isKindOfClass:[ENRMMathInlineAttachment class]]) {
mathBoxHeight = MAX(((ENRMMathInlineAttachment *)value).boxHeight, mathBoxHeight);
}
}];
contentLineHeight = MAX(contentLineHeight, mathBoxHeight);
}
#endif
if (contentLineHeight <= 0 || targetLineHeight <= contentLineHeight) {
return 0;
}
return (targetLineHeight - contentLineHeight) / 2.0;
}
// Centers text within its line height (see calculateBaselineOffset). Called per block over its
// own range so blocks can keep different line heights. Ranges that already carry a baseline offset
// are left untouched, keeping nesting (e.g. a blockquote wrapping list items) idempotent.
void applyBaselineOffset(NSMutableAttributedString *output, NSRange range)
{
CGFloat baseLineOffset = calculateBaselineOffset(output, range);
if (baseLineOffset <= 0) {
return;
}
[output enumerateAttribute:NSBaselineOffsetAttributeName
inRange:range
options:0
usingBlock:^(NSNumber *existingOffset, NSRange subrange, BOOL *stop) {
if (existingOffset != nil) {
return;
}
[output enumerateAttribute:NSAttachmentAttributeName
inRange:subrange
options:0
usingBlock:^(id attachment, NSRange attachmentRange, BOOL *innerStop) {
if ([attachment isKindOfClass:[ENRMImageAttachment class]] &&
!((ENRMImageAttachment *)attachment).isInline) {
return;
}
[output addAttribute:NSBaselineOffsetAttributeName
value:@(baseLineOffset)
range:attachmentRange];
}];
}];
}
void applyTextAlignment(NSMutableAttributedString *output, NSRange range, NSTextAlignment textAlign)
{
NSMutableParagraphStyle *style = getOrCreateParagraphStyle(output, range.location);
style.alignment = textAlign;
[output addAttribute:NSParagraphStyleAttributeName value:style range:range];
}
void ENRMApplyLineBreakStrategyToParagraphStyles(NSMutableAttributedString *output,
NSLineBreakStrategy lineBreakStrategy)
{
if (output.length == 0) {
return;
}
[output enumerateAttribute:NSParagraphStyleAttributeName
inRange:NSMakeRange(0, output.length)
options:0
usingBlock:^(NSParagraphStyle *value, NSRange range, BOOL *stop) {
if (!value) {
return;
}
NSMutableParagraphStyle *mutable = [value mutableCopy];
mutable.lineBreakStrategy = lineBreakStrategy;
[output addAttribute:NSParagraphStyleAttributeName value:mutable range:range];
}];
}
NSTextAlignment textAlignmentFromString(NSString *textAlign)
{
if ([textAlign isEqualToString:@"left"]) {
return NSTextAlignmentLeft;
} else if ([textAlign isEqualToString:@"center"]) {
return NSTextAlignmentCenter;
} else if ([textAlign isEqualToString:@"right"]) {
return NSTextAlignmentRight;
} else if ([textAlign isEqualToString:@"justify"]) {
return NSTextAlignmentJustified;
}
return NSTextAlignmentNatural;
}