-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocument.go
More file actions
39 lines (30 loc) · 956 Bytes
/
document.go
File metadata and controls
39 lines (30 loc) · 956 Bytes
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
package main
import (
"regexp"
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/html"
"github.com/gomarkdown/markdown/parser"
)
type document struct {
path string
contents []byte
cache []byte
}
var linkRE = regexp.MustCompile(`(\[[^]]+\]\(\.\/[^)]+?)\.md(\))`)
func newDocument(path string, contents []byte) (*document, error) {
contents = []byte(linkRE.ReplaceAllString(string(contents), `$1$2`))
return &document{path: path, contents: contents}, nil
}
func (d *document) Render() ([]byte, error) {
if d.cache != nil {
return d.cache, nil
}
extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock
p := parser.NewWithExtensions(extensions)
doc := p.Parse(d.contents)
htmlFlags := html.CommonFlags | html.HrefTargetBlank
opts := html.RendererOptions{Flags: htmlFlags}
renderer := html.NewRenderer(opts)
d.cache = markdown.Render(doc, renderer)
return d.cache, nil
}