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

182 lines
6.9 KiB
Objective-C

#import "ENRMLinkPillIconCache.h"
#import "ENRMImageDownloader.h"
#import "ENRMLocalImageLoader.h"
#if !TARGET_OS_OSX
#import <ImageIO/ImageIO.h>
#import <QuartzCore/QuartzCore.h>
// Icons are drawn at the label's font size, so even a large heading at 3x needs few pixels.
static const CGFloat kMaxIconPixelSize = 128;
static NSCache<NSString *, UIImage *> *ENRMLinkPillIcons(void)
{
static NSCache<NSString *, UIImage *> *icons;
static dispatch_once_t once;
dispatch_once(&once, ^{
icons = [NSCache new];
icons.countLimit = 64;
icons.totalCostLimit = 8 * 1024 * 1024;
});
return icons;
}
// A failed source is left alone for a while, so a broken URL is not requested on every
// render, and then tried again, so a transient failure does not hide the icon for good.
static const CFTimeInterval kFailedIconRetryInterval = 30;
static NSCache<NSString *, NSNumber *> *ENRMFailedLinkPillIcons(void)
{
static NSCache<NSString *, NSNumber *> *failed;
static dispatch_once_t once;
dispatch_once(&once, ^{
failed = [NSCache new];
failed.countLimit = 256;
});
return failed;
}
// A local icon and the state of its file when it was decoded.
@interface ENRMLocalLinkPillIcon : NSObject
@property (nonatomic, strong) UIImage *image;
@property (nonatomic, copy) NSString *path;
@property (nonatomic, strong, nullable) NSDate *modified;
@property (nonatomic, assign) unsigned long long fileSize;
@property (nonatomic, assign) CFTimeInterval checkedAt;
@end
@implementation ENRMLocalLinkPillIcon
@end
// Every render asks for the same icons again, so the file is looked at no more than once
// per interval; a file replaced under the same path is picked up after that.
static const CFTimeInterval kLocalIconRevalidateInterval = 1;
static NSCache<NSString *, ENRMLocalLinkPillIcon *> *ENRMLocalLinkPillIcons(void)
{
static NSCache<NSString *, ENRMLocalLinkPillIcon *> *icons;
static dispatch_once_t once;
dispatch_once(&once, ^{
icons = [NSCache new];
icons.countLimit = 64;
icons.totalCostLimit = 8 * 1024 * 1024;
});
return icons;
}
static UIImage *ENRMDecodeLinkPillIcon(NSString *path)
{
CGImageSourceRef source = CGImageSourceCreateWithURL((__bridge CFURLRef)[NSURL fileURLWithPath:path], NULL);
if (!source)
return nil;
NSDictionary *options = @{
(__bridge NSString *)kCGImageSourceCreateThumbnailFromImageAlways : @YES,
(__bridge NSString *)kCGImageSourceCreateThumbnailWithTransform : @YES,
(__bridge NSString *)kCGImageSourceShouldCacheImmediately : @YES,
(__bridge NSString *)kCGImageSourceThumbnailMaxPixelSize : @(kMaxIconPixelSize),
};
CGImageRef decoded = CGImageSourceCreateThumbnailAtIndex(source, 0, (__bridge CFDictionaryRef)options);
CFRelease(source);
if (!decoded)
return nil;
UIImage *image = [UIImage imageWithCGImage:decoded];
CGImageRelease(decoded);
return image;
}
UIImage *ENRMLoadLinkPillIcon(NSString *iconUri)
{
if (iconUri.length == 0)
return nil;
NSCache<NSString *, ENRMLocalLinkPillIcon *> *icons = ENRMLocalLinkPillIcons();
ENRMLocalLinkPillIcon *cached = [icons objectForKey:iconUri];
CFTimeInterval now = CACurrentMediaTime();
if (cached && now - cached.checkedAt < kLocalIconRevalidateInterval)
return cached.image;
NSString *path = cached.path ?: ENRMResolveLocalImagePath(iconUri);
if (!path)
return nil;
NSDictionary *attributes = [NSFileManager.defaultManager attributesOfItemAtPath:path error:nil];
NSDate *modified = attributes[NSFileModificationDate];
unsigned long long fileSize = [attributes[NSFileSize] unsignedLongLongValue];
if (cached && attributes && cached.fileSize == fileSize && [cached.modified isEqualToDate:modified]) {
cached.checkedAt = now;
return cached.image;
}
UIImage *image = attributes ? ENRMDecodeLinkPillIcon(path) : nil;
if (!image) {
[icons removeObjectForKey:iconUri];
return nil;
}
ENRMLocalLinkPillIcon *entry = [ENRMLocalLinkPillIcon new];
entry.image = image;
entry.path = path;
entry.modified = modified;
entry.fileSize = fileSize;
entry.checkedAt = now;
[icons setObject:entry forKey:iconUri cost:ENRMImageByteCost(image)];
return image;
}
UIImage *ENRMCachedLinkPillIcon(NSString *iconUri, NSDictionary<NSString *, NSString *> *headers)
{
return [ENRMLinkPillIcons() objectForKey:ENRMImageCacheKey(iconUri, headers)];
}
BOOL ENRMLinkPillIconDidFail(NSString *iconUri, NSDictionary<NSString *, NSString *> *headers)
{
NSString *key = ENRMImageCacheKey(iconUri, headers);
NSNumber *failedAt = [ENRMFailedLinkPillIcons() objectForKey:key];
if (!failedAt)
return NO;
if (CACurrentMediaTime() - failedAt.doubleValue < kFailedIconRetryInterval)
return YES;
[ENRMFailedLinkPillIcons() removeObjectForKey:key];
return NO;
}
static UIImage *ENRMLinkPillThumbnail(UIImage *image)
{
CGFloat pixelWidth = image.size.width * image.scale;
CGFloat pixelHeight = image.size.height * image.scale;
CGFloat longest = MAX(pixelWidth, pixelHeight);
if (longest <= kMaxIconPixelSize || longest <= 0)
return image;
CGFloat ratio = kMaxIconPixelSize / longest;
CGSize size = CGSizeMake(MAX(1, floor(pixelWidth * ratio)), MAX(1, floor(pixelHeight * ratio)));
UIGraphicsImageRendererFormat *format = [UIGraphicsImageRendererFormat preferredFormat];
format.scale = 1;
format.opaque = NO;
UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:size format:format];
return [renderer imageWithActions:^(UIGraphicsImageRendererContext *context) {
[image drawInRect:(CGRect){CGPointZero, size}];
}];
}
void ENRMLoadLinkPillIconAsync(NSString *iconUri, NSDictionary<NSString *, NSString *> *headers,
void (^completion)(UIImage *_Nullable icon))
{
NSString *key = ENRMImageCacheKey(iconUri, headers);
[[ENRMImageDownloader shared] downloadURL:iconUri
headers:headers
completion:^(RCTUIImage *image) {
// Drawn pills are cached by icon identity: every pill waiting
// for this icon gets the same image.
UIImage *icon = [ENRMLinkPillIcons() objectForKey:key];
if (!icon && image) {
icon = ENRMLinkPillThumbnail(image);
[ENRMLinkPillIcons() setObject:icon forKey:key cost:ENRMImageByteCost(icon)];
}
if (!icon) {
[ENRMFailedLinkPillIcons() setObject:@(CACurrentMediaTime()) forKey:key];
}
if (NSThread.isMainThread) {
completion(icon);
} else {
dispatch_async(dispatch_get_main_queue(), ^{ completion(icon); });
}
}];
}
#endif