How can I automatically escape HTML content using

2019-04-05 01:26发布

问题:

In foo.markdown I have the following:

---
layout: default
title: Snarky little Ewok
---

A little Ewok is sometimes referred too as <h3>. But pappa Ewok is called <h1> - if you know what's good for you.

Well, I want Jekyll to automatically html escape the greater than and less than characters. I'm seriously fatigued after today's apprentice training and I'm just too lazy to manually html escape myself: >h3<

Is there a config option or something to automatically escape Jekyll markdown content?

回答1:

If you used textile instead of markdown, there would be a way.

Liquid markup has textilize & escape filters; those two would allow you to do what you wanted, but on textile. You would have to save your files as text (file extension: txt), and then escape the html before textilizing:

---
layout: default
title: Snarky little Ewok
---

This file's extension is .txt

A little Ewok is sometimes referred too as <h3>. But pappa Ewok is called <h1> - if you know what's good for you.

Then on the default.html layout, instead of having:

{{ page.content }}

You would have this:

{{ page.content | xml_escape | textilize }}

Since there's no 'markdownify' filter on Jekyll yet, you can't do that with markdown. There's an issue (Issue 134) on Jekyll for adding a markdownify filter.

EDIT:

It's now possible to use markdown (since jekyll 0.10.1)

{{ page.content | xml_escape | markdownify }}