-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmd_anchor.go
69 lines (60 loc) · 1.8 KB
/
md_anchor.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
package website
import (
"fmt"
"log/slog"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/text"
"github.com/yuin/goldmark/util"
)
var _ goldmark.Extender = (*anchorExt)(nil)
// anchorExt is a Goldmark extension for adding anchor links to headings.
type anchorExt struct{}
func (e *anchorExt) Extend(m goldmark.Markdown) {
m.Parser().AddOptions(
parser.WithASTTransformers(
util.Prioritized(&anchorTransformer{}, 0),
),
)
}
var _ parser.ASTTransformer = (*readingTimeTransformer)(nil)
// anchorTransformer is a Goldmark AST transformer that adds anchor links to
// headings.
type anchorTransformer struct{}
// Transform implements parser.ASTTransformer.
func (*anchorTransformer) Transform(
node *ast.Document,
reader text.Reader,
pc parser.Context,
) {
if err := ast.Walk(node, func(n ast.Node, entering bool) (ast.WalkStatus, error) {
if !entering {
return ast.WalkContinue, nil
}
if n.Kind() == ast.KindHeading {
heading := n.(*ast.Heading)
rawID, ok := heading.AttributeString("id")
if !ok {
return ast.WalkStop, fmt.Errorf("no id found for heading %s",
heading.Text(reader.Source()))
}
id, ok := rawID.([]byte)
if !ok {
return ast.WalkStop, fmt.Errorf("id is not a string: %v", rawID)
}
str := ast.NewString([]byte("#"))
anchor := ast.NewLink()
anchor.Destination = append([]byte("#"), id...)
// IMPORTANT: Any classes used here need to be safelisted in
// the tailwind.config.cjs file.
anchor.SetAttributeString("class", []byte("ml-2 text-v-gray no-underline hover:text-v-lilac"))
anchor.AppendChild(anchor, str)
heading.AppendChild(heading, anchor)
}
return ast.WalkContinue, nil
}); err != nil {
slog.Error("adding anchor to header", "error", err)
return
}
}