我创建了这个非常简单的程序来进行测试。
package main
import (
"fmt"
"github.com/microcosm-cc/bluemonday"
"github.com/pressly/chi"
"github.com/russross/blackfriday"
"github.com/unrolled/render"
"net/http"
)
func main() {
r := chi.NewRouter()
r.Get("/", homepageGET)
http.ListenAndServe(":8080", r)
}
func homepageGET(w http.ResponseWriter, r *http.Request) {
Renderer := render.New(render.Options{
Directory: "frontend",
Extensions: []string{".tmpl", ".html"},
UnEscapeHTML: true,
})
unsafe := blackfriday.MarkdownCommon([]byte("**bolded text**"))
markdownContent := bluemonday.UGCPolicy().SanitizeBytes(unsafe)
fmt.Print(string(markdownContent))
Renderer.HTML(w, http.StatusOK, "index", map[string]interface{}{
"content": fmt.Sprintf(string(markdownContent))})
}
然后我有一个 HTML 文件,除此之外什么都不包含:
<body>
{{ .content }}
</body>
fmt.Print 命令打印“<p><strong>bolded text</strong></p>
”,而它被插入到 HTML 页面中为:“<p><strong>bolded text</strong></p>
".
我相信它与转义的 HTML 有关,但对于展开/渲染包,我将其配置为未转义。我非常感谢任何帮助让测试程序正常工作(最好与展开/渲染一起)。
在 Go 中,您可以将已知的安全 html 字符串转换为模板.HTML https://golang.org/pkg/html/template/#HTML类型,并且由于展开/渲染 https://github.com/unrolled/render使用 Go 的html/template
要渲染 html,您应该能够使用它。
Renderer.HTML(w, http.StatusOK, "index", map[string]interface{}{
"content": template.HTML(markdownContent),
})
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)