Retain HTML Entities When Parsing Markdown

2019-05-26 20:07发布

I have exhausted my research and tried many methods to no effect and am hoping I'm overlooking some kind of simple solution:

I am using a Jekyll site to produce HTML files for e-mail and need to use HTML entities for special characters (such as em-dashes and smart quotes) in order to avoid improper symbol interpretation (the content-type/charset is at times stripped out of the e-mail head due to complexities I won't get into here.)

My problem is that parsing my Markdown appears to convert all of my written HTML entities into the special characters when output as HTML, and I am unable to escape with common methods. When I type &rsquo; into markdown to produce a right curly quote it is converted to the symbol within my HTML (instead of retaining &rsquo; in my HTML). If I try to escape it with back-ticks it will not convert &rsquo in the HTML but it places it within <code> tags which cause it to render as &rsquo; and not . Is there a way to retain typed-out HTML entities for special characters or -- even better -- convert special characters into to HTML entities (in the HTML) when parsed?

I am using the Kramdown markdown parser with Jekyll. I have even gone as far as specifying the entity_output option in Kramdown to : as_input without success. Any help is much appreciated!

2条回答
再贱就再见
2楼-- · 2019-05-26 20:12

As the docs state:

    --entity-output ARG
     Defines how entities are output

     The possible values are :as_input (entities are output in the same
     form as found in the input), :numeric (entities are output in numeric
     form), :symbolic (entities are output in symbolic form if possible) or
     :as_char (entities are output as characters if possible, only available
     on Ruby 1.9).

     Default: :as_char
     Used by: HTML converter, kramdown converter

So lets try those options:

$ kramdown --version
1.11.1 
$ kramdown
&lsquo;foo&rsquo;
<p>‘foo’</p>
$ kramdown --entity-output=as_input
&lsquo;foo&rsquo;
<p>&lsquo;foo&rsquo;</p>
$ kramdown --entity-output=symbolic
&lsquo;foo&rsquo;
<p>&lsquo;foo&rsquo;</p>
$ kramdown --entity-output=numeric
&lsquo;foo&rsquo;
<p>&#8216;foo&#8217;</p>
$ kramdown --entity-output=as_char
&lsquo;foo&rsquo;
<p>‘foo’</p>
$ ruby --version
ruby 1.9.3p484 (2013-11-22 revision 43786) [x86_64-linux]

Notice that I have Kramdown version 1.11 and Ruby version 1.9. if you have earlier versions, then things may not work properly.

查看更多
forever°为你锁心
3楼-- · 2019-05-26 20:27

In your _config.yml you can configure kramdown to leave html entities as they are written in your code with :

kramdown:
  entity_output: :as_input

See documentation.

查看更多
登录 后发表回答