1// Package markup is an umbrella package for all markups and their renderers.
2package markup
3
4import (
5 "bytes"
6 "fmt"
7
8 "github.com/yuin/goldmark"
9 "github.com/yuin/goldmark/ast"
10 "github.com/yuin/goldmark/extension"
11 "github.com/yuin/goldmark/parser"
12 "github.com/yuin/goldmark/text"
13 "github.com/yuin/goldmark/util"
14)
15
16func RenderMarkdown(source string) string {
17 md := goldmark.New(
18 goldmark.WithExtensions(extension.GFM),
19 goldmark.WithParserOptions(
20 parser.WithAutoHeadingID(),
21 ),
22 )
23 var buf bytes.Buffer
24 if err := md.Convert([]byte(source), &buf); err != nil {
25 return source
26 }
27 return buf.String()
28}
29
30type RelativeLinkTransformer struct {
31 User string
32 Repo string
33 Ref string
34}
35
36func (t *RelativeLinkTransformer) Transform(node *ast.Document, reader text.Reader, pc parser.Context) {
37 ast.Walk(node, func(n ast.Node, entering bool) (ast.WalkStatus, error) {
38 if !entering {
39 fmt.Printf("Node: %T\n", n)
40 }
41 return ast.WalkContinue, nil
42 })
43}
44
45func RenderMarkdownExtended(source, user, repo, ref string) string {
46 md := goldmark.New(
47 goldmark.WithExtensions(extension.GFM),
48 goldmark.WithParserOptions(parser.WithAutoHeadingID()),
49 goldmark.WithParser(
50 parser.NewParser(
51 parser.WithASTTransformers(
52 util.Prioritized(&RelativeLinkTransformer{
53 User: user,
54 Repo: repo,
55 Ref: ref,
56 }, 999),
57 ),
58 ),
59 ),
60 )
61
62 var buf bytes.Buffer
63 if err := md.Convert([]byte(source), &buf); err != nil {
64 return source
65 }
66 return buf.String()
67}