Skip to content

Commit

Permalink
markdown: Fix auto-link bug related to escaped html
Browse files Browse the repository at this point in the history
Yona use 'jsoup' for parsing posting contents.
But some already escaped html contents are parsed wrong.
jsoup's node.text() method do unescape strings.
So, change to use node.toString() instead of node.text().

And Upgrade jsoup to 1.8.3 from 1.8.2
  • Loading branch information
doortts committed Mar 13, 2016
1 parent d51263f commit dab9ac9
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 48 deletions.
97 changes: 50 additions & 47 deletions app/utils/AutoLinkRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,55 +125,58 @@ public AutoLinkRenderer(String body, Project project) {
}

public String render() {
return this
.parse(PATH_WITH_ISSUE_PATTERN, new ToLink() {
@Override
public Link toLink(Matcher matcher) {
String path = matcher.group(1);
String issueNumber = matcher.group(2);

Project project = getProjectFromPath(path);
return toValidIssueLink(path, project, issueNumber);
}
})
.parse(ISSUE_PATTERN, new ToLink() {
@Override
public Link toLink(Matcher matcher) {
return toValidIssueLink(project, matcher.group(1));
}
})
.parse(PATH_WITH_SHA_PATTERN, new ToLink() {
@Override
public Link toLink(Matcher matcher) {
String path = matcher.group(1);
String SHA = matcher.group(2);

Project project = getProjectFromPath(path);
return toValidSHALink(path, project, SHA);
}
})
.parse(SHA_PATTERN, new ToLink() {
@Override
public Link toLink(Matcher matcher) {
return toValidSHALink(project, matcher.group(1));
}
})
.parse(LOGIN_ID_PATTERN_ALLOW_FORWARD_SLASH_PATTERN, new ToLink() {
@Override
public Link toLink(Matcher matcher) {
String path = matcher.group(1);
this.parse(PATH_WITH_ISSUE_PATTERN, new ToLink() {
@Override
public Link toLink(Matcher matcher) {
String path = matcher.group(1);
String issueNumber = matcher.group(2);

Project project = getProjectFromPath(path);
return toValidIssueLink(path, project, issueNumber);
}
});

int slashIndex = path.indexOf("/");
parse(ISSUE_PATTERN, new ToLink() {
@Override
public Link toLink(Matcher matcher) {
return toValidIssueLink(project, matcher.group(1));
}
});

if (slashIndex > -1) {
return toValidProjectLink(path.substring(0, slashIndex), path.substring(slashIndex + 1));
} else {
return toValidUserLink(path);
}
}
})
parse(PATH_WITH_SHA_PATTERN, new ToLink() {
@Override
public Link toLink(Matcher matcher) {
String path = matcher.group(1);
String SHA = matcher.group(2);

Project project = getProjectFromPath(path);
return toValidSHALink(path, project, SHA);
}
});

parse(SHA_PATTERN, new ToLink() {
@Override
public Link toLink(Matcher matcher) {
return toValidSHALink(project, matcher.group(1));
}
});

parse(LOGIN_ID_PATTERN_ALLOW_FORWARD_SLASH_PATTERN, new ToLink() {
@Override
public Link toLink(Matcher matcher) {
String path = matcher.group(1);

int slashIndex = path.indexOf("/");

if (slashIndex > -1) {
return toValidProjectLink(path.substring(0, slashIndex), path.substring(slashIndex + 1));
} else {
return toValidUserLink(path);
}
}
});

.body;
return this.body;
}

private AutoLinkRenderer parse(Pattern pattern, ToLink toLink) {
Expand All @@ -192,7 +195,7 @@ private AutoLinkRenderer parse(Pattern pattern, ToLink toLink) {
List<TextNode> textNodeList = el.textNodes();

for (TextNode node : textNodeList) {
String result = convertLink(node.text(), pattern, toLink);
String result = convertLink(node.toString(), pattern, toLink);
node.text(StringUtils.EMPTY);
node.after(result);
}
Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ libraryDependencies ++= Seq(
"commons-io" % "commons-io" % "2.4",
"org.julienrf" %% "play-jsmessages" % "1.6.2",
"commons-collections" % "commons-collections" % "3.2.1",
"org.jsoup" % "jsoup" % "1.8.2",
"org.jsoup" % "jsoup" % "1.8.3",
"com.googlecode.juniversalchardet" % "juniversalchardet" % "1.0.3",
"org.mockito" % "mockito-all" % "1.9.0" % "test",
"com.github.zafarkhaja" % "java-semver" % "0.7.2",
Expand Down

0 comments on commit dab9ac9

Please sign in to comment.