如何使用blackfriday为golang模板(html或tmpl)渲染markdown?

时间:2021-06-02 06:45:19

I use the Martini framework,I have some markdown file and I want render it as HTML in tmpl/html template.

我使用Martini框架,我有一些markdown文件,我想在tmpl / html模板中将其渲染为HTML。

The markdown file like this:

像这样的降价文件:

title: A Test Demo
---
##ABC
> 123

And the template file like this:

和这样的模板文件:

<head>
  <title>{{name}}</title>
</head>

<body>
  <h2>{{abc}}</h2>
  <blockquote>
    <p>{{xyz}}</p>
  </blockquote>
</body>

I use the blackfriday parse the markdown and return []byte type,next step I wanna render the markdown file to this template and make each block to the right place,so how can I do this right way? Or use any way to do this better?

我使用blackfriday解析markdown并返回[]字节类型,下一步我想将markdown文件渲染到此模板并将每个块放到正确的位置,那么我该如何正确地执行此操作?或者用任何方式更好地做到这一点?

1 个解决方案

#1


20  

One way to achieve this is to use the Funcs method to add a custom function to the template function map. See the Functions section of the template package docs for more info.

实现此目的的一种方法是使用Funcs方法将自定义函数添加到模板函数映射。有关详细信息,请参阅模板包文档的“函数”部分。

Given a template file page.html, some writer w (probably an http.ResponseWriter), and some struct p with a field Body containing data to be put into a template field, you can do something like:

给定一个模板文件page.html,一些编写器w(可能是一个http.ResponseWriter),以及一些带有包含要放入模板字段的数据的Body的struct p,你可以这样做:

Define a function:

定义一个函数:

func markDowner(args ...interface{}) template.HTML {
    s := blackfriday.MarkdownCommon([]byte(fmt.Sprintf("%s", args...)))
    return template.HTML(s)
}

Add it to the template function map:

将其添加到模板功能图:

tmpl := template.Must(template.New("page.html").Funcs(template.FuncMap{"markDown": markDowner}).ParseFiles("page.html"))

Execute the template:

执行模板:

err := tmpl.ExecuteTemplate(w, "page.html", p)
if err != nil {
    http.Error(w, err.Error(), http.StatusInternalServerError)
}

Then, in your template file, you can put something like:

然后,在您的模板文件中,您可以输入以下内容:

{{.Body | markDown}}

And it will pass the Body through your markDowner function.

它将通过您的markDowner函数传递Body。

Playground

#1


20  

One way to achieve this is to use the Funcs method to add a custom function to the template function map. See the Functions section of the template package docs for more info.

实现此目的的一种方法是使用Funcs方法将自定义函数添加到模板函数映射。有关详细信息,请参阅模板包文档的“函数”部分。

Given a template file page.html, some writer w (probably an http.ResponseWriter), and some struct p with a field Body containing data to be put into a template field, you can do something like:

给定一个模板文件page.html,一些编写器w(可能是一个http.ResponseWriter),以及一些带有包含要放入模板字段的数据的Body的struct p,你可以这样做:

Define a function:

定义一个函数:

func markDowner(args ...interface{}) template.HTML {
    s := blackfriday.MarkdownCommon([]byte(fmt.Sprintf("%s", args...)))
    return template.HTML(s)
}

Add it to the template function map:

将其添加到模板功能图:

tmpl := template.Must(template.New("page.html").Funcs(template.FuncMap{"markDown": markDowner}).ParseFiles("page.html"))

Execute the template:

执行模板:

err := tmpl.ExecuteTemplate(w, "page.html", p)
if err != nil {
    http.Error(w, err.Error(), http.StatusInternalServerError)
}

Then, in your template file, you can put something like:

然后,在您的模板文件中,您可以输入以下内容:

{{.Body | markDown}}

And it will pass the Body through your markDowner function.

它将通过您的markDowner函数传递Body。

Playground