Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Link parsing conflicts with autodetection when the link text happens to be recognized as something to link as well #62

Closed
gentwo opened this issue Feb 17, 2017 · 3 comments
Assignees
Labels
Milestone

Comments

@gentwo
Copy link

gentwo commented Feb 17, 2017

Consider the following markdown text:

[foo@bar.com](mailto://foo@bar.com)

In the above case, it appears that auto-detection overrides the original link (which does correctly identify the URL as mailto://foo@bar.com with the text of the link ie (foo@bar.com).

[Mr. Foo](mailto://foo@bar.com)

therefore works.

Not sure how this should be fixed, but one thought is that if a link text already has a link associated with it, then don't override with autodetect link... There may be more complicated use cases like:

[send a mail to foo@bar.com](mailto://foo@bar.com)

ie the link text range and the autodetect link text range may be different. I am going to attempt to fix this, but would like to hear feedback on the above suggestion or other ideas on a solution.

@gentwo
Copy link
Author

gentwo commented Feb 17, 2017

Some additional information. The rationale for my conclusion is as follows: Debugging this: it looks like for the above text, the following sections of TSMarkdownParser.m are both called, with the first one being called first (with the correct URL) and the second one being called later (with the link text).

--> explicit link with link text

    [defaultParser addLinkParsingWithLinkFormattingBlock:^(NSMutableAttributedString *attributedString, NSRange range, NSString * _Nullable link) {
        if (!weakParser.skipLinkAttribute) {
            NSURL *url = [NSURL URLWithString:link] ?: [NSURL URLWithString:
                                                        [link stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
            if (url) {
                [attributedString addAttribute:NSLinkAttributeName
                                         value:url
                                         range:range];
            }
        }
        [attributedString addAttributes:weakParser.linkAttributes range:range];
    }];

--> Auto detection

    /* autodetection */
    
    [defaultParser addLinkDetectionWithLinkFormattingBlock:^(NSMutableAttributedString *attributedString, NSRange range, NSString * _Nullable link) {
        if (!weakParser.skipLinkAttribute) {
            [attributedString addAttribute:NSLinkAttributeName
                                     value:[NSURL URLWithString:link]
                                     range:range];
        }
        [attributedString addAttributes:weakParser.linkAttributes range:range];
    }];

@gentwo
Copy link
Author

gentwo commented Feb 17, 2017

Test to confirm the above:

- (void)testStandardLinkParsingAutodetectionConflict {
  NSAttributedString *attributedString = [self.standardParser attributedStringFromMarkdown:@"Hello\n This is a [foo@bar.com](https://www.example.net/) to test Wi-Fi\nat home"];
  NSURL *link = [attributedString attribute:NSLinkAttributeName atIndex:20 effectiveRange:NULL];
  XCTAssertEqualObjects(link, [NSURL URLWithString:@"https://www.example.net/"]);
  XCTAssertTrue([attributedString.string rangeOfString:@"["].location == NSNotFound);
  XCTAssertTrue([attributedString.string rangeOfString:@"]"].location == NSNotFound);
  XCTAssertTrue([attributedString.string rangeOfString:@"("].location == NSNotFound);
  XCTAssertTrue([attributedString.string rangeOfString:@")"].location == NSNotFound);
  XCTAssertTrue([attributedString.string rangeOfString:@"foo@bar.com"].location != NSNotFound);
  NSNumber *underline = [attributedString attribute:NSUnderlineStyleAttributeName atIndex:20 effectiveRange:NULL];
  XCTAssertEqualObjects(underline, @(NSUnderlineStyleSingle));
  UIColor *linkColor = [attributedString attribute:NSForegroundColorAttributeName atIndex:20 effectiveRange:NULL];
  XCTAssertEqualObjects(linkColor, [UIColor blueColor]);

  NSURL *linkAtTheNextCharacter = [attributedString attribute:NSLinkAttributeName atIndex:21 effectiveRange:NULL]; 
  XCTAssertNil(linkAtTheNextCharacter); // THIS ASSERTION ALSO FAILS. DID NOT EXPECT THIS.
}

NOTE: The last assert also fails, which was unexpected.

gentwo pushed a commit to gentwo/TSMarkdownParser that referenced this issue Feb 17, 2017
# Ensured that autodetect linking does not overlap existing links; which happens in case the link text contains text that could be auto detected as links.
Coeur added a commit that referenced this issue Feb 21, 2017
* gentwo:
  simplification of the enumeration
  # Fix for Issue #62 # Ensured that autodetect linking does not overlap existing links; which happens in case the link text contains text that could be auto detected as links.
@Coeur
Copy link
Collaborator

Coeur commented Feb 21, 2017

I didn't see assertion failures in your pull request.
Thank you for reporting the issue and fixing it. I'll make a new release in a few minutes.

@Coeur Coeur closed this as completed Feb 21, 2017
@Coeur Coeur added this to the 2.1.3 milestone Feb 21, 2017
@Coeur Coeur self-assigned this Feb 21, 2017
@Coeur Coeur added the bug label Feb 21, 2017
Coeur added a commit that referenced this issue Mar 6, 2018
* master:
  fix macOS example
  updating to 2.1.4
  #69: avoiding crash if font is missing
  project clang warning update
  Add Syntax Highlighting
  Add spacing to headers
  updating to 2.1.3
  simplification of the enumeration
  # Fix for Issue #62 # Ensured that autodetect linking does not overlap existing links; which happens in case the link text contains text that could be auto detected as links.
  adding myself as author
  updating to 2.1.2
  add ercentEscapesUsingEncoding

# Conflicts:
#	CHANGELOG.md
#	README.md
#	TSMarkdownParser.podspec
#	TSMarkdownParser.xcodeproj/project.pbxproj
#	TSMarkdownParser/TSMarkdownParser.m
#	TSMarkdownParserExample OSX/ViewController.m
#	TSMarkdownParserTests/TSMarkdownParserTests.m
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants