From 57389a5c57e20c1cded373a9a58d54878951140d Mon Sep 17 00:00:00 2001 From: Harshad Vedartham Date: Sun, 21 Jan 2024 14:42:36 -0800 Subject: [PATCH] Fixed issue in link detection --- .../activity/DocumentShareIntoFragment.java | 31 +++++++++++++------ 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/net/gsantner/markor/activity/DocumentShareIntoFragment.java b/app/src/main/java/net/gsantner/markor/activity/DocumentShareIntoFragment.java index 56d129dfa8..2de7caf36b 100644 --- a/app/src/main/java/net/gsantner/markor/activity/DocumentShareIntoFragment.java +++ b/app/src/main/java/net/gsantner/markor/activity/DocumentShareIntoFragment.java @@ -220,25 +220,36 @@ private void appendToExistingDocumentAndClose(final File file, final boolean sho */ public static List> findLinksAndPaths(final CharSequence text) { final List> 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; }