-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmermaid.go
More file actions
165 lines (126 loc) · 3.27 KB
/
mermaid.go
File metadata and controls
165 lines (126 loc) · 3.27 KB
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
package hype
import (
"bytes"
"context"
"encoding/json"
"fmt"
"html"
"strings"
"sync"
mermaidcmd "github.com/AlexanderGrooff/mermaid-ascii/cmd"
)
// mermaidMu protects concurrent access to the mermaid-ascii library
// which uses package-level globals during rendering.
var mermaidMu sync.Mutex
// Mermaid is a tag that renders Mermaid diagrams as ASCII art.
// It processes fenced code blocks with the "mermaid" language identifier
// and converts them to ASCII art using the mermaid-ascii library.
type Mermaid struct {
*Element
// Source is the original Mermaid diagram source
Source string
// Rendered is the ASCII art output
Rendered string
}
func (m *Mermaid) MarshalJSON() ([]byte, error) {
if m == nil {
return nil, ErrIsNil("mermaid")
}
m.RLock()
defer m.RUnlock()
mm, err := m.JSONMap()
if err != nil {
return nil, err
}
mm["type"] = toType(m)
if len(m.Source) > 0 {
mm["source"] = m.Source
}
if len(m.Rendered) > 0 {
mm["rendered"] = m.Rendered
}
return json.MarshalIndent(mm, "", " ")
}
// MD returns the markdown representation of the rendered Mermaid diagram.
func (m *Mermaid) MD() string {
if m == nil {
return ""
}
m.RLock()
defer m.RUnlock()
if len(m.Rendered) == 0 {
return ""
}
bb := &bytes.Buffer{}
fmt.Fprint(bb, "```\n")
fmt.Fprint(bb, m.Rendered)
fmt.Fprint(bb, "\n```")
return bb.String()
}
// String returns the HTML representation of the rendered Mermaid diagram.
// It returns just the code element content since the parent pre element
// is already provided by the markdown parser.
func (m *Mermaid) String() string {
if m == nil {
return ""
}
m.RLock()
defer m.RUnlock()
if len(m.Rendered) == 0 {
return ""
}
// Return just the code element - the parent pre is already in the DOM
return fmt.Sprintf("<code class=\"language-plain\">%s</code>",
html.EscapeString(m.Rendered))
}
// Execute renders the Mermaid diagram to ASCII art.
func (m *Mermaid) Execute(ctx context.Context, doc *Document) error {
if m == nil {
return ErrIsNil("mermaid")
}
if m.Element == nil {
return ErrIsNil("element")
}
if doc == nil {
return ErrIsNil("document")
}
m.Lock()
defer m.Unlock()
// Trim whitespace from source to avoid parsing issues
source := strings.TrimSpace(m.Source)
// Serialize access to mermaid-ascii library which uses package-level globals
mermaidMu.Lock()
output, err := mermaidcmd.RenderDiagram(source, nil)
mermaidMu.Unlock()
if err != nil {
return m.WrapErr(fmt.Errorf("failed to render mermaid diagram: %w", err))
}
m.Rendered = output
return nil
}
// NewMermaid creates a new Mermaid element from the given element.
func NewMermaid(el *Element) (*Mermaid, error) {
if el == nil {
return nil, ErrIsNil("element")
}
m := &Mermaid{
Element: el,
}
// Extract the mermaid source from the element's children (text content)
m.Source = html.UnescapeString(el.Children().String())
if len(strings.TrimSpace(m.Source)) == 0 {
return nil, m.WrapErr(fmt.Errorf("mermaid diagram source is empty"))
}
return m, nil
}
// NewMermaidNodes creates a new Mermaid node from a fenced code block.
func NewMermaidNodes(p *Parser, el *Element) (Nodes, error) {
if el == nil {
return nil, ErrIsNil("element")
}
m, err := NewMermaid(el)
if err != nil {
return nil, err
}
return Nodes{m}, nil
}