diff options
Diffstat (limited to 'app/src/main')
-rw-r--r-- | app/src/main/java/ml/docilealligator/infinityforreddit/markdown/SuperscriptPlugin.java | 54 |
1 files changed, 54 insertions, 0 deletions
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(); |