Forum Discussion

arthur_'s avatar
arthur_
Guide
7 years ago

Truncate messages that get passed to email notifications

Hi,

 

We are looking into possibility to truncate post messages that get passed into email notifications users get.

 

As there are no such functionality built in, the intention was to find the last occurrence of </p> tag (since everything gets wrapped in paragraph tags by default in tinymce editor) after certain number of characters to avoid that we deliberately broke in half some embedded link or image.

 

The problem is that, indexOf() method returns -1 when we look for </p> tag inside the string that gets passed from context object. 

 

The attempt to use indexOf() method with possible sequences of escaped </p> tag has yielded the same result, it constantly returns -1.

 

Any suggestion on how to solve this problem (truncate messages) is appreciated.

  • arthur_- Can you try with Capital letters.

     

    .indexOf("<P>")
    .indexOf("</P>")

     I noticed this issue with freemarker as lithium message body returns HTML tags in the capital case.

  • This is a great question. Unfortunately, Velocity documentation isn't the best, here's what I've found: http://velocitycoder.com/string-substring-and-index-of/

    #set ( $index = $notification.message.body.indexOf('</p>', 0) )

    Have you tried using indexOf with a string value you know exists in the message? Might be good to check if the function is working correctly

    We plan to implement this at some point but haven't done any work. One consideration was using CSS max-height with some kind of fade mask effect. Support for max-height is a bit spotty though: https://www.campaignmonitor.com/css/box-model/max-height/

    Good luck, please share updates if you find a good solution
    • arthur_'s avatar
      arthur_
      Guide

      ttadej

       

      Thank you very much for the suggestion.

       

      Initially we were hoping to use message's teaser where available, but since it does not get exposed, the rest were attempts to work around this limitation.

       

      About indexOf method 

       

      What I understood from Velocity documentation, it's able to run basic Java methods. At least in theory, it should be possible to find the index using indexOf method on specific string.

       

      For example if we have the string like this (should be passed from post to service that makes and send emails):

       

      <p>Another test</p>

       

      Then, when we run indexOf method on it like:

       

      .indexOf("test")
      .indexOf("</p>")

       

      We get index of 11 for string "test", that's correct. It starts at 11th position in our example string.

       

      When we look for closing </p> tag, it returns not found (-1). The same happens if we look for opening <p> tag.

       

      About max-height

       

      It might work, but unfortunately it's not reliable as it still might cut part of the post creating bad user experience.

      • arthur_- Can you try with Capital letters.

         

        .indexOf("<P>")
        .indexOf("</P>")

         I noticed this issue with freemarker as lithium message body returns HTML tags in the capital case.