🌊
GolangでマークダウンテキストをHTML表記に変換
このライブラリを使用する
簡単な使い方
main.go
package main
import (
"fmt"
"html/template"
"github.com/microcosm-cc/bluemonday"
"github.com/russross/blackfriday/v2"
)
func main() {
s := `
# title
* list1
* list2
## title2
this is markdown.
hello, world.
https://github.com/
[GitHub](https://github.com/)
`
renderer := blackfriday.NewHTMLRenderer(blackfriday.HTMLRendererParameters{
Flags: blackfriday.HrefTargetBlank,
})
output := blackfriday.Run([]byte(s), blackfriday.WithExtensions(blackfriday.HardLineBreak + blackfriday.Autolink), blackfriday.WithRenderer(renderer))
html := bluemonday.UGCPolicy().SanitizeBytes(output)
fmt.Print(template.HTML(string(html)))
}
出力結果
<h1>title</h1>
<ul>
<li>list1<br>
</li>
<li>list2<br>
</li>
</ul>
<h2>title2</h2>
<p>this is markdown.<br>
hello, world.</p>
<p><a href="https://github.com/" rel="nofollow">https://github.com/</a></p>
<p><a href="https://github.com/" rel="nofollow">GitHub</a></p>
Discussion