yydcdut / RxMarkdown

:fax:Markdown for Android, supports TextView && EditText (Live Preview), supports code high light.
749 stars 93 forks source link

Link detection faulty #65

Open lenzls opened 4 years ago

lenzls commented 4 years ago

Hello,

while trying to hook into hyperlink processing (to make relative links work), I realized that hyperlink parsing in general seems to be broken.

problem

Suppose we have a markdown like

 1. [link!](https://www.example.com)
 2. [link!](example.com)
 3. [link!](56366)

All three should be valid and interpreted as links. But rxmarkdown only interprets (1) as a link. The others are ignored and rendered as [link!](example.com) and [link!](56366) in the output.

presumed cause

com.yydcdut.markdown.syntax.text.HyperLinkSyntax#contains seems to be broken. It return in all three cases false. While it should actually return true in all three cases.

As a result, you only check in line 60 the AUTO_LINK_PATTERN regex to see if you can do auto linking in this line. And for case (1) this actually works. That's why line (1) is interpreted correctly as a link but the other two not.

The problem lies in line 106 and following. Here

                    if (TextHelper.getChar(array, ++i) == 0 || TextHelper.getChar(findArray, ++findPosition) == 0) {
                        return false;
                    }
                    if (TextHelper.getChar(array, ++i) != TextHelper.getChar(findArray, ++findPosition)) {
                        findPosition--;
                    } else {
                        findPosition++;
                    }

you increment i and findPosition in the method calls which results in incorrect behaviour. I guess replacing eg. ++i with i+1 (and the same for findPosition would solve the problem.

On the other hand, I'm not entirely sure, why you need this method at all. Isn't matching with the PATTERN regex from the same file enough to detect whether the string contains a markdown link? It looks like the method tries to do the same as matching the regex programmatically (but does it faulty).

regards,

Simon