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
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#import "ListItemRenderer.h"
#import "MarkdownExtractor.h"
#import <XCTest/XCTest.h>

@interface ENRMMarkdownExtractorTests : XCTestCase
@end

@implementation ENRMMarkdownExtractorTests

- (UIFont *)enrmExtractorBoldFont
{
return [UIFont boldSystemFontOfSize:16];
}

- (void)testBoldSplitAcrossAttributeRunsCopiesAsOneSpan
{
NSMutableAttributedString *text =
[[NSMutableAttributedString alloc] initWithString:@"What"
attributes:@{NSFontAttributeName : [self enrmExtractorBoldFont]}];
// Any attribute without inline meaning splits the run after the first character.
[text addAttribute:@"ENRMTestUnrelatedAttribute" value:@YES range:NSMakeRange(0, 1)];

XCTAssertEqualObjects(extractMarkdownFromAttributedString(text, NSMakeRange(0, text.length)), @"**What**");
}

- (void)testBoldAtListItemStartCopiesAsOneSpan
{
NSDictionary *listAttrs = @{
ListDepthAttribute : @0,
ListTypeAttribute : @0,
ListItemNumberAttribute : @1,
};
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:@"What: next"
attributes:listAttrs];
[text addAttribute:NSFontAttributeName value:[self enrmExtractorBoldFont] range:NSMakeRange(0, 4)];
[text addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:NSMakeRange(4, 6)];
// ListItemRenderer anchors the bullet marker on the item's first grapheme.
[text addAttribute:ListItemMarkerStartAttribute value:@[] range:NSMakeRange(0, 1)];

XCTAssertEqualObjects(extractMarkdownFromAttributedString(text, NSMakeRange(0, text.length)), @"- **What**: next");
}

- (void)testAdjacentRunsWithDifferentFormattingStayWrappedSeparately
{
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:@"ab"];
[text addAttribute:NSFontAttributeName value:[self enrmExtractorBoldFont] range:NSMakeRange(0, 1)];
[text addAttribute:NSFontAttributeName value:[UIFont italicSystemFontOfSize:16] range:NSMakeRange(1, 1)];

XCTAssertEqualObjects(extractMarkdownFromAttributedString(text, NSMakeRange(0, text.length)), @"**a***b*");
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -83,42 +83,55 @@ static void extractFontTraits(NSDictionary *attrs, BOOL *isBold, BOOL *isItalic,
}
}

static NSString *applyInlineFormatting(NSString *text, BOOL isBold, BOOL isItalic, BOOL isMonospace,
BOOL isStrikethrough, BOOL isUnderline, BOOL isSuperscript, BOOL isSubscript,
BOOL isHighlight, NSString *linkURL)
/// Builds the opening and closing Markdown delimiters for one run's inline formatting.
/// The run's text goes between them. Both are empty when the run has no inline formatting.
static void buildInlineDelimiters(BOOL isBold, BOOL isItalic, BOOL isMonospace, BOOL isStrikethrough, BOOL isUnderline,
BOOL isSuperscript, BOOL isSubscript, BOOL isHighlight, NSString *linkURL,
NSString **outOpen, NSString **outClose)
{
NSMutableString *result = [NSMutableString stringWithString:text];
NSMutableString *open = [NSMutableString string];
NSMutableString *close = [NSMutableString string];

// Innermost first
if (isMonospace && !linkURL) {
result = [NSMutableString stringWithFormat:@"`%@`", result];
[open insertString:@"`" atIndex:0];
[close appendString:@"`"];
}
if (isStrikethrough) {
result = [NSMutableString stringWithFormat:@"~~%@~~", result];
[open insertString:@"~~" atIndex:0];
[close appendString:@"~~"];
}
if (isSubscript) {
result = [NSMutableString stringWithFormat:@"~%@~", result];
[open insertString:@"~" atIndex:0];
[close appendString:@"~"];
}
if (isSuperscript) {
result = [NSMutableString stringWithFormat:@"^%@^", result];
[open insertString:@"^" atIndex:0];
[close appendString:@"^"];
}
if (isUnderline && !linkURL) {
result = [NSMutableString stringWithFormat:@"<u>%@</u>", result];
[open insertString:@"<u>" atIndex:0];
[close appendString:@"</u>"];
}
if (isItalic) {
result = [NSMutableString stringWithFormat:@"*%@*", result];
[open insertString:@"*" atIndex:0];
[close appendString:@"*"];
}
if (isBold) {
result = [NSMutableString stringWithFormat:@"**%@**", result];
[open insertString:@"**" atIndex:0];
[close appendString:@"**"];
}
if (linkURL) {
result = [NSMutableString stringWithFormat:@"[%@](%@)", result, linkURL];
[open insertString:@"[" atIndex:0];
[close appendFormat:@"](%@)", linkURL];
}
if (isHighlight) {
result = [NSMutableString stringWithFormat:@"==%@==", result];
[open insertString:@"==" atIndex:0];
[close appendString:@"=="];
}

return result;
*outOpen = open;
*outClose = close;
}

#pragma mark - Main Extraction Function
Expand All @@ -139,6 +152,14 @@ static void extractFontTraits(NSDictionary *attrs, BOOL *isBold, BOOL *isItalic,
__block NSMutableString *headingContent = nil;
__block ExtractionState state = {.blockquoteDepth = -1, .listDepth = -1, .needsBlankLine = NO};

// The last formatted inline segment appended to `result`, so the next run can extend it.
// Attribute runs split on attributes that carry no inline formatting (e.g. the list-marker
// anchor on an item's first character), and wrapping each run on its own would turn
// **What** into **W****hat**.
__block NSString *lastInlineOpen = nil;
__block NSString *lastInlineClose = nil;
__block NSUInteger lastInlineEnd = NSNotFound;

void (^flushHeading)(void) = ^{
if (!currentHeadingType || headingContent.length == 0)
return;
Expand Down Expand Up @@ -318,9 +339,26 @@ static void extractFontTraits(NSDictionary *attrs, BOOL *isBold, BOOL *isItalic,

BOOL isHighlight = [attrs[HighlightAttributeName] boolValue];
NSString *linkURL = recognizedLink ? nil : attrs[NSLinkAttributeName];
NSString *segment =
applyInlineFormatting(text, isBold, isItalic, isMonospace, isStrikethrough, isUnderline,
isSuperscript, isSubscript, isHighlight, linkURL);
NSString *inlineOpen = nil;
NSString *inlineClose = nil;
buildInlineDelimiters(isBold, isItalic, isMonospace, isStrikethrough, isUnderline,
isSuperscript, isSubscript, isHighlight, linkURL, &inlineOpen,
&inlineClose);

// Same formatting as the segment that still ends `result`: move its closing
// delimiters past this run's text instead of opening a new span.
if (inlineClose.length > 0 && result.length == lastInlineEnd && !state.needsBlankLine &&
[inlineOpen isEqualToString:lastInlineOpen] &&
[inlineClose isEqualToString:lastInlineClose]) {
[result deleteCharactersInRange:NSMakeRange(result.length - inlineClose.length,
inlineClose.length)];
[result appendString:text];
[result appendString:inlineClose];
lastInlineEnd = result.length;
return;
}

NSString *segment = [NSString stringWithFormat:@"%@%@%@", inlineOpen, text, inlineClose];

// Add block prefixes at line start
if (isAtLineStart(result)) {
Expand Down Expand Up @@ -354,6 +392,9 @@ static void extractFontTraits(NSDictionary *attrs, BOOL *isBold, BOOL *isItalic,
}

[result appendString:segment];
lastInlineOpen = inlineOpen;
lastInlineClose = inlineClose;
lastInlineEnd = result.length;
}];

// Flush remaining heading
Expand Down