One minute
Go解析markdown生成html在线访问
go安装部分库, 代码
package main
import (
"html/template"
"io/ioutil"
"net/http"
"github.com/gin-gonic/gin"
"github.com/microcosm-cc/bluemonday"
"github.com/russross/blackfriday"
)
func main() {
r := gin.Default()
r.LoadHTMLGlob("html/*")
r.GET("/index", func(c *gin.Context) {
// input := []byte("### lijinquan.com是个不错的网站")
input, err := ioutil.ReadFile("./test.md")
if err != nil {
panic(err)
}
unsafe := blackfriday.MarkdownCommon(input)
content := bluemonday.UGCPolicy().SanitizeBytes(unsafe)
// fmt.Println(string(content))
c.HTML(http.StatusOK, "md.html", gin.H{"title": "我是测试", "content": template.HTML(string(content))})
})
r.Run()
}
Html/md.html
<!doctype html>
<html>
<head>
<meta charset='UTF-8'><meta name='viewport' content='width=device-width initial-scale=1'>
<title>{{.title}}</title>
</head>
<body >
<div class=''>{{.content}}</div>
</body>
<link href="https://cdn.bootcdn.net/ajax/libs/highlight.js/11.1.0/styles/base16/default-light.min.css" rel="stylesheet">
<script src="https://cdn.bootcdn.net/ajax/libs/highlight.js/11.1.0/highlight.min.js"></script>
<script>
hljs.highlightAll();
</script>
</html>