matcher.matches();
String content = matcher.group(1);
String url = matcher.group(2);
+ boolean isRelativeUrl = !url.contains("://");
output.append("<a href=\"");
output.append(url);
- output.append("\" target=\"_blank\" rel=\"noopener noreferrer\">");
+ output.append(isRelativeUrl ? "\">" : "\" target=\"_blank\" rel=\"noopener noreferrer\">");
output.append(content);
output.append("</a>");
}
@Override
protected void consume(CharSequence token, MarkdownOutput output) {
+ String url = token.toString();
+ boolean isRelativeUrl = !url.contains("://");
output.append("<a href=\"");
- output.append(token);
- output.append("\" target=\"_blank\" rel=\"noopener noreferrer\">");
- output.append(token);
+ output.append(url);
+ output.append(isRelativeUrl ? "\">" : "\" target=\"_blank\" rel=\"noopener noreferrer\">");
+ output.append(url);
output.append("</a>");
}
}
public class MarkdownTest {
@Test
- public void shouldDecorateUrl() {
+ public void shouldDecorateAbsoluteUrl() {
assertThat(Markdown.convertToHtml("http://google.com"))
.isEqualTo("<a href=\"http://google.com\" target=\"_blank\" rel=\"noopener noreferrer\">http://google.com</a>");
}
+ @Test
+ public void shouldDecorateRelativeUrl() {
+ assertThat(Markdown.convertToHtml("[Google](/google/com)"))
+ .isEqualTo("<a href=\"/google/com\">Google</a>");
+ }
+
@Test
public void shouldDecorateDocumentedLink() {
assertThat(Markdown.convertToHtml("For more details, please [check online documentation](http://docs.sonarqube.org/display/SONAR)."))