blob: bc656dd8062f14d32a7ce9dfe9d6367e6308e0e6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
package it.niedermann.owncloud.notes.shared.util.text;
import android.text.TextUtils;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import androidx.annotation.VisibleForTesting;
public class NoteLinksProcessor extends TextProcessor {
public static final String RELATIVE_LINK_WORKAROUND_PREFIX = "https://nextcloudnotes/notes/";
@VisibleForTesting
private static final String linksThatLookLikeNoteLinksRegEx = "\\[[^]]*]\\((\\d+)\\)";
private static final String replaceNoteRemoteIdsRegEx = "\\[([^\\]]*)\\]\\((%s)\\)";
private final Set<String> existingNoteRemoteIds;
public NoteLinksProcessor(Set<String> existingNoteRemoteIds) {
this.existingNoteRemoteIds = existingNoteRemoteIds;
}
/**
* Replaces all links to other notes of the form `[<link-text>](<note-file-id>)`
* in the markdown string with links to a dummy url.
*
* Why is this needed?
* See discussion in issue #623
*
* @return Markdown with all note-links replaced with dummy-url-links
*/
@Override
public String process(String s) {
return replaceNoteLinksWithDummyUrls(s, existingNoteRemoteIds);
}
private static String replaceNoteLinksWithDummyUrls(String markdown, Set<String> existingNoteRemoteIds) {
Pattern noteLinkCandidates = Pattern.compile(linksThatLookLikeNoteLinksRegEx);
Matcher matcher = noteLinkCandidates.matcher(markdown);
Set<String> noteRemoteIdsToReplace = new HashSet<>();
while (matcher.find()) {
String presumedNoteId = matcher.group(1);
if (existingNoteRemoteIds.contains(presumedNoteId)) {
noteRemoteIdsToReplace.add(presumedNoteId);
}
}
String noteRemoteIdsCondition = TextUtils.join("|", noteRemoteIdsToReplace);
Pattern replacePattern = Pattern.compile(String.format(replaceNoteRemoteIdsRegEx, noteRemoteIdsCondition));
Matcher replaceMatcher = replacePattern.matcher(markdown);
return replaceMatcher.replaceAll(String.format("[$1](%s$2)", RELATIVE_LINK_WORKAROUND_PREFIX));
}
}
|