forked from fyne-io/fyne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
markdown.go
222 lines (204 loc) · 5.8 KB
/
markdown.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
package widget
import (
"io"
"net/url"
"strings"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/renderer"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/storage"
)
// NewRichTextFromMarkdown configures a RichText widget by parsing the provided markdown content.
//
// Since: 2.1
func NewRichTextFromMarkdown(content string) *RichText {
return NewRichText(parseMarkdown(content)...)
}
// ParseMarkdown allows setting the content of this RichText widget from a markdown string.
// It will replace the content of this widget similarly to SetText, but with the appropriate formatting.
func (t *RichText) ParseMarkdown(content string) {
t.Segments = parseMarkdown(content)
t.Refresh()
}
type markdownRenderer struct {
blockquote bool
heading bool
nextSeg RichTextSegment
parentStack [][]RichTextSegment
segs []RichTextSegment
}
func (m *markdownRenderer) AddOptions(...renderer.Option) {}
func (m *markdownRenderer) Render(_ io.Writer, source []byte, n ast.Node) error {
m.nextSeg = &TextSegment{}
err := ast.Walk(n, func(n ast.Node, entering bool) (ast.WalkStatus, error) {
if !entering {
if n.Kind().String() == "Heading" {
m.segs = append(m.segs, m.nextSeg)
m.heading = false
}
return ast.WalkContinue, m.handleExitNode(n)
}
switch n.Kind().String() {
case "List":
// prepare a new child level
m.parentStack = append(m.parentStack, m.segs)
m.segs = nil
case "ListItem":
// prepare a new item level
m.parentStack = append(m.parentStack, m.segs)
m.segs = nil
case "Heading":
m.heading = true
switch n.(*ast.Heading).Level {
case 1:
m.nextSeg = &TextSegment{
Style: RichTextStyleHeading,
}
case 2:
m.nextSeg = &TextSegment{
Style: RichTextStyleSubHeading,
}
default:
m.nextSeg = &TextSegment{
Style: RichTextStyleParagraph,
}
m.nextSeg.(*TextSegment).Style.TextStyle.Bold = true
}
case "HorizontalRule", "ThematicBreak":
m.segs = append(m.segs, &SeparatorSegment{})
case "Link":
m.nextSeg = makeLink(n.(*ast.Link))
case "Paragraph":
m.nextSeg = &TextSegment{
Style: RichTextStyleInline, // we make it a paragraph at the end if there are no more elements
}
if m.blockquote {
m.nextSeg.(*TextSegment).Style = RichTextStyleBlockquote
}
case "CodeSpan":
m.nextSeg = &TextSegment{
Style: RichTextStyleCodeInline,
}
case "CodeBlock", "FencedCodeBlock":
var data []byte
lines := n.Lines()
for i := 0; i < lines.Len(); i++ {
line := lines.At(i)
data = append(data, line.Value(source)...)
}
if len(data) == 0 {
return ast.WalkContinue, nil
}
if data[len(data)-1] == '\n' {
data = data[:len(data)-1]
}
m.segs = append(m.segs, &TextSegment{
Style: RichTextStyleCodeBlock,
Text: string(data),
})
case "Emph", "Emphasis":
switch n.(*ast.Emphasis).Level {
case 2:
m.nextSeg = &TextSegment{
Style: RichTextStyleStrong,
}
default:
m.nextSeg = &TextSegment{
Style: RichTextStyleEmphasis,
}
}
case "Strong":
m.nextSeg = &TextSegment{
Style: RichTextStyleStrong,
}
case "Text":
ret := addTextToSegment(string(n.Text(source)), m.nextSeg, n)
if ret != 0 {
return ret, nil
}
_, isImage := m.nextSeg.(*ImageSegment)
if !m.heading && !isImage {
m.segs = append(m.segs, m.nextSeg)
}
case "Blockquote":
m.blockquote = true
case "Image":
m.nextSeg = makeImage(n.(*ast.Image)) // remember this for applying title
m.segs = append(m.segs, m.nextSeg)
}
return ast.WalkContinue, nil
})
return err
}
func (m *markdownRenderer) handleExitNode(n ast.Node) error {
if n.Kind().String() == "Blockquote" {
m.blockquote = false
} else if n.Kind().String() == "List" {
listSegs := m.segs
m.segs = m.parentStack[len(m.parentStack)-1]
m.parentStack = m.parentStack[:len(m.parentStack)-1]
marker := n.(*ast.List).Marker
m.segs = append(m.segs, &ListSegment{Items: listSegs, Ordered: marker != '*' && marker != '-' && marker != '+'})
} else if n.Kind().String() == "ListItem" {
itemSegs := m.segs
m.segs = m.parentStack[len(m.parentStack)-1]
m.parentStack = m.parentStack[:len(m.parentStack)-1]
m.segs = append(m.segs, &ParagraphSegment{Texts: itemSegs})
} else if !m.blockquote && !m.heading {
if len(m.segs) > 0 {
if text, ok := m.segs[len(m.segs)-1].(*TextSegment); ok && n.Kind().String() == "Paragraph" {
text.Style.Inline = false
}
}
m.nextSeg = &TextSegment{
Style: RichTextStyleInline,
}
}
return nil
}
func addTextToSegment(text string, s RichTextSegment, node ast.Node) ast.WalkStatus {
trimmed := strings.ReplaceAll(text, "\n", " ") // newline inside paragraph is not newline
if trimmed == "" {
return ast.WalkContinue
}
if t, ok := s.(*TextSegment); ok {
next := node.(*ast.Text).NextSibling()
if next != nil {
if nextText, ok := next.(*ast.Text); ok {
if nextText.Segment.Start > node.(*ast.Text).Segment.Stop { // detect presence of a trailing newline
trimmed = trimmed + " "
}
}
}
t.Text = t.Text + trimmed
}
if link, ok := s.(*HyperlinkSegment); ok {
link.Text = link.Text + trimmed
}
return 0
}
func makeImage(n *ast.Image) *ImageSegment {
dest := string(n.Destination)
u, err := storage.ParseURI(dest)
if err != nil {
u = storage.NewFileURI(dest)
}
return &ImageSegment{Source: u, Title: string(n.Title), Alignment: fyne.TextAlignCenter}
}
func makeLink(n *ast.Link) *HyperlinkSegment {
link, _ := url.Parse(string(n.Destination))
return &HyperlinkSegment{fyne.TextAlignLeading, "", link, nil}
}
func parseMarkdown(content string) []RichTextSegment {
r := &markdownRenderer{}
if content == "" {
return r.segs
}
md := goldmark.New(goldmark.WithRenderer(r))
err := md.Convert([]byte(content), nil)
if err != nil {
fyne.LogError("Failed to parse markdown", err)
}
return r.segs
}