-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmarkdown.go
290 lines (283 loc) · 8.44 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
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
package bbConvert
import (
"slices"
"strconv"
"strings"
"github.com/dlclark/regexp2"
)
type MarkdownConverter struct {
largeCodeConv *regexp2.Regexp
inlineCodeConv *regexp2.Regexp
blockQuoteConv *regexp2.Regexp
bAndIConv *regexp2.Regexp
bConv *regexp2.Regexp
surroundConv *regexp2.Regexp
linkImgConv *regexp2.Regexp
listConv *regexp2.Regexp
headingConv *regexp2.Regexp
//TODO: Tables???
}
func NewMarkdownConverter() MarkdownConverter {
largeCodeConv := regexp2.MustCompile("```([\\s\\S]*?)```", regexp2.Multiline)
inlineCodeConv := regexp2.MustCompile("`(.*?)`", regexp2.None)
listConv := regexp2.MustCompile(`(?<!.+)([ \t]*)([\*-]|(?:[0-9]+[.)])) (.*)\n`, regexp2.None)
blockQuoteConv := regexp2.MustCompile(`(?<!.+)(>+) ?(.*)\n`, regexp2.None)
bAndIConv := regexp2.MustCompile(`(\*\*\*|___)(.*?)(\1)`, regexp2.None)
bConv := regexp2.MustCompile(`(\*\*|__)(.*?)(\1)`, regexp2.None)
surroundConv := regexp2.MustCompile(`(\*|_|~~)(.*?)(\1)`, regexp2.None)
linkImgConv := regexp2.MustCompile(`[!]?\[(.*?)\]\((.*?)\)`, regexp2.None)
headingConv := regexp2.MustCompile(`(?<!.+)(#+) (.*)`, regexp2.None)
return MarkdownConverter{
largeCodeConv: largeCodeConv,
inlineCodeConv: inlineCodeConv,
listConv: listConv,
blockQuoteConv: blockQuoteConv,
bAndIConv: bAndIConv,
bConv: bConv,
surroundConv: surroundConv,
linkImgConv: linkImgConv,
headingConv: headingConv,
}
}
func (m MarkdownConverter) HTMLConvert(mk string) string {
return m.mkActualConv([]rune(mk), false)
}
func (m MarkdownConverter) mkActualConv(in []rune, comboConv bool) string {
var codeBlocks []string
var match *regexp2.Match
var err error
if !comboConv {
// Code blocks
for {
match, err = m.largeCodeConv.FindRunesMatch(in)
if err != nil || match == nil {
break
}
codeBlocks = append(codeBlocks, strings.TrimPrefix(match.GroupByNumber(1).String(), "\n"))
in = slices.Concat(in[:match.Index], []rune("</p><pre>"+codePlaceholder+"</pre><p>"), in[match.Index+match.Length:])
}
// Inline code
for {
match, err = m.inlineCodeConv.FindRunesMatch(in)
if err != nil || match == nil {
break
}
codeBlocks = append(codeBlocks, match.GroupByNumber(1).String())
in = slices.Concat(in[:match.Index], []rune(codePlaceholder), in[match.Index+match.Length:])
}
}
// lists (ordered and unordered)
for {
var allMatches []*regexp2.Match
var prevMatch *regexp2.Match
prevMatch, err = m.listConv.FindRunesMatch(in)
if err != nil || prevMatch == nil {
break
}
allMatches = append(allMatches, prevMatch)
// Find all lines that are in a single list
for {
var newMatch *regexp2.Match
newMatch, err = m.listConv.FindNextMatch(prevMatch)
if newMatch == nil || err != nil {
break
}
if newMatch.Index != prevMatch.Index+prevMatch.Length {
break
}
allMatches = append(allMatches, newMatch)
prevMatch = newMatch
}
if len(allMatches) == 0 {
break
}
var isOrdered []bool
var converted string
if allMatches[0].GroupByNumber(2).String() == "*" || allMatches[0].GroupByNumber(2).String() == "-" {
converted = "</p><ul>"
isOrdered = append(isOrdered, false)
} else {
converted = "</p><ol>"
isOrdered = append(isOrdered, true)
}
curHeight := calculateListLevel(allMatches[0].GroupByNumber(1).String())
curLvl := 1
for _, m := range allMatches {
itemHeight := calculateListLevel(m.GroupByNumber(1).String())
if itemHeight > curHeight {
curLvl++
curHeight = itemHeight
if m.GroupByNumber(2).String() == "*" || m.GroupByNumber(2).String() == "-" {
converted += "<ul>"
isOrdered = append(isOrdered, false)
} else {
converted += "<ol>"
isOrdered = append(isOrdered, true)
}
} else if itemHeight < curHeight {
if curLvl > 1 {
curLvl--
curHeight = itemHeight
if isOrdered[len(isOrdered)-1] {
converted += "</ol>"
} else {
converted += "</ul>"
}
isOrdered = isOrdered[:len(isOrdered)-1]
if m.GroupByNumber(2).String() == "*" || m.GroupByNumber(2).String() == "-" {
if isOrdered[len(isOrdered)-1] {
curLvl++
converted += "<ul>"
isOrdered = append(isOrdered, false)
}
} else {
if !isOrdered[len(isOrdered)-1] {
curLvl++
converted += "<ol>"
isOrdered = append(isOrdered, true)
}
}
}
}
converted += "<li>" + m.GroupByNumber(3).String() + "</li>"
}
for _, b := range slices.Backward(isOrdered) {
if b {
converted += "</ol>"
} else {
converted += "</ul>"
}
}
in = slices.Concat(in[:allMatches[0].Index], []rune(converted+"<p>"), in[prevMatch.Index+prevMatch.Length:])
}
// Block Quotes
for {
var allMatches []*regexp2.Match
var prevMatch *regexp2.Match
prevMatch, err = m.blockQuoteConv.FindRunesMatch(in)
if err != nil || prevMatch == nil {
break
}
allMatches = append(allMatches, prevMatch)
// Find all lines that are in a single blockquote
for {
var newMatch *regexp2.Match
newMatch, err = m.blockQuoteConv.FindNextMatch(prevMatch)
if newMatch == nil || err != nil {
break
}
if newMatch.Index != prevMatch.Index+prevMatch.Length {
break
}
allMatches = append(allMatches, newMatch)
prevMatch = newMatch
}
if len(allMatches) == 0 {
break
}
curHeight := len(allMatches[0].GroupByNumber(1).String())
curLvl := 1
converted := "</p><blockquote><p>"
for _, m := range allMatches {
if m.GroupByNumber(2).String() == "" {
converted += "</p><p>"
continue
}
itemHeight := len(m.GroupByNumber(1).String())
if itemHeight > curHeight {
curHeight = itemHeight
curLvl++
converted += "</p><blockquote><p>"
} else if itemHeight < curHeight && curLvl > 1 {
curHeight = itemHeight
curLvl--
converted += "</p></blockquote><p>"
}
converted += m.GroupByNumber(2).String()
}
converted += strings.Repeat("</p></blockquote><p>", curLvl)
in = slices.Concat(in[:allMatches[0].Index], []rune(converted), in[prevMatch.Index+prevMatch.Length:])
}
// Bold and Italics (*** and ___)
for {
match, err = m.bAndIConv.FindRunesMatch(in)
if err != nil || match == nil {
break
}
in = slices.Concat(in[:match.Index], []rune("<b><i>"), match.GroupByNumber(2).Runes(), []rune("</i></b>"), in[match.Index+match.Length:])
}
// Bold (** and __)
for {
match, err = m.bConv.FindRunesMatch(in)
if err != nil || match == nil {
break
}
in = slices.Concat(in[:match.Index], []rune("<b>"), match.GroupByNumber(2).Runes(), []rune("</b>"), in[match.Index+match.Length:])
}
// Italics and strikethough
for {
match, err = m.surroundConv.FindRunesMatch(in)
if err != nil || match == nil {
break
}
var converted string
switch match.GroupByNumber(1).String() {
case "*":
fallthrough
case "_":
converted = "<i>" + match.GroupByNumber(2).String() + "</i>"
case "~~":
converted = "<s>" + match.GroupByNumber(2).String() + "</s>"
default:
converted = match.GroupByNumber(2).String()
}
in = slices.Concat(in[:match.Index], []rune(converted), in[match.Index+match.Length:])
}
// Links and images
for {
match, err = m.linkImgConv.FindRunesMatch(in)
if err != nil || match == nil {
break
}
var converted string
if match.String()[0] == '!' {
converted = "<img src='" +
strings.ReplaceAll(match.GroupByNumber(2).String(), "'", "\\'") + "' alt='" +
strings.ReplaceAll(match.GroupByNumber(1).String(), "'", "\\'") + "'>"
} else {
converted = "<a href='" +
strings.ReplaceAll(match.GroupByNumber(2).String(), "'", "\\'") + "'>" +
match.GroupByNumber(1).String() + "</a>"
}
in = slices.Concat(in[:match.Index], []rune(converted), in[match.Index+match.Length:])
}
// Headings
for {
match, err = m.headingConv.FindRunesMatch(in)
if err != nil || match == nil {
break
}
level := len(match.GroupByNumber(1).String())
if level > 6 {
level = 6
}
in = slices.Concat(
in[:match.Index],
[]rune("</p><h"+strconv.Itoa(level)+">"),
match.GroupByNumber(2).Runes(),
[]rune("</h"+strconv.Itoa(level)+"><p>"),
in[match.Index+match.Length:])
}
out := "<p>" + strings.ReplaceAll(string(in), "\n\n", "</p>\n<p>") + "</p>"
out = strings.ReplaceAll(out, "<p></p>", "")
out = strings.ReplaceAll(out, "<p>\n</p>", "\n")
// Replace the code placeholders
for i := range codeBlocks {
out = strings.Replace(out, codeInner, codeBlocks[i], 1)
}
return out
}
func calculateListLevel(indent string) int {
indent = strings.ReplaceAll(indent, "\t", " ")
return len(indent) / 2
}