Skip to content

Commit

Permalink
Fixed issue in link detection
Browse files Browse the repository at this point in the history
  • Loading branch information
harshad1 committed Jan 21, 2024
1 parent 5d54567 commit 57389a5
Showing 1 changed file with 21 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -220,25 +220,36 @@ private void appendToExistingDocumentAndClose(final File file, final boolean sho
*/
public static List<Pair<int[], Boolean>> findLinksAndPaths(final CharSequence text) {
final List<Pair<int[], Boolean>> links = new ArrayList<>();

GsTextUtils.forEachline(text, (line, start, end) -> {

start = TextViewUtils.getNextNonWhitespace(text, start);
end = TextViewUtils.getLastNonWhitespace(text, end) + 1;
end = TextViewUtils.getLastNonWhitespace(text, end - 1) + 1;

if (start != -1 && end != -1 && start <= end) {
final String tl = text.subSequence(start, end).toString();
final boolean hasSpaces = tl.contains(" ") || tl.contains("\\t");
if (!hasSpaces && Patterns.WEB_URL.matcher(tl).matches()) {

final String trimmed = text.subSequence(start, end).toString();
final boolean hasSpaces = trimmed.contains(" ") || trimmed.contains("\\t");

// Test for web links
if (!hasSpaces && Patterns.WEB_URL.matcher(trimmed).matches()) {
links.add(Pair.create(new int[]{start, end}, false));
} else {
try {
if (new File(tl.replace("%20", " ")).exists()) {
links.add(Pair.create(new int[]{start, end}, true));
}
} catch (NullPointerException ignored) {
return true;
}

// Test for file links
try {
if (new File(trimmed.replace("%20", " ")).exists()) {
links.add(Pair.create(new int[]{start, end}, true));
return true;
}
} catch (NullPointerException ignored) {
}
}

return true;
});

return links;
}

Expand Down

0 comments on commit 57389a5

Please sign in to comment.