Forum Discussion

domladden's avatar
9 years ago

Word Count component

Hi, I need to create a quick little component that will pull the word count from my articles.

 

I've checked the community here and can't find much more than a suggestion for a product idea that wasn't picked up.

 

I can pull the body of my articles with a simple API call: /restapi/vc/messages/id/3701/body

 

This will dismiss all the content in other parts of the page (related content, labels etc.) but it returns a bunch of HTML, is there any quick of stripping out everything that isn't contained in a <span> tag, counting that and storing it in a variable?

 

Hopefully I want to end with nothing but a number at the bottom of my articles, doens't sound too hard but I'm struggling so far.

 

Thanks,

 

Dom

  • domladden,

     

    Did you tried Regex patterns to remove the tags from the body contained so that you can have the bunch of body contained.

     

    Please try below regex pattern:-

    ?replace("</?[^>]*(>|$)|&(nbsp;?|#?[0-9A-Za-z]*$)", " ", "r")?replace("\\s+", " ", "r")

     

    This will help you to remove all HTML tags from the body.

     

    Regards,

    Vishwajit Shinde.

     

    If my post is helpful and answers your question, please give "Kudos" and "Accept it as a Solution."

    • domladden's avatar
      domladden
      Ace

      Hi, yes this works great, thanks.

       

      If anyone else is interested, this is the component I made:

       

            <#assign msg_id_check = page.context.message.uniqueId />
            <#assign msg_id_check2 = msg_id_check?string>
      
      
      <#assign messagebody = rest("messages/id/${msg_id_check2}/body").value />
      
      
      
      <#assign messagebody2 = messagebody?replace("</?[^>]*(>|$)|&(nbsp;?|#?[0-9A-Za-z]*$)", " ", "r")?replace("\\s+", " ", "r") />
      
      
      <#assign messagebody3 = messagebody2?trim?split(" ")?size />
      
      
      <p>${messagebody3}</p>
      
      
      
      

      I then applied some CSS to the <p> tag to pretty much hide the word count so only I can see it but you should be pretty set to go.

       

      Thanks again,

       

      Dom

      • luk's avatar
        luk
        Boss
        Why so complicated =), wouldn't that have been much easier with a tiny bit of jQuery?