mirror of
https://codeberg.org/Bazsalanszky/Infinity-For-Lemmy.git
synced 2025-01-16 13:13:08 +01:00
Don't handle unescaped bracket links for now
Until there is a better solution that can also handle `[Title]: https://link.com` type links and others.
This commit is contained in:
parent
e3976248cc
commit
839325efbe
@ -61,7 +61,7 @@ public class Utils {
|
|||||||
private static final long YEAR_MILLIS = 12 * MONTH_MILLIS;
|
private static final long YEAR_MILLIS = 12 * MONTH_MILLIS;
|
||||||
|
|
||||||
public static String modifyMarkdown(String markdown) {
|
public static String modifyMarkdown(String markdown) {
|
||||||
StringBuilder regexed = new StringBuilder(trimAndEscapeSpaceInLinks(markdown)
|
StringBuilder regexed = new StringBuilder(markdown
|
||||||
.replaceAll("((?<=[\\s])|^)/[rRuU]/[\\w-]+/{0,1}", "[$0](https://www.reddit.com$0)")
|
.replaceAll("((?<=[\\s])|^)/[rRuU]/[\\w-]+/{0,1}", "[$0](https://www.reddit.com$0)")
|
||||||
.replaceAll("((?<=[\\s])|^)[rRuU]/[\\w-]+/{0,1}", "[$0](https://www.reddit.com/$0)")
|
.replaceAll("((?<=[\\s])|^)[rRuU]/[\\w-]+/{0,1}", "[$0](https://www.reddit.com/$0)")
|
||||||
.replaceAll("\\^{2,}", "^")
|
.replaceAll("\\^{2,}", "^")
|
||||||
@ -77,52 +77,6 @@ public class Utils {
|
|||||||
return regexed.toString();
|
return regexed.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Also matches links inside code blocks as a side effect
|
|
||||||
private static String trimAndEscapeSpaceInLinks(String markdown){
|
|
||||||
String TEXT = "\\[([^\\[]\\n?[\\S\\h]+?\\n?[\\S\\h]*?)]";
|
|
||||||
String LINK = "(\\n?[\\h]*?\\n?https?:\\/\\/[\\S\\h]+?\n?)";
|
|
||||||
String TITLE = "(?:\\s\"(\\n?[\\S\\h]+?\\n?[\\S\\h]*?)?\")?";
|
|
||||||
Pattern pattern = Pattern.compile(TEXT + "\\(" + LINK + TITLE + "\\n?\\h*?\\)");
|
|
||||||
Matcher matcher = pattern.matcher(markdown);
|
|
||||||
StringBuilder builder = new StringBuilder();
|
|
||||||
int start = 0;
|
|
||||||
while (matcher.find()) try {
|
|
||||||
String match1;
|
|
||||||
String match2;
|
|
||||||
String match3;
|
|
||||||
if ((match1 = matcher.group(1)) != null && (match2 = matcher.group(2)) != null) {
|
|
||||||
match1 = match1.trim().replaceAll("\\s+", " ");
|
|
||||||
match2 = match2.trim().replaceAll(" ", "%20");
|
|
||||||
match3 = matcher.group(3);
|
|
||||||
builder.append(markdown.substring(start, matcher.start()))
|
|
||||||
.append("[")
|
|
||||||
.append(match1)
|
|
||||||
.append("]")
|
|
||||||
.append("(")
|
|
||||||
.append(match2);
|
|
||||||
if (match3 != null) {
|
|
||||||
builder.append(" \"")
|
|
||||||
.append(match3)
|
|
||||||
.append("\"");
|
|
||||||
}
|
|
||||||
builder.append(")");
|
|
||||||
start = matcher.end();
|
|
||||||
}
|
|
||||||
} catch (NullPointerException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (start < markdown.length()) {
|
|
||||||
builder.append(markdown.substring(start));
|
|
||||||
}
|
|
||||||
|
|
||||||
if(builder.length() > 0) {
|
|
||||||
return builder.toString(); }
|
|
||||||
else {
|
|
||||||
return markdown;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String fixSuperScript(StringBuilder regexed) {
|
public static String fixSuperScript(StringBuilder regexed) {
|
||||||
boolean hasBracket = false;
|
boolean hasBracket = false;
|
||||||
int nCarets = 0;
|
int nCarets = 0;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user