Skip to content

Commit

Permalink
Fix Wiki inner link
Browse files Browse the repository at this point in the history
  • Loading branch information
xeemoo committed Nov 18, 2018
1 parent 31db42e commit 76894c7
Showing 1 changed file with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ static String generateMdHtml(@NonNull String mdSource, @Nullable String baseUrl,
boolean isDark, @NonNull String backgroundColor,
@NonNull String accentColor, boolean wrapCode) {
String skin = isDark ? "markdown_dark.css" : "markdown_white.css";
mdSource = StringUtils.isBlank(baseUrl) ? mdSource : fixLinks(mdSource, baseUrl);
mdSource = StringUtils.isBlank(baseUrl) ? fixWikiLinks(mdSource) : fixLinks(mdSource, baseUrl);
return generateMdHtml(mdSource, skin, backgroundColor, accentColor, wrapCode);
}

Expand Down Expand Up @@ -137,6 +137,21 @@ private static String formatCode(@NonNull String codeSource) {
return codeSource.replaceAll("<", "&lt;").replaceAll(">", "&gt;");
}

private static String fixWikiLinks(@NonNull String source) {
Matcher linksMatcher = LINK_PATTERN.matcher(source);
while (linksMatcher.find()) {
while (linksMatcher.find()) {
String oriUrl = linksMatcher.group(1);
String fixedUrl;
if (oriUrl.startsWith("/") && oriUrl.contains("/wiki/")) {
fixedUrl = "https://github.com" + oriUrl;
source = source.replace("href=\"" + oriUrl + "\"", "href=\"" + fixedUrl + "\"");
}
}
}
return source;
}

private static String fixLinks(@NonNull String source, @NonNull String baseUrl) {
GitHubName gitHubName = GitHubName.fromUrl(baseUrl);
if (gitHubName == null) return source;
Expand Down

0 comments on commit 76894c7

Please sign in to comment.