question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

  • Markwon version: {4.4.0}

Thanks for this awesome library. My life became so much easier after I wasted 2 days trying to handle bullet points, numbered lists, tables and whatnot manually 😅

However, earlier in my manual parsing, I was adding hashtag parsing (making text bold with some color) to any word preceded by # (without spaces), like “#someWord”

Is there any way to add these custom spans to the already existing markdown text? I’m using the following code to set the markdown:

var TextView?.markdownText: String?
  @Deprecated(NO_GETTER, level = DeprecationLevel.ERROR) get() = noGetter()
  set(value) {
    this?.let { textView ->
      Markwon.builder(context)
        .usePlugin(StrikethroughPlugin.create())
        .usePlugin(object : AbstractMarkwonPlugin() {

          override fun configureConfiguration(builder: MarkwonConfiguration.Builder) {
            builder.linkResolver { view, link ->  }
          }

          override fun configureTheme(builder: MarkwonTheme.Builder) {
            builder
              .linkColor(Color.parseColor(ACCENT_BLUE_COLOR))
              .bulletWidth(dip(4))
              .bulletListItemStrokeWidth(dip(1))
              .listItemColor(Color.parseColor("#DE2F2F2F"))
              .blockMargin(dip(20))
          }
        })
        .usePlugin(TablePlugin.create {
          it.tableBorderWidth(dip(1))
          it.tableBorderColor(Color.parseColor("#E5E5E5"))
          it.tableCellPadding(dip(4))
        })
        .build().let { markwon ->
          val markdown = markwon.toMarkdown(value.orEmpty())
          markwon.setParsedMarkdown(textView, markdown)
        }
    }
  }

And this is my hashtag parsing function:

  private fun SpannableStringBuilder.withHashTagHandling(): SpannableStringBuilder = apply {
    if (isEmpty()) return this
    Pattern.compile("""#[A-Za-z0-9!]*""").matcher(this)?.let { matcher ->
      while (matcher.find()) {
        matcher.group(0)?.let { hashTagString ->
          setSpan(StyleSpan(Typeface.BOLD), matcher.start(), matcher.end(), SPAN_EXCLUSIVE_EXCLUSIVE)
          setSpan(ForegroundColorSpan(colorCode), matcher.start(), matcher.end(), SPAN_EXCLUSIVE_EXCLUSIVE)
          setSpan(RelativeSizeSpan(1.1F), matcher.start(), matcher.end(), SPAN_EXCLUSIVE_EXCLUSIVE)
        }
      }
    }
  }

I search around in the wiki regarding hashtags, but couldn’t find anything, can you please guide me if there is a way to add the spans while setting the text?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
notiescommented, Jun 4, 2020

There is always room for improvement, but I find your implementation pretty solid

1reaction
premacckcommented, Jun 2, 2020

Okay so this is what I’ve implemented:

My HashtagTextAddedListener:

  private class HashtagTextAddedListener : CorePlugin.OnTextAddedListener {

    private val hashtagPattern = Pattern.compile("""#[A-Za-z0-9!]*""")

    override fun onTextAdded(visitor: MarkwonVisitor, text: String, start: Int) {
      hashtagPattern.matcher(text)?.let { matcher ->
        while (matcher.find()) {
          SpannableBuilder.setSpans(
            visitor.builder(), arrayOf(StyleSpan(Typeface.BOLD), ForegroundColorSpan(Color.parseColor(ACCENT_BLUE_COLOR)), RelativeSizeSpan(1.1F)), matcher.start(), matcher.end()
          )
        }
      }
    }
  }

And I’m applying this in the configure method like this:

    override fun configure(registry: MarkwonPlugin.Registry) {
      registry.require(CorePlugin::class.java) {
        it.addOnTextAddedListener(HashtagTextAddedListener())
      }
    }

This approach is working, but can it be further improved?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Hashtags for #span to grow your Instagram, TikTok
Best hashtags for use with #span are #span #rokspan #rok #n #rokwanita #sp #fashion #woodporn #kunsthandwerk #basteln #holzarbeit #modernart #holzdeko ...
Read more >
Best Instagram hashtags for #span | Flick: Instagram Hashtag Tool
The top 30 trending hashtags for #span ... Trending Instagram hashtags related to #span are determined by the average number of daily posts...
Read more >
span Hashtags 2022 - copy and paste hashtags generator
Are you looking for span hashtags to boost likes and followers on your Instagram ... Best span hashtags popular on Instagram, Twitter, Facebook,...
Read more >
Search a string for hashtags (with spaces) and wrap with spans
I'd like to search a string and wrap hashtags with spans. I found a great answer here.
Read more >
#span hashtag on Instagram • Photos and Videos
104k Posts - See Instagram photos and videos from 'span' hashtag. ... Rusty Bridge #bridgmanpark #bridge #Lynncreek #span #creekcrossing #yellow #leaves.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found