Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a markdown friendly alternative to "truncatechars:x"?

The classic task is to display the list of blog-posts, including a short summary of each post.

As I have seen on the web, the most common approach is to truncate the content of the original blog post.

So my post_list.html template looks like this:

{% load custom_markdown %}

<div class="col-sm-12">
 <p>
  <small>
   {{ post.text | custom_markdown | truncatechars:160 }}
  </small>
 </p>
</div>

If I truncate markdown sometimes the html tags do not get closed properly - the end tag gets truncated - and the whole html page gets all messed-up.

Is there an intelligent way to truncate or do I need to write my own function?

PS The closest I got was this stackoverflow post: how to truncate markdown in Ruby/Rails.

like image 846
ionescu77 Avatar asked Jun 16 '16 15:06

ionescu77


1 Answers

You can use the truncatechars_html tag.

{{ post.text | custom_markdown | truncatechars_html:160 }}
like image 138
Alasdair Avatar answered Oct 05 '22 01:10

Alasdair