What I am doing:
- In NodeJS I am creating an email template by using MustacheJS, using data from an array of JSON objects.
- The text/message that goes in the template can contain text along with basic html tags (such as
b
p
&a
). - Due to limitation of space I need to only show an excerpt of the message. For that I do a word count, and after lets say 20 words (checked by spaces) I truncate the string and append
View more
anchor tag. This links it to the website's post page, that contains the complete post. Something like:
Hey this is a sample post text
<b>message</b>
. Lorem ipsum dolor sit amit...<a href="someurl">
View more</a>
The problem:
During word count and truncation, it is possible that I truncate the string in between an html tag as I am simply calculating words on basis of space. Something like:
I am sharing a link with you.
<a style="color:
...<a href="someurl">
View more</a>
Now this will break the html.
Possible solution:
- Before truncating string, run a regex on it to find all the html tags in it.
- Use
indexOf()
(or some other method) to find starting and ending indices of each tag. - After word count, get the index where I need to truncate it.
- Now see that if the index intersects with any of the tags region.
- If it does intersect, simply move the truncate index to the start or end of the html tag.
Question:
Is there a better way to do this. I don't know what search terms I should be searching on google, to get help with this.
P.S. The code is flexible and I can change the flow if there is a significantly better solution. I am not good with post titles. Please modify it to something that reflects the question.
Aucun commentaire:
Enregistrer un commentaire