-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.go
More file actions
172 lines (139 loc) · 4.75 KB
/
util.go
File metadata and controls
172 lines (139 loc) · 4.75 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
166
167
168
169
170
171
172
package notezero
import (
"fmt"
"io"
"log"
"regexp"
"strings"
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/ast"
"github.com/gomarkdown/markdown/html"
"github.com/gomarkdown/markdown/parser"
"github.com/microcosm-cc/bluemonday"
)
// TODO: Maybe move this to core DL
// TODO make links/reference optional: mdToHtml(conent string, skipLinks bool)
var mdrenderer = html.NewRenderer(html.RendererOptions{
Flags: html.CommonFlags | html.HrefTargetBlank,
})
func stripLinksFromMarkdown(md string) string {
// Regular expression to match Markdown links and HTML links
linkRegex := regexp.MustCompile(`\[([^\]]*)\]\([^)]*\)|<a[^>]*>(.*?)</a>`)
// Replace both Markdown and HTML links with just the link text
strippedMD := linkRegex.ReplaceAllString(md, "$1$2")
return strippedMD
}
var tgivmdrenderer = html.NewRenderer(html.RendererOptions{
Flags: html.CommonFlags | html.HrefTargetBlank,
RenderNodeHook: func(w io.Writer, node ast.Node, entering bool) (ast.WalkStatus, bool) {
// telegram instant view really doesn't like when there is an image inside a paragraph (like <p><img></p>)
// so we use this custom thing to stop all paragraphs before the images, print the images then start a new
// paragraph afterwards.
if img, ok := node.(*ast.Image); ok {
if entering {
src := img.Destination
w.Write([]byte(`</p><img src="`))
html.EscLink(w, src)
w.Write([]byte(`" alt="`))
} else {
if img.Title != nil {
w.Write([]byte(`" title="`))
html.EscapeHTML(w, img.Title)
}
w.Write([]byte(`" /><p>`))
}
return ast.GoToNext, true
}
return ast.GoToNext, false
},
})
func sanitizeXSS(html string) string {
p := bluemonday.UGCPolicy()
p.AllowStyling()
p.RequireNoFollowOnLinks(false)
p.AllowElements("video", "source", "iframe")
p.AllowAttrs("controls", "width").OnElements("video")
p.AllowAttrs("src", "width").OnElements("source")
p.AllowAttrs("src", "frameborder").OnElements("iframe")
return p.Sanitize(html)
}
func markdownToHtml(md string, usingTelegramInstantView bool, skipLinks bool) string {
md = strings.ReplaceAll(md, "\u00A0", " ")
// create markdown parser with extensions
// this parser is stateful so it must be reinitialized every time
doc := parser.NewWithExtensions(
parser.CommonExtensions |
parser.AutoHeadingIDs |
parser.NoEmptyLineBeforeBlock |
parser.Footnotes,
).Parse([]byte(md))
renderer := mdrenderer
if usingTelegramInstantView {
renderer = tgivmdrenderer
}
// create HTML renderer with extensions
output := string(markdown.Render(doc, renderer))
if skipLinks {
output = stripLinksFromMarkdown(output)
}
// sanitize content
output = sanitizeXSS(output)
return output
}
func mdToHtml(content string) string {
text, err := ReplaceReferences(content)
if err != nil {
log.Fatalln(err)
}
// create markdown parser with extensions
extensions := parser.CommonExtensions
p := parser.NewWithExtensions(extensions)
doc := p.Parse([]byte(text))
// create HTML renderer with extensions
htmlFlags := html.CommonFlags | html.HrefTargetBlank
opts := html.RendererOptions{Flags: htmlFlags}
renderer := html.NewRenderer(opts)
res := markdown.Render(doc, renderer)
return string(res)
}
// text := "Click [me](nostr:nevent17915d512457e4bc461b54ba95351719c150946ed4aa00b1d83a263deca69dae) to"
// replacement := `<a href="#" hx-get="article/$2" hx-push-url="true" hx-target="body" hx-swap="outerHTML">$1</a>`
func ReplaceReferences(text string) (string, error) {
// Define the regular expression pattern to match the markdown-like link
//pattern := `\[(.*?)\]\((.*?)\)`
pattern := `\[(.*?)\]\(nostr:(.*?)\)`
// Compile the regular expression
re := regexp.MustCompile(pattern)
// Define the replacement pattern
replacement := `<a href="#" class="inline"
hx-get="$2"
hx-push-url="true"
hx-target="body"
hx-swap="outerHTML">$1
</a>`
// Replace the matched patterns with the HTML tag
result := re.ReplaceAllString(text, replacement)
return result, nil
}
func applyHighlight(content, highlight string) string {
fmt.Println("------------ Content")
fmt.Println(content)
fmt.Println("------------ Highliths")
fmt.Println(highlight)
fmt.Println("------------")
if strings.Contains(content, highlight) {
// replace := `<span class="inline"
// hx-get="highlight/%s"
// hx-push-url="true"
// hx-target="body"
// hx-swap="outerHTML">%s
// </span>`
//
// txt := fmt.Sprintf(replace, h.Id, h.Content)
txt := fmt.Sprintf("<span class='highlight'>%s</span>", highlight)
content = strings.ReplaceAll(content, highlight, txt)
}
txt := fmt.Sprintf("<span class='highlight'>%s</span>", highlight)
content = strings.ReplaceAll(content, highlight, txt)
return content
}