英文:
How can I render markdown to html with Blackfriday in Go?
问题
我有一个这样的结构体:
type Page struct {
Content string
}
然后我读取一个 Markdown 文件并将其赋值给一个变量:
data, err := ioutil.ReadFile("a.md")
lines := string(data)
page.Content = markdownRender([]byte(lines))
Markdown 文件的内容如下:
##Hello World
###Holo Go
然后我将其传递给 Markdown 渲染函数,并返回一个字符串值:
func markdownRender(content []byte) string {
htmlFlags := 0
htmlFlags |= blackfriday.HTML_USE_SMARTYPANTS
htmlFlags |= blackfriday.HTML_SMARTYPANTS_FRACTIONS
renderer := blackfriday.HtmlRenderer(htmlFlags, "", "")
extensions := 0
extensions |= blackfriday.EXTENSION_NO_INTRA_EMPHASIS
extensions |= blackfriday.EXTENSION_TABLES
extensions |= blackfriday.EXTENSION_FENCED_CODE
extensions |= blackfriday.EXTENSION_AUTOLINK
extensions |= blackfriday.EXTENSION_STRIKETHROUGH
extensions |= blackfriday.EXTENSION_SPACE_HEADERS
return string(blackfriday.Markdown(content, renderer, extensions))
}
最后,我在 HTML 模板中调用 page.Content
并生成一个静态 HTML:
{{.Content}}
但是在生成的 HTML 中,在浏览器中显示的是这样的(我在 Chrome 和 Safari 中尝试过)(不是源代码,而是显示在页面上):
<p>##Hello World ###Holo Go </p>
但我希望它显示为:
Hello World
Holo Go
那么,我该如何做到这一点?
英文:
I have a struct like this:
type Page struct {
Content string
}
then I read a markdown file and assign to a variable:
data, err := ioutil.ReadFile("a.md")
lines = string(data)
page.Content = markdownRender([]byte(lines))
The markdown file is like this:
##Hello World
###Holo Go
and then I put it into markdown render function and return a string value:
func markdownRender(content []byte) string {
htmlFlags := 0
htmlFlags |= blackfriday.HTML_USE_SMARTYPANTS
htmlFlags |= blackfriday.HTML_SMARTYPANTS_FRACTIONS
renderer := blackfriday.HtmlRenderer(htmlFlags, "", "")
extensions := 0
extensions |= blackfriday.EXTENSION_NO_INTRA_EMPHASIS
extensions |= blackfriday.EXTENSION_TABLES
extensions |= blackfriday.EXTENSION_FENCED_CODE
extensions |= blackfriday.EXTENSION_AUTOLINK
extensions |= blackfriday.EXTENSION_STRIKETHROUGH
extensions |= blackfriday.EXTENSION_SPACE_HEADERS
return string(blackfriday.Markdown(content, renderer, extensions))
}
and finally I call the page.Content
in a html template and generate a static html:
{{.Content}}
but in the generated html it shows in the browser (I tried it in the chrome and safari) is like this (not the source code, it just shows in the page):
<p>##Hello World ###Holo Go </p>
but I want it like this
Hello World
Holo Go
So, how can I do this?
答案1
得分: 3
首先,你的 Markdown 输入格式不正确——标题应该在 #
和文本之间有空格。你可以使用 blackfriday-tool 来验证这一点:
$ echo ##Hello | blackfriday-tool
<p>##Hello</p>
$ echo ## Hello | blackfriday-tool
<h2>Hello</h2>
其次,如果你将 blackfriday
的 HTML 输出传递给 html/template
,它会自动进行转义以确保安全。
如果你信任 Markdown 输入和 blackfriday 的 HTML 输出,你可以通过将其包装在 html/template
的 HTML
值中来告诉模板系统信任该内容:
type Page struct {
Content template.HTML
}
err = t.ExecuteTemplate(w, "page", Page{Content: template.HTML(s)})
可以参考 http://play.golang.org/p/eO7KDJMlb8 查看示例。
英文:
First, your markdown input is not quite right -- headings should have whitespace separating the #
s from the text. You can verify this using blackfriday-tool:
$ echo ##Hello | blackfriday-tool
<p>##Hello</p>
$ echo ## Hello | blackfriday-tool
<h2>Hello</h2>
Second, if you feed the HTML output from blackfriday
into a html/template
, it is going to be automatically escaped for safety.
If you trust the markdown input and blackfriday's HTML output, then you can tell the template system to trust the content by wrapping it in a html/template
HTML
value:
type Page struct {
Content template.HTML
}
err = t.ExecuteTemplate(w, "page", Page{Content: template.HTML(s)})
See http://play.golang.org/p/eO7KDJMlb8 for an example.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论