forked from gomarkdown/markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic.go
50 lines (36 loc) · 1.04 KB
/
basic.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
// example for https://blog.kowalczyk.info/article/cxn3/advanced-markdown-processing-in-go.html
import (
"os"
"github.com/panhongrainbow/markdown"
"github.com/panhongrainbow/markdown/ast"
"github.com/panhongrainbow/markdown/html"
"github.com/panhongrainbow/markdown/parser"
"fmt"
)
var mds = `# header
Sample text.
[link](http://example.com)
`
var printAst = false
func mdToHTML(md []byte) []byte {
// create markdown parser with extensions
extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock
p := parser.NewWithExtensions(extensions)
doc := p.Parse(md)
if printAst {
fmt.Print("--- AST tree:\n")
ast.Print(os.Stdout, doc)
fmt.Print("\n")
}
// create HTML renderer with extensions
htmlFlags := html.CommonFlags | html.HrefTargetBlank
opts := html.RendererOptions{Flags: htmlFlags}
renderer := html.NewRenderer(opts)
return markdown.Render(doc, renderer)
}
func main() {
md := []byte(mds)
html := mdToHTML(md)
fmt.Printf("--- Markdown:\n%s\n\n--- HTML:\n%s\n", md, html)
}