"truncatechars:x" 有降价友好的替代品吗?

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

经典任务是显示博客列表-post,包括每个 post.

的简短摘要

我在网上看到,最常见的做法是截断原博客的内容post。

所以我的 post_list.html 模板如下所示:

{% load custom_markdown %}

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

如果我截断 markdown,有时 html 标签不会正确关闭 - 结束标签会被截断 - 整个 html 页面都会变得一团糟。

是否有截断的智能方法或者我需要编写自己的函数?

PS 我得到的最接近的是这个 Whosebug post: how to truncate markdown in Ruby/Rails.

您可以使用 truncatechars_html 标签。

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