-
Notifications
You must be signed in to change notification settings - Fork 81
/
main.go
47 lines (42 loc) · 965 Bytes
/
main.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
// Mdbook-mermaid is a helper meant to slip-stream mermaid diagrams.
//
// The diagram will get built and slip-streamed where there's a codeblock of the
// "mermaid" type. For example:
//
// ```mermaid
// graph LR
// x --> y
// ```
package main
import (
"context"
"regexp"
"strings"
"github.com/quay/claircore/internal/mdbook"
)
var (
marker = regexp.MustCompile("(?sUm)^```mermaid$.*^```$")
repl = strings.NewReplacer(
"```mermaid", `<pre class="mermaid">`,
"```", "</pre>",
"<", "<",
">", ">",
`"`, """,
"&", "&",
)
)
func main() {
mdbook.Main("mermaid", newProc)
}
func newProc(ctx context.Context, cfg *mdbook.Context) (*mdbook.Proc, error) {
proc := mdbook.Proc{
Chapter: func(ctx context.Context, b *strings.Builder, c *mdbook.Chapter) error {
c.Content = marker.ReplaceAllStringFunc(c.Content, func(m string) string {
out := repl.Replace(m)
return out
})
return nil
},
}
return &proc, nil
}