-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmarkdown.go
359 lines (307 loc) · 7.89 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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package fir
import (
"bytes"
"crypto/md5"
"encoding/hex"
"fmt"
"io"
"net/http"
"net/url"
"path"
"strings"
"sync"
"time"
embed "github.com/13rac1/goldmark-embed"
chromahtml "github.com/alecthomas/chroma/v2/formatters/html"
"github.com/livefir/fir/internal/logger"
"github.com/valyala/bytebufferpool"
"github.com/yuin/goldmark"
highlighting "github.com/yuin/goldmark-highlighting/v2"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer"
"github.com/yuin/goldmark/renderer/html"
)
type mdcache struct {
values map[string]string
sync.RWMutex
}
func (c *mdcache) get(key string) (string, bool) {
c.RLock()
defer c.RUnlock()
value, ok := c.values[key]
return value, ok
}
func (c *mdcache) set(key string, value string) {
c.Lock()
defer c.Unlock()
c.values[key] = value
}
type filecache struct {
files map[string][]byte
etags map[string]string
lastChecked map[string]time.Time
sync.RWMutex
}
func (c *filecache) getFile(key string) ([]byte, bool) {
c.RLock()
defer c.RUnlock()
value, ok := c.files[key]
return value, ok
}
func (c *filecache) setFile(key string, value []byte) {
c.Lock()
defer c.Unlock()
c.files[key] = value
}
func (c *filecache) getEtag(key string) string {
c.RLock()
defer c.RUnlock()
value, ok := c.etags[key]
if ok {
return value
}
return ""
}
func (c *filecache) setEtag(key string, val string) {
c.Lock()
defer c.Unlock()
c.etags[key] = val
}
func (c *filecache) getLastChecked(key string) time.Time {
c.RLock()
defer c.RUnlock()
value, ok := c.lastChecked[key]
if ok {
return value
}
return time.Time{}
}
func (c *filecache) setLastChecked(key string, value time.Time) {
c.Lock()
defer c.Unlock()
c.lastChecked[key] = value
}
func md5Key(in string, markers []string) string {
hash := md5.Sum([]byte(in))
checksum := hex.EncodeToString(hash[:])
if len(markers) == 0 {
return checksum
}
return checksum + "-" + strings.Join(markers, "-")
}
func fetchFileEtag(url string) string {
// make a head request to the url and get the etag from header
// if etag is not present, return empty string
// if etag is present, return etag
// Create a new HEAD request
req, err := http.NewRequest("HEAD", url, nil)
if err != nil {
logger.Errorf("error creating request: %v", err)
return ""
}
// Send the request
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
logger.Errorf("error sending request: %v", err)
return ""
}
defer resp.Body.Close()
// Check if the request was successful
if resp.StatusCode != http.StatusOK {
logger.Errorf("request failed with status: %v", resp.Status)
return ""
}
return resp.Header.Get("ETag")
}
func fetchFile(url string) ([]byte, error) {
response, err := http.Get(url)
if err != nil {
logger.Errorf("failed to fetch the file: %v\n", err)
return nil, err
}
defer response.Body.Close()
// Copy the content from the remote response to the local file
data, err := io.ReadAll(response.Body)
if err != nil {
logger.Errorf("failed to save the file: %v\n", err)
return nil, err
}
return data, nil
}
func isValidURL(input string) bool {
_, err := url.ParseRequestURI(input)
if err != nil {
return false
}
u, err := url.Parse(input)
if err != nil || u.Scheme == "" || u.Host == "" {
return false
}
return u.Scheme == "http" || u.Scheme == "https"
}
func markdown(readFile readFileFunc, existFile existFileFunc) func(in string, markers ...string) string {
cachemd := &mdcache{
values: make(map[string]string),
}
cachefile := &filecache{
files: make(map[string][]byte),
etags: make(map[string]string),
lastChecked: make(map[string]time.Time),
}
mdparser := markdownParser()
return func(in string, markers ...string) string {
var indata []byte
var isFile bool
if isValidURL(in) {
fkey := md5Key(in, nil)
var err error
f, ok := cachefile.getFile(fkey)
if !ok || time.Since(cachefile.getLastChecked(fkey)) > time.Minute*5 {
etag := fetchFileEtag(in)
cachefile.setLastChecked(fkey, time.Now())
if etag != cachefile.getEtag(fkey) || (etag == "" && cachefile.getEtag(fkey) == "") {
f, err = fetchFile(in)
if err != nil {
logger.Errorf("%v", err)
return string("error fetching file")
}
cachefile.setEtag(fkey, etag)
cachefile.setFile(fkey, f)
}
}
// enclose the file in a code block
ext := path.Ext(in)
indata = []byte("```" + ext + "\n" + string(f) + "```")
isFile = true
} else {
if existFile(in) {
_, data, err := readFile(in)
if err != nil {
logger.Errorf("%v", err)
return string("error reading file")
}
indata = data
isFile = true
} else {
indata = []byte(in)
}
}
// check if snippet is already in cache
key := md5Key(in, markers)
if value, ok := cachemd.get(key); ok {
return string(value)
}
indata = snippets(indata, markers)
buf := bytebufferpool.Get()
defer bytebufferpool.Put(buf)
if err := mdparser.Convert(indata, buf); err != nil {
logger.Errorf("%v", err)
return string("error converting to markdown")
}
result := buf.String()
if isFile {
cachemd.set(key, result)
}
return string(result)
}
}
func snippets(in []byte, markers []string) []byte {
if len(markers) == 0 {
return in
}
var out []byte
atleastOneMarker := false
for _, marker := range markers {
content, valid := snippet(in, marker)
if len(content) == 0 && !valid {
continue
}
if valid {
atleastOneMarker = true
}
// Add a newline before the snippet if there is already content
if len(out) > 0 {
out = append(out, []byte("\n")...)
}
out = append(out, content...)
}
if len(out) == 0 && !atleastOneMarker {
return in
}
return out
}
func snippet(in []byte, marker string) ([]byte, bool) {
startMarker := fmt.Sprintf("<!-- start %s -->", marker)
endMarker := fmt.Sprintf("<!-- end %s -->", marker)
lines := bytes.Split(in, []byte("\n"))
start := -1
end := -1
for i, line := range lines {
if bytes.Equal(bytes.TrimSpace(line), []byte(startMarker)) {
start = i + 1
}
if bytes.Equal(bytes.TrimSpace(line), []byte(endMarker)) {
end = i
}
}
if start == -1 && end == -1 {
return []byte{}, false
}
if start > -1 && end == -1 {
return bytes.Join(lines[start:], []byte("\n")), true
}
if start == -1 && end > -1 {
return []byte{}, false
}
if end < start {
return []byte{}, false
}
if start == end {
return []byte{}, false
}
return bytes.Join(lines[start:end], []byte("\n")), true
}
func markdownParser() goldmark.Markdown {
var (
extensions []goldmark.Extender
parserOptions []parser.Option
rendererOptions []renderer.Option
)
rendererOptions = append(rendererOptions, html.WithHardWraps())
rendererOptions = append(rendererOptions, html.WithXHTML())
rendererOptions = append(rendererOptions, html.WithUnsafe())
extensions = append(extensions, extension.Table)
extensions = append(extensions, extension.Strikethrough)
extensions = append(extensions, extension.Linkify)
extensions = append(extensions, extension.TaskList)
extensions = append(extensions, extension.Typographer)
extensions = append(extensions, extension.DefinitionList)
extensions = append(extensions, extension.Footnote)
extensions = append(extensions, extension.GFM)
extensions = append(extensions, extension.CJK)
extensions = append(extensions, embed.New())
extensions = append(extensions, extension.Footnote)
extensions = append(extensions, highlighting.NewHighlighting(
highlighting.WithGuessLanguage(true),
highlighting.WithStyle("dracula"),
highlighting.WithFormatOptions(
chromahtml.WithLineNumbers(true),
),
))
parserOptions = append(parserOptions, parser.WithAutoHeadingID())
parserOptions = append(parserOptions, parser.WithAttribute())
md := goldmark.New(
goldmark.WithExtensions(
extensions...,
),
goldmark.WithParserOptions(
parserOptions...,
),
goldmark.WithRendererOptions(
rendererOptions...,
),
)
return md
}