#949 Use https:// as protocol for www links

This commit is contained in:
Stefan Niedermann 2020-10-05 09:41:52 +02:00
parent 7767776835
commit 01c46004e7
4 changed files with 13 additions and 14 deletions

View file

@ -1,28 +1,26 @@
package it.niedermann.owncloud.notes.shared.util.text;
import java.util.regex.Matcher;
import androidx.annotation.NonNull;
import java.util.regex.Pattern;
public class WwwLinksProcessor extends TextProcessor {
public static final String WWW_URLS_PROTOCOL_PREFIX = "http://";
private static final String replaceWwwUrlsRegEx = "\\[([^]]*)]\\((www\\..+)\\)";
private static final String WWW_URLS_PROTOCOL_PREFIX = "https://";
private static final String REGEX_REPLACE_WWW_URLS = "\\[([^]]*)]\\((www\\..+)\\)";
/**
* Prefixes all links, that not not start with a protocol identifier, but with "www." with http://
*
* <p>
* See https://github.com/stefan-niedermann/nextcloud-notes/issues/949
*
* @return Markdown with all pseudo-links replaced through actual HTTP-links
*/
@Override
public String process(String s) {
return replaceWwwUrls(s);
}
private static String replaceWwwUrls(String markdown) {
Pattern replacePattern = Pattern.compile(replaceWwwUrlsRegEx);
Matcher replaceMatcher = replacePattern.matcher(markdown);
return replaceMatcher.replaceAll(String.format("[$1](%s$2)", WWW_URLS_PROTOCOL_PREFIX));
return Pattern
.compile(REGEX_REPLACE_WWW_URLS)
.matcher(s)
.replaceAll(String.format("[$1](%s$2)", WWW_URLS_PROTOCOL_PREFIX));
}
}

View file

@ -18,7 +18,7 @@ public class TextProcessorChainTest extends TestCase {
Assert.assertEquals("SelfIdentifyingProcessor 1\nSelfIdentifyingProcessor 2", chain.apply(""));
}
class SelfIdentifyingProcessor extends TextProcessor {
static class SelfIdentifyingProcessor extends TextProcessor {
private int id;
public SelfIdentifyingProcessor(int id) {

View file

@ -37,13 +37,12 @@ public class WwwLinksProcessorTest extends TestCase {
Assert.assertEquals(markdown, sut.process(markdown));
}
@SuppressWarnings("MarkdownUnresolvedFileReference")
public void testDoNotReplaceNormalLinks() {
TextProcessor sut = new WwwLinksProcessor();
//language=md
String markdown = "[normal link](https://example.com) and another [www link](www.example.com) and one more [normal link](https://www.example.com)";
String result = sut.process(markdown);
Assert.assertEquals("[normal link](https://example.com) and another [www link](http://www.example.com) and one more [normal link](https://www.example.com)", result);
Assert.assertEquals("[normal link](https://example.com) and another [www link](https://www.example.com) and one more [normal link](https://www.example.com)", result);
}
}

View file

@ -0,0 +1,2 @@
- Selecting a note in the main note list highlights two notes (#920) (@muety)
- Support www links without protocol in preview mode (#949) (@muety)