-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmd_admonition.go
300 lines (268 loc) · 7.67 KB
/
md_admonition.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package website
import (
"bytes"
"context"
"fmt"
"log/slog"
"github.com/a-h/templ"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer"
"github.com/yuin/goldmark/renderer/html"
"github.com/yuin/goldmark/text"
"github.com/yuin/goldmark/util"
)
var _ goldmark.Extender = (*admonitionExt)(nil)
// admonitionExt is a Goldmark extension for admonitions.
type admonitionExt struct{}
func (e *admonitionExt) Extend(m goldmark.Markdown) {
rendr := renderer.NewRenderer(
html.WithHardWraps(),
// Allow raw HTML coming from the post markdown.
html.WithUnsafe(),
renderer.WithNodeRenderers(
util.Prioritized(html.NewRenderer(), 1000),
util.Prioritized(&emptyNodeRenderer{
nodeKinds: []ast.NodeKind{admonitionNodeKind},
}, 0),
),
)
m.Parser().AddOptions(parser.WithASTTransformers(
util.Prioritized(&admonitionTransformer{}, 0),
))
m.Renderer().AddOptions(renderer.WithNodeRenderers(
util.Prioritized(&admonitionRenderer{
Renderer: rendr,
}, 0),
))
}
type admonitionLevel string
const (
AdmonitionTypeNote admonitionLevel = "NOTE"
AdmonitionTypeTip admonitionLevel = "TIP"
AdmonitionTypeImportant admonitionLevel = "IMPORTANT"
AdmonitionTypeWarning admonitionLevel = "WARNING"
AdmonitionTypeCaution admonitionLevel = "CAUTION"
)
func (a admonitionLevel) Title() (string, error) {
switch a {
case AdmonitionTypeNote:
return "Note", nil
case AdmonitionTypeTip:
return "Tip", nil
case AdmonitionTypeImportant:
return "Important", nil
case AdmonitionTypeWarning:
return "Warning", nil
case AdmonitionTypeCaution:
return "Caution", nil
default:
return "", fmt.Errorf("unknown admonition type: %s", a)
}
}
var _ parser.ASTTransformer = (*admonitionTransformer)(nil)
// admonitionTransformer transforms blockquotes into admonitions.
type admonitionTransformer struct{}
func (*admonitionTransformer) Transform(
node *ast.Document,
reader text.Reader,
pc parser.Context,
) {
// You cannot remove a node that is being visited by the walker.
// Hence, store the blockquotes which should be transformed into admonitions
// so that once the walker has finished we can remove them.
blockquotesToRemove := []*ast.Blockquote{}
if err := ast.Walk(node, func(n ast.Node, entering bool) (ast.WalkStatus, error) {
if !entering {
return ast.WalkContinue, nil
}
bq, ok := n.(*ast.Blockquote)
if !ok {
return ast.WalkContinue, nil
}
if !bq.HasChildren() {
return ast.WalkContinue, nil
}
// Check first child is a paragraph.
p1, ok := bq.FirstChild().(*ast.Paragraph)
if !ok {
return ast.WalkContinue, nil
}
index := 0
isAdmonition := false
adLevel := []byte{}
remainder := []ast.Node{}
for child := p1.FirstChild(); child != nil; child = child.NextSibling() {
text := child.Text(reader.Source())
switch index {
case 0:
if child.Kind() != ast.KindText {
return ast.WalkContinue, nil
}
if !bytes.Equal(text, []byte("[")) {
return ast.WalkContinue, nil
}
case 1:
if child.Kind() != ast.KindText {
return ast.WalkContinue, nil
}
if text[0] != '!' {
return ast.WalkContinue, nil
}
adLevel = text[1:]
case 2:
if child.Kind() != ast.KindText {
return ast.WalkContinue, nil
}
if !bytes.Equal(text, []byte("]")) {
return ast.WalkContinue, nil
}
isAdmonition = true
default:
remainder = append(remainder, child)
}
index++
}
if !isAdmonition {
return ast.WalkContinue, nil
}
admonition := &admonitionBlock{
AdType: admonitionLevel(adLevel),
}
if len(remainder) > 0 {
p := ast.NewParagraph()
for _, child := range remainder {
p.AppendChild(p, child)
}
admonition.AppendChild(admonition, p)
}
bq.RemoveChild(bq, p1)
// Get remaining children of blockquote.
// We cannot append the children directly to the aside because then the
// NextSibling does not exist.
// Instead, first add them to a slice and then append them to the aside
// from the slice.
children := make([]ast.Node, 0, bq.ChildCount())
for child := bq.FirstChild(); child != nil; child = child.NextSibling() {
children = append(children, child)
}
for _, child := range children {
admonition.AppendChild(admonition, child)
}
// Insert the admonition before the blockquote and add blockquote to
// list to be removed from the ast later.
bq.Parent().InsertBefore(bq.Parent(), bq, admonition)
blockquotesToRemove = append(blockquotesToRemove, bq)
return ast.WalkSkipChildren, nil
}); err != nil {
slog.Error("transforming admonition", "error", err)
return
}
// After tree has been walked, remove the blockquotes.
for _, bq := range blockquotesToRemove {
bq.Parent().RemoveChild(bq.Parent(), bq)
}
}
// admonitionNodeKind is a NodeKind for admonition nodes.
var admonitionNodeKind = ast.NewNodeKind("Admonition")
var _ ast.Node = (*admonitionBlock)(nil)
// admonitionBlock is a block node for an admonition.
type admonitionBlock struct {
ast.BaseBlock
Title string
AdType admonitionLevel
}
// Dump implements ast.Node.
func (a *admonitionBlock) Dump(source []byte, level int) {
ast.DumpHelper(a, source, level, nil, nil)
}
// Kind implements ast.Node.
func (*admonitionBlock) Kind() ast.NodeKind {
return admonitionNodeKind
}
var _ renderer.NodeRenderer = (*admonitionRenderer)(nil)
// admonitionRenderer renders an Admonition node to HTML.
type admonitionRenderer struct {
Renderer renderer.Renderer
}
func (r *admonitionRenderer) RegisterFuncs(
reg renderer.NodeRendererFuncRegisterer,
) {
reg.Register(admonitionNodeKind, r.renderAside)
}
func (r *admonitionRenderer) renderAside(
writer util.BufWriter,
source []byte,
n ast.Node,
entering bool,
) (ast.WalkStatus, error) {
if !entering {
return ast.WalkContinue, nil
}
aside := n.(*admonitionBlock)
title := aside.Title
if title == "" {
var err error
title, err = aside.AdType.Title()
if err != nil {
return ast.WalkStop, fmt.Errorf(
"getting admonition title: %w",
err,
)
}
}
buf := bytes.Buffer{}
if err := r.Renderer.Render(&buf, source, aside); err != nil {
return ast.WalkStop, fmt.Errorf("rendering aside: %w", err)
}
var render templ.ComponentFunc
switch aside.AdType {
case AdmonitionTypeNote:
render = admonitionNote(title, buf.Bytes()).Render
case AdmonitionTypeTip:
render = admonitionTip(title, buf.Bytes()).Render
case AdmonitionTypeImportant:
render = admonitionImportant(title, buf.Bytes()).Render
case AdmonitionTypeWarning:
render = admonitionWarning(title, buf.Bytes()).Render
case AdmonitionTypeCaution:
render = admonitionCaution(title, buf.Bytes()).Render
default:
return ast.WalkStop, fmt.Errorf(
"unknown admonition type: %s",
aside.AdType,
)
}
if err := render(context.TODO(), writer); err != nil {
return ast.WalkStop, fmt.Errorf("rendering aside: %w", err)
}
return ast.WalkSkipChildren, nil
}
var _ renderer.NodeRenderer = (*emptyNodeRenderer)(nil)
// emptyNodeRenderer renders the body of nodes to nothing.
// It is used by a rendered so that certain nodes are allowed in the AST, but
// they do not produce any HTML.
// This is because the bodies of such nodes are given to something like templ
// which produces the final HTML.
//
// It is a bit weird, but the best/easiest way we came up with to combine Templ
// and Goldmark.
type emptyNodeRenderer struct {
nodeKinds []ast.NodeKind
}
func (r *emptyNodeRenderer) RegisterFuncs(
reg renderer.NodeRendererFuncRegisterer,
) {
for _, kind := range r.nodeKinds {
reg.Register(kind, r.renderNothing)
}
}
func (*emptyNodeRenderer) renderNothing(
writer util.BufWriter,
source []byte,
n ast.Node,
entering bool,
) (ast.WalkStatus, error) {
return ast.WalkContinue, nil
}