Files
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

259 lines
11 KiB
Objective-C

#import "BlockquoteRenderer.h"
#import "BlockquoteBorder.h"
#import "FontUtils.h"
#import "LastElementUtils.h"
#import "ListItemRenderer.h"
#import "MarkdownASTNode.h"
#import "ParagraphStyleUtils.h"
#import "RenderContext.h"
#import "RendererFactory.h"
#import "StyleConfig.h"
static NSString *const kNestedInfoDepthKey = @"depth";
static NSString *const kNestedInfoRangeKey = @"range";
@implementation BlockquoteRenderer
- (id)takeContextSnapshot:(RenderContext *)context
{
return [context snapshotScope];
}
- (void)renderNodeContent:(MarkdownASTNode *)node
into:(NSMutableAttributedString *)output
context:(RenderContext *)context
{
if (output.length > 0 && ![output.string hasSuffix:@"\n"]) {
[output appendAttributedString:kNewlineAttributedString];
}
NSInteger currentDepth = context.blockquoteDepth;
context.blockquoteDepth = currentDepth + 1;
[context setBlockStyle:BlockTypeBlockquote
font:[_config blockquoteFont]
color:[_config blockquoteColor]
headingLevel:0];
NSUInteger start = output.length;
[_rendererFactory renderChildrenOfNode:node into:output context:context];
NSUInteger end = output.length;
if (end <= start) {
return;
}
[self applyStylingAndSpacing:output start:start end:end currentDepth:currentDepth context:context];
}
#pragma mark - Styling and Spacing
- (void)applyStylingAndSpacing:(NSMutableAttributedString *)output
start:(NSUInteger)start
end:(NSUInteger)end
currentDepth:(NSInteger)currentDepth
context:(RenderContext *)context
{
NSUInteger contentStart = start;
if (currentDepth == 0) {
contentStart += applyBlockSpacingBefore(output, start, [_config blockquoteMarginTop]);
}
// nested quotes pad their own box (matching web CSS padding)
CGFloat padding = [_config blockquotePadding];
NSUInteger topPadLength = 0;
NSUInteger bottomPadLength = 0;
if (padding > 0) {
NSMutableParagraphStyle *topSpacerStyle = [context spacerStyleWithHeight:padding spacing:0];
NSAttributedString *topSpacer = [[NSAttributedString alloc]
initWithString:@"\n"
attributes:@{NSParagraphStyleAttributeName : topSpacerStyle, BlockquoteSpacerAttributeName : @YES}];
[output insertAttributedString:topSpacer atIndex:contentStart];
topPadLength = 1;
NSUInteger bottomSpacerLocation = output.length;
[output appendAttributedString:kNewlineAttributedString];
NSMutableParagraphStyle *bottomSpacerStyle = [context spacerStyleWithHeight:padding spacing:0];
[output addAttributes:@{NSParagraphStyleAttributeName : bottomSpacerStyle, BlockquoteSpacerAttributeName : @YES}
range:NSMakeRange(bottomSpacerLocation, 1)];
bottomPadLength = 1;
}
NSRange blockquoteRange = NSMakeRange(contentStart, (end - start) + topPadLength + bottomPadLength);
NSRange innerRange = NSMakeRange(contentStart + topPadLength, end - start);
CGFloat levelSpacing = [_config blockquoteBorderWidth] + [_config blockquoteGapWidth];
NSArray<NSDictionary *> *nestedInfo = [self collectNestedBlockquotes:output range:innerRange depth:currentDepth];
// Apply base styling (indentation, depth, background, line height)
[self applyBaseBlockquoteStyle:output
boxRange:blockquoteRange
innerRange:innerRange
depth:currentDepth
levelSpacing:levelSpacing
backgroundColor:[_config blockquoteBackgroundColor]
lineHeight:[_config blockquoteLineHeight]];
// Re-apply nested blockquote styles to restore their correct indentation
// (applyBaseBlockquoteStyle overwrites nested indents with the parent's indent)
[self reapplyNestedStyles:output nestedInfo:nestedInfo levelSpacing:levelSpacing];
if (currentDepth == 0) {
applyBlockSpacingAfter(output, [_config blockquoteMarginBottom]);
}
}
#pragma mark - Nested Blockquote Handling
- (NSArray<NSDictionary *> *)collectNestedBlockquotes:(NSMutableAttributedString *)output
range:(NSRange)blockquoteRange
depth:(NSInteger)currentDepth
{
NSMutableArray<NSDictionary *> *nestedInfo = [NSMutableArray array];
[output
enumerateAttribute:BlockquoteDepthAttributeName
inRange:blockquoteRange
options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired
usingBlock:^(id value, NSRange range, BOOL *stop) {
NSInteger depth = [value integerValue];
if (value && depth > currentDepth) {
[nestedInfo
addObject:@{kNestedInfoDepthKey : value, kNestedInfoRangeKey : [NSValue valueWithRange:range]}];
}
}];
return nestedInfo;
}
- (void)applyBaseBlockquoteStyle:(NSMutableAttributedString *)output
boxRange:(NSRange)boxRange
innerRange:(NSRange)innerRange
depth:(NSInteger)currentDepth
levelSpacing:(CGFloat)levelSpacing
backgroundColor:(RCTUIColor *)backgroundColor
lineHeight:(CGFloat)lineHeight
{
CGFloat totalIndent = blockquoteIndentForDepth(currentDepth + 1, levelSpacing);
CGFloat padding = [_config blockquotePadding];
// Depth and background cover the whole box (incl. padding spacers) so the
// border and background render behind list content and the padding rows.
NSMutableDictionary *depthAttributes =
[NSMutableDictionary dictionaryWithObjectsAndKeys:@(currentDepth), BlockquoteDepthAttributeName, nil];
if (backgroundColor) {
depthAttributes[BlockquoteBackgroundColorAttributeName] = backgroundColor;
}
[output addAttributes:depthAttributes range:boxRange];
// List items bake the blockquote indent into their own paragraph style; only non-list content is stamped here.
[self enumerateNonListRangesIn:output
range:innerRange
usingBlock:^(NSRange nonListRange) {
NSMutableParagraphStyle *paragraphStyle =
getOrCreateParagraphStyle(output, nonListRange.location);
paragraphStyle.firstLineHeadIndent = totalIndent;
paragraphStyle.headIndent = totalIndent;
if (padding > 0) {
paragraphStyle.tailIndent = -padding;
}
[output addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:nonListRange];
applyLineHeight(output, nonListRange, lineHeight);
applyBaselineOffset(output, nonListRange);
}];
if (padding > 0) {
[self applyTailIndent:-padding toListRangesIn:output range:innerRange];
}
}
- (void)applyTailIndent:(CGFloat)tailIndent toListRangesIn:(NSMutableAttributedString *)output range:(NSRange)range
{
[output enumerateAttribute:ListDepthAttribute
inRange:range
options:0
usingBlock:^(id value, NSRange listRange, BOOL *stop) {
if (!value) {
return;
}
[output enumerateAttribute:NSParagraphStyleAttributeName
inRange:listRange
options:0
usingBlock:^(NSParagraphStyle *style, NSRange styleRange, BOOL *innerStop) {
NSMutableParagraphStyle *mutableStyle =
style ? [style mutableCopy] : [[NSMutableParagraphStyle alloc] init];
mutableStyle.tailIndent = tailIndent;
[output addAttribute:NSParagraphStyleAttributeName
value:mutableStyle
range:styleRange];
}];
}];
}
- (void)reapplyNestedStyles:(NSMutableAttributedString *)output
nestedInfo:(NSArray<NSDictionary *> *)nestedInfo
levelSpacing:(CGFloat)levelSpacing
{
// Re-apply indentation to nested blockquotes since applyBaseBlockquoteStyle
// overwrote them with the parent's indentation
CGFloat padding = [_config blockquotePadding];
for (NSDictionary *info in nestedInfo) {
NSRange nestedRange = [info[kNestedInfoRangeKey] rangeValue];
NSInteger nestedDepth = [info[kNestedInfoDepthKey] integerValue];
CGFloat indent = blockquoteIndentForDepth(nestedDepth + 1, levelSpacing);
[output addAttribute:BlockquoteDepthAttributeName value:info[kNestedInfoDepthKey] range:nestedRange];
[self enumerateNonListRangesIn:output
range:nestedRange
usingBlock:^(NSRange nonListRange) {
NSMutableParagraphStyle *style = getOrCreateParagraphStyle(output, nonListRange.location);
style.firstLineHeadIndent = indent;
style.headIndent = indent;
style.tailIndent = padding > 0 ? -padding : 0;
[output addAttribute:NSParagraphStyleAttributeName value:style range:nonListRange];
}];
}
}
- (void)enumerateNonListRangesIn:(NSMutableAttributedString *)output
range:(NSRange)range
usingBlock:(void (^)(NSRange nonListRange))block
{
// List items — and code blocks nested inside them (which carry a list-derived
// indent instead of ListDepthAttribute) — already bake the correct indent in.
NSMutableArray<NSValue *> *listRanges = [NSMutableArray array];
void (^collectRanges)(NSString *) = ^(NSString *attribute) {
[output enumerateAttribute:attribute
inRange:range
options:0
usingBlock:^(id value, NSRange subRange, BOOL *stop) {
if (value) {
[listRanges addObject:[NSValue valueWithRange:subRange]];
}
}];
};
collectRanges(ListDepthAttribute);
collectRanges(CodeBlockIndentAttributeName);
collectRanges(BlockquoteSpacerAttributeName);
[listRanges sortUsingComparator:^NSComparisonResult(NSValue *a, NSValue *b) {
NSUInteger la = [a rangeValue].location;
NSUInteger lb = [b rangeValue].location;
return la < lb ? NSOrderedAscending : (la > lb ? NSOrderedDescending : NSOrderedSame);
}];
NSUInteger pos = range.location;
const NSUInteger end = NSMaxRange(range);
for (NSValue *val in listRanges) {
NSRange listRange = [val rangeValue];
if (pos < listRange.location) {
block(NSMakeRange(pos, listRange.location - pos));
}
pos = MAX(pos, NSMaxRange(listRange));
}
if (pos < end) {
block(NSMakeRange(pos, end - pos));
}
}
@end