Jekyll strip_html 但允许某些 HTML 标签
Jekyll strip_html but allow certain HTML tags
在我的 Jekyll post 中,我正在使用 strip_html
在主页上显示 post 的 50 字简短介绍:
<p>{{ post.content | strip_html | truncatewords:50 }}</p>
strip_html
从此摘录中删除所有 HTML。但我希望包括一些 HTML,特别是 <i>
和 <p>
。
是否有用于此操作的配置 (_config.yaml
),或者是否存在 strip_html
没有自定义的限制?
据我所知,没有内置的方式来自定义 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>
在我的 Jekyll post 中,我正在使用 strip_html
在主页上显示 post 的 50 字简短介绍:
<p>{{ post.content | strip_html | truncatewords:50 }}</p>
strip_html
从此摘录中删除所有 HTML。但我希望包括一些 HTML,特别是 <i>
和 <p>
。
是否有用于此操作的配置 (_config.yaml
),或者是否存在 strip_html
没有自定义的限制?
据我所知,没有内置的方式来自定义 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>