RaZ
RaZ

Reputation: 1296

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.

Upvotes: 5

Views: 338

Answers (1)

Alasdair
Alasdair

Reputation: 308799

You can use the truncatechars_html tag.

{{ post.text | custom_markdown | truncatechars_html:160 }}

Upvotes: 7

Related Questions