0

I have to use at the same time parse_markdown and truncate. My text is (for example)

"Hi **boys**, I am very happy to be <a href="/here">here<a/> with you. I am so proud of you and your work."

If I use

<%= parse_markdown(truncate(@city.last_news.text, length: 30, separator: ' ')).html_safe %>

I see

Hi **boys**, I am very happy to be <a href="/here">here<a/> with you. I am so proud of you and your work.

with the markdown but without the truncate.

and if I use

<%= truncate(parse_markdown(@city.last_news.text).html_safe, length: 30, separator: ' ') %>

I see

    Hi **boys**, I am very happy to be <a href="/here">here<a/> with yo...

with the truncatebut without the markdown.

How to solve?

Clara
  • 2,677
  • 4
  • 16
  • 31
J.Luca
  • 208
  • 1
  • 10
  • 1
    There is no simple solution to this -- blindly truncating at a set length makes it likely you will truncate a portion of the mark up. – dbugger Dec 27 '20 at 19:44

1 Answers1

-1

I was using this gem to truncate html. If you can manage to get it working today (latest commit is from 5 years ago), this will solve one part of your problem. Idk if it can be useful for markdown too. Try it out

Example usage:

%p
  = truncate_html(document.description, length: 200)
Vi.
  • 940
  • 6
  • 24