Jekyll strip_html但允许某些HTML标记

Mat*_*ith 6 liquid jekyll

在我的Jekyll帖子中,我strip_html用来在主页面上显示一篇简短的50字的帖子介绍:

<p>{{ post.content | strip_html | truncatewords:50 }}</p>
Run Code Online (Sandbox Code Playgroud)

strip_html从此摘录中删除所有HTML.但我想包括一些HTML,特别是<i><p>.

是否有_config.yaml用于执行此操作的配置(),或者是否存在strip_html无法自定义的限制?

was*_*ful 5

AFAIK没有内置的自定义方式strip_html.

如果你有一个独家 - 而不是那么长 - 你想要的标签列表,你可能首先使用replace你想要保留的标签用非html标记替换它们,然后使用strip_html,然后replace再次取回html:

{% assign preprocessed_content=post.content | replace: '<p>', '__p__' %}
{% assign preprocessed_content=preprocessed_content | replace: '</p>', '__/p__' %}

{% assign truncated_content=preprocessed_content | strip_html | truncatewords:50 %}

{% assign cleaned_content=truncated_content | replace: '__p__', '<p>' %}
{% assign cleaned_content=cleaned_content | replace: '__/p__', '</p>' %}

<p>{{ cleaned_content }}</p>
Run Code Online (Sandbox Code Playgroud)