From a288a638b5d08b77e16a6679fa2de5653e434a13 Mon Sep 17 00:00:00 2001 From: Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> Date: Thu, 4 May 2023 13:22:07 +0800 Subject: Markdown in subreddit and user descriptions. --- .../activities/ViewSubredditDetailActivity.java | 16 ++++++++++++++-- .../activities/ViewUserDetailActivity.java | 3 +-- .../infinityforreddit/markdown/MarkdownUtils.java | 19 +++++++++++++++++++ 3 files changed, 34 insertions(+), 4 deletions(-) (limited to 'app/src/main') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java index 1c4e340e..5203c567 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java @@ -9,6 +9,7 @@ import android.os.Bundle; import android.os.Handler; import android.text.Editable; import android.text.TextWatcher; +import android.text.util.Linkify; import android.view.Gravity; import android.view.KeyEvent; import android.view.Menu; @@ -64,6 +65,13 @@ import io.noties.markwon.Markwon; import io.noties.markwon.MarkwonConfiguration; import io.noties.markwon.MarkwonPlugin; import io.noties.markwon.core.MarkwonTheme; +import io.noties.markwon.ext.strikethrough.StrikethroughPlugin; +import io.noties.markwon.inlineparser.BangInlineProcessor; +import io.noties.markwon.inlineparser.HtmlInlineProcessor; +import io.noties.markwon.inlineparser.MarkwonInlineParserPlugin; +import io.noties.markwon.linkify.LinkifyPlugin; +import io.noties.markwon.movement.MovementMethodPlugin; +import io.noties.markwon.recycler.table.TableEntryPlugin; import jp.wasabeef.glide.transformations.RoundedCornersTransformation; import me.saket.bettermovementmethod.BetterLinkMovementMethod; import ml.docilealligator.infinityforreddit.ActivityToolbarInterface; @@ -99,6 +107,10 @@ import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent; import ml.docilealligator.infinityforreddit.fragments.PostFragment; import ml.docilealligator.infinityforreddit.fragments.SidebarFragment; import ml.docilealligator.infinityforreddit.markdown.MarkdownUtils; +import ml.docilealligator.infinityforreddit.markdown.RedditHeadingPlugin; +import ml.docilealligator.infinityforreddit.markdown.SpoilerAwareMovementMethod; +import ml.docilealligator.infinityforreddit.markdown.SpoilerParserPlugin; +import ml.docilealligator.infinityforreddit.markdown.SuperscriptPlugin; import ml.docilealligator.infinityforreddit.message.ReadMessage; import ml.docilealligator.infinityforreddit.multireddit.MultiReddit; import ml.docilealligator.infinityforreddit.post.Post; @@ -387,8 +399,8 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp urlMenuBottomSheetFragment.show(getSupportFragmentManager(), null); return true; }; - Markwon markwon = MarkdownUtils.createLinksOnlyMarkwon(this, - miscPlugin, onLinkLongClickListener); + + Markwon markwon = MarkdownUtils.createDescriptionMarkwon(this, miscPlugin, onLinkLongClickListener); descriptionTextView.setOnLongClickListener(view -> { if (description != null && !description.equals("") && descriptionTextView.getSelectionStart() == -1 && descriptionTextView.getSelectionEnd() == -1) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java index 84eb24ff..a9a2068c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java @@ -389,8 +389,7 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele urlMenuBottomSheetFragment.show(getSupportFragmentManager(), null); return true; }; - Markwon markwon = MarkdownUtils.createLinksOnlyMarkwon(this, - miscPlugin, onLinkLongClickListener); + Markwon markwon = MarkdownUtils.createDescriptionMarkwon(this, miscPlugin, onLinkLongClickListener); descriptionTextView.setOnLongClickListener(view -> { if (description != null && !description.equals("") && descriptionTextView.getSelectionStart() == -1 && descriptionTextView.getSelectionEnd() == -1) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/MarkdownUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/MarkdownUtils.java index 678e5a38..2c56fc9c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/MarkdownUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/MarkdownUtils.java @@ -51,6 +51,25 @@ public class MarkdownUtils { .build(); } + @NonNull + public static Markwon createDescriptionMarkwon(Context context, MarkwonPlugin miscPlugin, + BetterLinkMovementMethod.OnLinkLongClickListener onLinkLongClickListener) { + return Markwon.builder(context) + .usePlugin(MarkwonInlineParserPlugin.create(plugin -> { + plugin.excludeInlineProcessor(HtmlInlineProcessor.class); + plugin.excludeInlineProcessor(BangInlineProcessor.class); + })) + .usePlugin(miscPlugin) + .usePlugin(SuperscriptPlugin.create()) + .usePlugin(RedditHeadingPlugin.create()) + .usePlugin(StrikethroughPlugin.create()) + .usePlugin(MovementMethodPlugin.create(new SpoilerAwareMovementMethod() + .setOnLinkLongClickListener(onLinkLongClickListener))) + .usePlugin(LinkifyPlugin.create(Linkify.WEB_URLS)) + .usePlugin(TableEntryPlugin.create(context)) + .build(); + } + /** * Creates a Markwon instance that processes only the links. * @return configured Markwon instance -- cgit v1.2.3