Support inline gif in comments.

This commit is contained in:
Alex Ning 2021-04-06 23:43:32 +08:00
parent 98a66a8bb7
commit bedcf6d0b6
2 changed files with 20 additions and 1 deletions

View File

@ -140,7 +140,7 @@ public class ParseComment {
String distinguished = singleCommentData.getString(JSONUtils.DISTINGUISHED_KEY); String distinguished = singleCommentData.getString(JSONUtils.DISTINGUISHED_KEY);
String commentMarkdown = ""; String commentMarkdown = "";
if (!singleCommentData.isNull(JSONUtils.BODY_KEY)) { if (!singleCommentData.isNull(JSONUtils.BODY_KEY)) {
commentMarkdown = Utils.modifyMarkdown(singleCommentData.getString(JSONUtils.BODY_KEY).trim()); commentMarkdown = Utils.parseInlineGifInComments(Utils.modifyMarkdown(singleCommentData.getString(JSONUtils.BODY_KEY).trim()));
} }
String commentRawText = Utils.trimTrailingWhitespace( String commentRawText = Utils.trimTrailingWhitespace(
Html.fromHtml(singleCommentData.getString(JSONUtils.BODY_HTML_KEY))).toString(); Html.fromHtml(singleCommentData.getString(JSONUtils.BODY_HTML_KEY))).toString();

View File

@ -22,6 +22,8 @@ import androidx.core.text.HtmlCompat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Calendar; import java.util.Calendar;
import java.util.Locale; import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.SortType; import ml.docilealligator.infinityforreddit.SortType;
@ -93,6 +95,23 @@ public class Utils {
return regexed.toString(); return regexed.toString();
} }
public static String parseInlineGifInComments(String markdown) {
StringBuilder markdownStringBuilder = new StringBuilder(markdown);
Pattern inlineGifPattern = Pattern.compile("!\\[gif]\\(giphy\\|\\w+\\)");
Matcher matcher = inlineGifPattern.matcher(markdownStringBuilder);
while (matcher.find()) {
markdownStringBuilder.replace(matcher.start(), matcher.end(), "[gif](https://media3.giphy.com/media/" + markdownStringBuilder.substring(matcher.start() + "![gif](giphy|".length(), matcher.end() - 1) + "/giphy.mp4)");
}
Pattern inlineGifPattern2 = Pattern.compile("!\\[gif]\\(giphy\\|\\w+\\|downsized\\)");
Matcher matcher2 = inlineGifPattern2.matcher(markdownStringBuilder);
while (matcher2.find()) {
markdownStringBuilder.replace(matcher2.start(), matcher2.end(), "[gif](https://media3.giphy.com/media/" + markdownStringBuilder.substring(matcher2.start() + "![gif](giphy|".length(), matcher2.end() - "|downsized\\)".length()) + "/giphy.mp4)");
}
return markdownStringBuilder.toString();
}
public static CharSequence trimTrailingWhitespace(CharSequence source) { public static CharSequence trimTrailingWhitespace(CharSequence source) {
if(source == null) if(source == null)