Golang で Markdown 表記を扱う

環境

  • Golang 1.14.x

Github

サンプルコード

go.mod

module github.com/psychedelicnekopunch/xxxxx

go 1.14

require (
	gopkg.in/russross/blackfriday.v2 v2.0.1
)

replace gopkg.in/russross/blackfriday.v2 => github.com/russross/blackfriday/v2 v2.0.1
# ライブラリ整理
$ go mod tidy

main.go

package main

import (
	"fmt"
	"html/template"

	"github.com/microcosm-cc/bluemonday"
	"gopkg.in/russross/blackfriday.v2"
)

func main() {

	s := `
# title

* list1
* list2


## title2

this is markdown.
hello, world.
`

	output := blackfriday.Run([]byte(s), blackfriday.WithExtensions(blackfriday.HardLineBreak))
	html := bluemonday.UGCPolicy().SanitizeBytes(output)

	fmt.Print(template.HTML(string(html)))
}