Skip to content

Commit

Permalink
Extend workaround for extra new lines in timeline
Browse files Browse the repository at this point in the history
Closes #8179

Caused by noties/Markwon#423
  • Loading branch information
jonnyandrew committed Feb 28, 2023
1 parent 09ad11d commit 61ca38e
Showing 1 changed file with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ import android.graphics.Typeface
import android.graphics.drawable.Drawable
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.style.StrikethroughSpan
import android.text.style.URLSpan
import android.text.style.UnderlineSpan
import android.widget.TextView
import androidx.core.text.toSpannable
import com.bumptech.glide.Glide
Expand All @@ -46,6 +49,8 @@ import io.noties.markwon.Markwon
import io.noties.markwon.MarkwonPlugin
import io.noties.markwon.MarkwonSpansFactory
import io.noties.markwon.PrecomputedFutureTextSetterCompat
import io.noties.markwon.core.spans.EmphasisSpan
import io.noties.markwon.core.spans.StrongEmphasisSpan
import io.noties.markwon.ext.latex.JLatexMathPlugin
import io.noties.markwon.ext.latex.JLatexMathTheme
import io.noties.markwon.html.HtmlPlugin
Expand Down Expand Up @@ -154,14 +159,21 @@ class EventHtmlRenderer @Inject constructor(
/**
* Workaround for https://github.com/noties/Markwon/issues/423
*/
private val removeLeadingNewlineForInlineCode = object : AbstractMarkwonPlugin() {
private val removeLeadingNewlineForInlineElement = object : AbstractMarkwonPlugin() {
override fun afterSetText(textView: TextView) {
super.afterSetText(textView)

val text = SpannableStringBuilder(textView.text.toSpannable())
val inlineCodeSpans = text.getSpans(0, textView.length(), InlineCodeSpan::class.java).toList()
val legacyInlineCodeSpans = text.getSpans(0, textView.length(), HtmlCodeSpan::class.java).filter { !it.isBlock }
val spans = inlineCodeSpans + legacyInlineCodeSpans
val spans = arrayOf(
text.getSpans(0, textView.length(), InlineCodeSpan::class.java),
text.getSpans(0, textView.length(), HtmlCodeSpan::class.java).filter { !it.isBlock }.toTypedArray(),
text.getSpans(0, textView.length(), EmphasisSpan::class.java),
text.getSpans(0, textView.length(), CustomTypefaceSpan::class.java),
text.getSpans(0, textView.length(), StrongEmphasisSpan::class.java),
text.getSpans(0, textView.length(), UnderlineSpan::class.java),
text.getSpans(0, textView.length(), URLSpan::class.java),
text.getSpans(0, textView.length(), StrikethroughSpan::class.java),
).flatten()

if (spans.isEmpty()) return

Expand All @@ -179,11 +191,11 @@ class EventHtmlRenderer @Inject constructor(
private val markwon = Markwon.builder(context)
.usePlugin(HtmlRootTagPlugin())
.usePlugin(HtmlPlugin.create(htmlConfigure))
.usePlugin(removeLeadingNewlineForInlineCode)
.usePlugin(removeLeadingNewlineForInlineElement)
.usePlugin(glidePlugin)
.apply {
if (vectorPreferences.latexMathsIsEnabled()) {
// If latex maths is enabled in app preferences, refomat it so Markwon recognises it
// If latex maths is enabled in app preferences, reformat it so Markwon recognises it
// It needs to be in this specific format: https://noties.io/Markwon/docs/v4/ext-latex
latexPlugins.forEach(::usePlugin)
}
Expand Down

0 comments on commit 61ca38e

Please sign in to comment.