From efcfa0ae71c90796b1995737d4feac46fe3542b1 Mon Sep 17 00:00:00 2001 From: Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> Date: Mon, 20 Feb 2023 21:36:38 -0500 Subject: Fix https://github.com/Docile-Alligator/Infinity-For-Reddit/issues/1367 --- .../markdown/SuperscriptPlugin.java | 54 ++++++++++++++++++++++ 1 file changed, 54 insertions(+) (limited to 'app') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/SuperscriptPlugin.java b/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/SuperscriptPlugin.java index 1a5f3526..796ca870 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/SuperscriptPlugin.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/SuperscriptPlugin.java @@ -7,8 +7,10 @@ import android.widget.TextView; import androidx.annotation.NonNull; import org.commonmark.ext.gfm.tables.TableCell; +import org.commonmark.node.Link; import org.commonmark.node.Node; import org.commonmark.node.Paragraph; +import org.commonmark.node.Text; import java.util.ArrayList; import java.util.List; @@ -95,6 +97,10 @@ public class SuperscriptPlugin extends AbstractMarkwonPlugin { public void visit(@NonNull MarkwonVisitor visitor, @NonNull Superscript superscript) { int start = visitor.length(); + if (!notEmptySuperscript(superscript)) { + return; + } + if (!superscript.isBracketed()) { visitor.builder().setSpan(new SuperscriptSpan(false), start, start + 1); // Workaround for Table Plugin superscriptOpeningList.add(new SuperscriptOpening(superscript, start)); @@ -123,6 +129,54 @@ public class SuperscriptPlugin extends AbstractMarkwonPlugin { }); } + private boolean notEmptyLink(Link link) { + Node next = link.getFirstChild(); + while (next != null) { + if (next instanceof Text) { + return true; + } else if (next instanceof Superscript) { + if (notEmptySuperscript((Superscript) next)) { + return true; + } + } else if (next instanceof Link) { + if (notEmptyLink((Link) next)) { + return true; + } + } else { + return true; + } + next = next.getNext(); + } + + return false; + } + + private boolean notEmptySuperscript(Superscript superscript) { + Node next; + if (superscript.isBracketed()) { + next = superscript.getFirstChild(); + } else { + next = superscript.getNext(); + } + + while (next != null) { + if (next instanceof Link) { + if (notEmptyLink((Link) next)) { + return true; + } + } else if (!(next instanceof Superscript)) { + return true; + } else { + if (notEmptySuperscript((Superscript) next)) { + return true; + } + } + next = next.getNext(); + } + + return false; + } + @Override public void afterRender(@NonNull Node node, @NonNull MarkwonVisitor visitor) { superscriptOpeningBracketStorage.clear(); -- cgit v1.2.3