/
markdown.go
197 lines (182 loc) · 5.17 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
package widget
import (
"io"
"net/url"
"strings"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/renderer"
"github.com/Jaeel/fyne"
)
// 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":
link, _ := url.Parse(string(n.(*ast.Link).Destination))
m.nextSeg = &HyperlinkSegment{fyne.TextAlignLeading, "", 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":
trimmed := string(n.Text(source))
trimmed = strings.ReplaceAll(trimmed, "\n", " ") // newline inside paragraph is not newline
if trimmed == "" {
return ast.WalkContinue, nil
}
if t, ok := m.nextSeg.(*TextSegment); ok {
next := n.(*ast.Text).NextSibling()
if next != nil {
if nextText, ok := next.(*ast.Text); ok {
if nextText.Segment.Start > n.(*ast.Text).Segment.Stop { // detect presence of a trailing newline
trimmed = trimmed + " "
}
}
}
t.Text = t.Text + trimmed
}
if link, ok := m.nextSeg.(*HyperlinkSegment); ok {
link.Text = link.Text + trimmed
}
if !m.heading {
m.segs = append(m.segs, m.nextSeg)
}
case "Blockquote":
m.blockquote = true
}
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 = RichTextStyleParagraph
}
}
m.nextSeg = &TextSegment{
Style: RichTextStyleInline,
}
}
return 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
}