* 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.
250 lines
8.4 KiB
Plaintext
250 lines
8.4 KiB
Plaintext
#import "ENRMAutoLinkDetector.h"
|
|
|
|
#import "ENRMFormattingRange.h"
|
|
#import "InputStylePropsUtils.h"
|
|
|
|
static NSAttributedStringKey const ENRMAutomaticLinkAttributeName = @"ENRMAutomaticLink";
|
|
|
|
@implementation ENRMAutoLinkDetector {
|
|
__weak NSTextStorage *_textStorage;
|
|
__weak ENRMFormattingStore *_formattingStore;
|
|
__weak ENRMInputFormatterStyle *_style;
|
|
ENRMLinkRegexConfig *_regexConfig;
|
|
}
|
|
|
|
- (instancetype)initWithTextStorage:(NSTextStorage *)textStorage
|
|
formattingStore:(ENRMFormattingStore *)store
|
|
style:(ENRMInputFormatterStyle *)style
|
|
{
|
|
self = [super init];
|
|
if (self) {
|
|
_textStorage = textStorage;
|
|
_formattingStore = store;
|
|
_style = style;
|
|
}
|
|
return self;
|
|
}
|
|
|
|
- (void)setRegexConfig:(ENRMLinkRegexConfig *)config
|
|
{
|
|
_regexConfig = config;
|
|
}
|
|
|
|
#pragma mark - ENRMTextDetector
|
|
|
|
- (void)processWord:(ENRMWordResult *)wordResult
|
|
{
|
|
NSRange matchedRange = NSMakeRange(NSNotFound, 0);
|
|
NSString *detectedUrl = [self detectNewLinkAtRange:wordResult.range
|
|
inText:wordResult.word
|
|
matchedRange:&matchedRange];
|
|
if (detectedUrl != nil && _onLinkDetected != nil) {
|
|
// Emit the actual linked slice, not the whole whitespace token — surrounding
|
|
// punctuation is not part of the link.
|
|
BOOL hasSlice = matchedRange.location != NSNotFound;
|
|
NSString *matchedText = hasSlice ? [wordResult.word substringWithRange:matchedRange] : wordResult.word;
|
|
NSRange absoluteRange = hasSlice
|
|
? NSMakeRange(wordResult.range.location + matchedRange.location, matchedRange.length)
|
|
: wordResult.range;
|
|
_onLinkDetected(matchedText, detectedUrl, absoluteRange);
|
|
}
|
|
}
|
|
|
|
- (void)refreshStyling
|
|
{
|
|
NSTextStorage *textStorage = _textStorage;
|
|
if (textStorage == nil || textStorage.length == 0) {
|
|
return;
|
|
}
|
|
|
|
[textStorage beginEditing];
|
|
[textStorage enumerateAttribute:ENRMAutomaticLinkAttributeName
|
|
inRange:NSMakeRange(0, textStorage.length)
|
|
options:0
|
|
usingBlock:^(id value, NSRange range, BOOL *stop) {
|
|
if (value != nil) {
|
|
[self applyVisualStylingToRange:range];
|
|
}
|
|
}];
|
|
[textStorage endEditing];
|
|
}
|
|
|
|
- (NSArray<ENRMFormattingRange *> *)transientFormattingRanges
|
|
{
|
|
NSTextStorage *textStorage = _textStorage;
|
|
if (textStorage == nil || textStorage.length == 0) {
|
|
return @[];
|
|
}
|
|
|
|
NSMutableArray<ENRMFormattingRange *> *ranges = [NSMutableArray array];
|
|
[textStorage enumerateAttribute:ENRMAutomaticLinkAttributeName
|
|
inRange:NSMakeRange(0, textStorage.length)
|
|
options:0
|
|
usingBlock:^(id value, NSRange range, BOOL *stop) {
|
|
if (value != nil && [value isKindOfClass:[NSString class]]) {
|
|
ENRMFormattingRange *fmtRange = [ENRMFormattingRange rangeWithType:ENRMInputStyleTypeLink
|
|
range:range
|
|
url:value];
|
|
[ranges addObject:fmtRange];
|
|
}
|
|
}];
|
|
return ranges;
|
|
}
|
|
|
|
- (void)clearAutoLinkInRange:(NSRange)range
|
|
{
|
|
NSTextStorage *textStorage = _textStorage;
|
|
if (textStorage == nil || NSMaxRange(range) > textStorage.length) {
|
|
return;
|
|
}
|
|
[textStorage removeAttribute:ENRMAutomaticLinkAttributeName range:range];
|
|
}
|
|
|
|
#pragma mark - Link matching
|
|
|
|
- (nullable NSString *)detectNewLinkAtRange:(NSRange)wordRange
|
|
inText:(NSString *)word
|
|
matchedRange:(NSRange *)outMatchedRange
|
|
{
|
|
if (outMatchedRange != NULL) {
|
|
*outMatchedRange = NSMakeRange(NSNotFound, 0);
|
|
}
|
|
|
|
if (_regexConfig != nil && _regexConfig.isDisabled) {
|
|
return nil;
|
|
}
|
|
|
|
NSTextStorage *textStorage = _textStorage;
|
|
if (textStorage == nil) {
|
|
return nil;
|
|
}
|
|
|
|
if (NSMaxRange(wordRange) > textStorage.length) {
|
|
return nil;
|
|
}
|
|
|
|
// Tokens carrying markdown link syntax (e.g. "[label](url)") belong to the
|
|
// parser, not the auto-linker. Auto-linking them re-wraps the destination and
|
|
// yields a corrupted URL like "https://[label](url)" on serialize.
|
|
if ([word hasPrefix:@"["] || [word rangeOfString:@"]("].location != NSNotFound) {
|
|
[self clearAutoLinkInRange:wordRange];
|
|
return nil;
|
|
}
|
|
|
|
NSRange localMatch = NSMakeRange(NSNotFound, 0);
|
|
NSString *matchedUrl = [self tryMatchWord:word matchedRange:&localMatch];
|
|
|
|
if (matchedUrl == nil || localMatch.location == NSNotFound) {
|
|
[self clearAutoLinkInRange:wordRange];
|
|
return nil;
|
|
}
|
|
|
|
NSRange matchRange = NSMakeRange(wordRange.location + localMatch.location, localMatch.length);
|
|
|
|
// A manual link overlapping the matched slice takes precedence — test the
|
|
// whole slice, not just its first character.
|
|
ENRMFormattingStore *store = _formattingStore;
|
|
if (store != nil && [store isStyleActive:ENRMInputStyleTypeLink inRange:matchRange]) {
|
|
[self clearAutoLinkInRange:wordRange];
|
|
return nil;
|
|
}
|
|
|
|
NSRange effectiveRange;
|
|
id existing = [textStorage attribute:ENRMAutomaticLinkAttributeName
|
|
atIndex:matchRange.location
|
|
effectiveRange:&effectiveRange];
|
|
BOOL alreadyApplied = [matchedUrl isEqual:existing] && NSEqualRanges(effectiveRange, matchRange);
|
|
|
|
if (!alreadyApplied) {
|
|
[textStorage beginEditing];
|
|
[textStorage removeAttribute:ENRMAutomaticLinkAttributeName range:wordRange];
|
|
[textStorage addAttribute:ENRMAutomaticLinkAttributeName value:matchedUrl range:matchRange];
|
|
[self applyVisualStylingToRange:matchRange];
|
|
[textStorage endEditing];
|
|
}
|
|
|
|
if (outMatchedRange != NULL) {
|
|
*outMatchedRange = localMatch;
|
|
}
|
|
return matchedUrl;
|
|
}
|
|
|
|
- (nullable NSString *)tryMatchWord:(NSString *)word matchedRange:(NSRange *)outRange
|
|
{
|
|
if (outRange != NULL) {
|
|
*outRange = NSMakeRange(NSNotFound, 0);
|
|
}
|
|
|
|
if (word.length == 0) {
|
|
return nil;
|
|
}
|
|
|
|
NSRange fullRange = NSMakeRange(0, word.length);
|
|
|
|
// A custom regex describes the whole-token contract, so keep whole-word
|
|
// semantics for it. The default detector extracts just the matched URL slice
|
|
// so surrounding punctuation isn't swallowed into the link.
|
|
BOOL customExact = _regexConfig != nil && !_regexConfig.isDefault && _regexConfig.parsedRegex != nil;
|
|
NSRegularExpression *regex = customExact ? _regexConfig.parsedRegex : [ENRMAutoLinkDetector defaultRegex];
|
|
if (regex == nil) {
|
|
return nil;
|
|
}
|
|
|
|
NSTextCheckingResult *match = [regex firstMatchInString:word options:0 range:fullRange];
|
|
if (match == nil || match.range.location == NSNotFound) {
|
|
return nil;
|
|
}
|
|
|
|
NSRange matchRange = customExact ? fullRange : match.range;
|
|
if (outRange != NULL) {
|
|
*outRange = matchRange;
|
|
}
|
|
return [ENRMAutoLinkDetector normalizeUrl:[word substringWithRange:matchRange]];
|
|
}
|
|
|
|
+ (NSString *)normalizeUrl:(NSString *)url
|
|
{
|
|
if ([url hasPrefix:@"http://"] || [url hasPrefix:@"https://"]) {
|
|
return url;
|
|
}
|
|
return [@"https://" stringByAppendingString:url];
|
|
}
|
|
|
|
#pragma mark - Visual styling
|
|
|
|
- (void)applyVisualStylingToRange:(NSRange)range
|
|
{
|
|
NSTextStorage *textStorage = _textStorage;
|
|
ENRMInputFormatterStyle *style = _style;
|
|
if (textStorage == nil || style == nil) {
|
|
return;
|
|
}
|
|
|
|
[textStorage addAttribute:NSForegroundColorAttributeName value:style.linkColor range:range];
|
|
if (style.linkUnderline) {
|
|
[textStorage addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:range];
|
|
}
|
|
}
|
|
|
|
#pragma mark - Default regex
|
|
|
|
+ (NSRegularExpression *)defaultRegex
|
|
{
|
|
static NSRegularExpression *regex;
|
|
static dispatch_once_t onceToken;
|
|
dispatch_once(&onceToken, ^{
|
|
NSError *error = nil;
|
|
regex = [NSRegularExpression
|
|
regularExpressionWithPattern:
|
|
@"(?:https?://[-a-zA-Z0-9@:%._\\+~#=]{1,256}\\.[a-z]{2,6}\\b[-a-zA-Z0-9@:%_\\+.~#?&//=]*"
|
|
@"|www\\.[-a-zA-Z0-9@:%._\\+~#=]{1,256}\\.[a-z]{2,6}\\b[-a-zA-Z0-9@:%_\\+.~#?&//=]*"
|
|
@"|[-a-zA-Z0-9@:%._\\+~#=]{1,256}\\.[a-z]{2,6}\\b[-a-zA-Z0-9@:%_\\+.~#?&//=]*)"
|
|
options:NSRegularExpressionCaseInsensitive
|
|
error:&error];
|
|
NSCAssert(regex != nil, @"ENRMAutoLinkDetector: failed to compile default regex: %@", error);
|
|
});
|
|
return regex;
|
|
}
|
|
|
|
@end
|