-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrender.go
94 lines (86 loc) · 2.19 KB
/
render.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package ukuleleweb
import (
"bytes"
"sort"
"strings"
pikchr "github.com/gopikchr/goldmark-pikchr"
"github.com/peterbourgon/diskv/v3"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer/html"
"github.com/yuin/goldmark/text"
)
var gmark = goldmark.New(
goldmark.WithExtensions(
extension.GFM,
extension.Typographer,
WikiLinkExt,
&ShortLinkExt{},
&pikchr.Extender{},
),
goldmark.WithParserOptions(
parser.WithAttribute(),
),
goldmark.WithRendererOptions(html.WithUnsafe()),
)
func RenderHTML(md string) (string, error) {
var buf bytes.Buffer
if err := gmark.Convert([]byte(md), &buf); err != nil {
return "", err
}
return string(buf.Bytes()), nil
}
// OutgoingLinks returns the outgoing wiki links in a given Markdown input.
// The outgoing links are a map of page names to true.
func OutgoingLinks(md string) map[string]bool {
found := make(map[string]bool)
reader := text.NewReader([]byte(md))
doc := gmark.Parser().Parse(reader)
ast.Walk(doc, func(n ast.Node, entering bool) (ast.WalkStatus, error) {
if !entering {
return ast.WalkContinue, nil
}
l, ok := n.(*ast.Link)
if !ok {
return ast.WalkContinue, nil
}
URL := string(l.Destination)
if strings.HasPrefix(URL, "/") {
found[URL[1:]] = true
}
return ast.WalkContinue, nil
})
return found
}
// AllReverseLinks calculates the reverse link map for the whole wiki.
// The returned map maps page names to a list of pages linking to it.
// Sets of pages are represented as sorted lists.
func AllReverseLinks(d *diskv.Diskv) map[string][]string {
revLinks := make(map[string]map[string]bool)
for p := range d.Keys(nil) {
pOut := OutgoingLinks(d.ReadString(p))
for q := range pOut {
qIn, ok := revLinks[q]
if !ok {
qIn = make(map[string]bool)
revLinks[q] = qIn
}
qIn[p] = true
}
}
revLinksSorted := make(map[string][]string)
for p, s := range revLinks {
revLinksSorted[p] = sortedStringSlice(s)
}
return revLinksSorted
}
func sortedStringSlice(a map[string]bool) []string {
var res []string
for k := range a {
res = append(res, k)
}
sort.Strings(res)
return res
}