aboutsummaryrefslogtreecommitdiff
path: root/markdown/ast_callout.go
blob: d538a09fd3d7e1baa6662600df8299de9d6f62ab (plain)
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 markdown

import (
	"errors"
	"html/template"
	"strings"

	"git.anhgelus.world/anhgelus/small-web/dom"
)

var (
	ErrInvalidCallout = errors.New("invalid callout")
)

type astCallout struct {
	kind    string
	title   *astParagraph
	content []*astParagraph
}

func (a *astCallout) Eval(opt *Option) (template.HTML, *ParseError) {
	inner := dom.NewContentElement("div", make([]dom.Element, 0))
	for _, c := range a.content {
		ct, err := c.Eval(opt)
		if err != nil {
			return "", err
		}
		inner.Contents = append(inner.Contents, dom.NewParagraph(
			template.HTML(strings.TrimSpace(string(ct))),
		))
	}

	titleContent, err := a.title.Eval(opt)
	if err != nil {
		return "", err
	}
	titleContent = template.HTML(strings.TrimSpace(string(titleContent)))
	if len(titleContent) == 0 {
		titleContent = template.HTML(a.kind)
	}
	title := dom.NewLiteralContentElement("h4", titleContent)

	callout := dom.NewContentElement("div", make([]dom.Element, 0))
	callout.Contents = append(callout.Contents, title)
	if len(inner.Contents) > 0 {
		callout.Contents = append(callout.Contents, inner)
	}
	callout.SetAttribute("data-kind", a.kind)
	callout.ClassList().Add("callout")
	return callout.Render(), nil
}

func callout(lxs *lexers) (block, *ParseError) {
	callout := new(astCallout)
	if lxs.Current().Value != "[!" {
		return paragraph(lxs, false)
	}
	if !lxs.Next() {
		return nil, &ParseError{lxs: *lxs, internal: ErrInvalidCallout}
	}
	callout.kind = strings.ToLower(lxs.Current().Value)
	if !lxs.Next() || lxs.Current().Type != lexerCallout || lxs.Current().Value != "]" {
		return nil, &ParseError{lxs: *lxs, internal: ErrInvalidCallout}
	}
	var err *ParseError
	callout.title, err = paragraph(lxs, true)
	if err != nil {
		return nil, err
	}
	n := 0
	for lxs.Next() && n < 2 {
		current := lxs.Current()
		n = 0
		switch current.Type {
		case lexerBreak:
			n = len(current.Value)
		case lexerQuote:
		case lexerLiteral, lexerModifier, lexerCode, lexerExternal:
			p, err := paragraph(lxs, true)
			if err != nil {
				return nil, err
			}
			lxs.Before()
			callout.content = append(callout.content, p)
			n++
		default:
			// because the code did not use it
			lxs.Before()
			return callout, nil
		}
	}
	lxs.Before()
	return callout, nil
}