Rendering unicode characters in Markdown from Emac

2019-07-10 09:49发布

问题:

I have a Markdown text file, utf-8-encoded, that has some non-ASCII characters such as . I couldn't get the reference Perl implementation to handle these characters correctly, but I can get it work with Pandoc. I'd like to be able to render my Markdown file straight from Emacs, using C-c C-c p from Markdown mode, and that's still not working for me. I get what looks like a blank space instead of the non-ASCII character. For example,

I love apostrophe’s.

turns into

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta http-equiv="Content-Style-Type" content="text/css" />
  <meta name="generator" content="pandoc" />
  <title></title>
  <style type="text/css">code{white-space: pre;}</style>
</head>
<body>
<p>I love apostrophe s.</p>
</body>
</html>

Note that the HTML contains charset=utf-8. It's nearly identical to what I get running Pandoc from the command line, except for the missing apostrophe. I'm invoking Pandoc, whether from Emacs or the command prompt, using pandoc -f markdown -t html -s --mathjax --highlight-style pygments, which I got from here.

Can I get the apostrophe and other unicode characters to render properly from Emacs?

EDIT: using the C-u C-x = command that @db48x suggested, I verified that the blank character is a regular space (#x20)

回答1:

I had the same problem with German Umlaut characters and figured out a solution: If I add the line

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

to the beginning of the markdown file it ends up in the html-file and, although embedded in <p></p> tags, works. C-c C-c e exports nicely my Umlauts and your apostrophe’s, too.



回答2:

To avoid typing <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> in the header of all your markdown files (which is not straightforward), you can tell the markdown mode of emacs to do this for you at compilation. You just have to customize the Markdown Xhtml Header Content variable and setting it to <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />.

You juste need to run M-x customize-mode, look for Markdown Xhtml Header Content and save with its new value.



回答3:

Running M-x customize-variable and toggling markdown-command-needs-filename to on solved the problem for me.