Skip to content
This repository was archived by the owner on May 4, 2023. It is now read-only.

Commit 73201e4

Browse files
committed
Add links to non markdown formatted https urls in MarkdownActivity.
1 parent 556b427 commit 73201e4

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

app/src/main/java/com/fox2code/mmm/markdown/MarkdownActivity.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,12 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
5959
TextView textView = findViewById(R.id.markdownView);
6060
new Thread(() -> {
6161
try {
62+
Log.d(TAG, "Downloading");
6263
String markdown = new String(Http.doHttpGet(url, true), StandardCharsets.UTF_8);
63-
Log.i(TAG, "Download successful");
64+
Log.d(TAG, "Done!");
6465
runOnUiThread(() -> {
65-
MainApplication.getINSTANCE().getMarkwon().setMarkdown(textView, markdown);
66+
MainApplication.getINSTANCE().getMarkwon().setMarkdown(
67+
textView, MarkdownUrlLinker.urlLinkify(markdown));
6668
if (markdownBackground != null) {
6769
markdownBackground.setClickable(true);
6870
markdownBackground.setOnClickListener(v -> this.onBackPressed());
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.fox2code.mmm.markdown;
2+
3+
import android.util.Log;
4+
5+
import com.fox2code.mmm.BuildConfig;
6+
7+
import java.util.ArrayList;
8+
9+
public class MarkdownUrlLinker {
10+
private static final String TAG = "MarkdownUrlLinker";
11+
12+
public static String urlLinkify(String url) {
13+
int index = url.indexOf("https://");
14+
if (index == -1) return url;
15+
ArrayList<LinkifyTask> linkifyTasks = new ArrayList<>();
16+
int extra = 0;
17+
while (index != -1) {
18+
int end = Math.min(url.indexOf('\n', index),
19+
url.indexOf(' ', index));
20+
if (end == -1) end = url.length();
21+
if (index == 0 ||
22+
'\n' == url.charAt(index - 1) ||
23+
' ' == url.charAt(index - 1)) {
24+
int endDomain = url.indexOf('/', index + 9);
25+
if (endDomain != -1 && endDomain < end) {
26+
linkifyTasks.add(new LinkifyTask(index, end));
27+
extra += (end - index) + 4;
28+
if (BuildConfig.DEBUG) {
29+
Log.d(TAG, "Linkify url: " + url.substring(index, end));
30+
}
31+
}
32+
}
33+
index = url.indexOf("https://", end);
34+
}
35+
if (linkifyTasks.isEmpty()) return url;
36+
LinkifyTask prev = LinkifyTask.NULL;
37+
StringBuilder stringBuilder = new StringBuilder(url.length() + extra);
38+
for (LinkifyTask linkifyTask : linkifyTasks) {
39+
stringBuilder.append(url, prev.end, linkifyTask.start)
40+
.append('[').append(url, linkifyTask.start, linkifyTask.end)
41+
.append("](").append(url, linkifyTask.start, linkifyTask.end).append(')');
42+
prev = linkifyTask;
43+
}
44+
if (prev.end != url.length()) stringBuilder.append(url, prev.end, url.length());
45+
Log.d(TAG, "Added Markdown link to " + linkifyTasks.size() + " urls");
46+
return stringBuilder.toString();
47+
}
48+
49+
private static class LinkifyTask {
50+
static final LinkifyTask NULL = new LinkifyTask(0, 0);
51+
52+
private final int start;
53+
private final int end;
54+
55+
private LinkifyTask(int start, int end) {
56+
this.start = start;
57+
this.end = end;
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)