-
Notifications
You must be signed in to change notification settings - Fork 14
/
hashtags.go
234 lines (191 loc) 路 5 KB
/
hashtags.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
package hashtags
import (
"context"
"embed"
"fmt"
"html/template"
"strings"
"sync"
. "github.com/emad-elsaid/xlog"
"github.com/emad-elsaid/xlog/extensions/shortcode"
_ "embed"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer"
"github.com/yuin/goldmark/text"
"github.com/yuin/goldmark/util"
)
//go:embed templates
var templates embed.FS
func init() {
Get(`/+/tags`, tagsHandler)
Get(`/+/tag/{tag}`, tagHandler)
RegisterWidget(AFTER_VIEW_WIDGET, 1, relatedPages)
RegisterBuildPage("/+/tags", true)
RegisterLink(func(_ Page) []Link { return []Link{link(0)} })
RegisterAutocomplete(autocomplete(0))
RegisterTemplate(templates, "templates")
shortcode.ShortCode("hashtag-pages", hashtagPages)
MarkDownRenderer.Renderer().AddOptions(renderer.WithNodeRenderers(
util.Prioritized(&HashTag{}, 0),
))
MarkDownRenderer.Parser().AddOptions(parser.WithInlineParsers(
util.Prioritized(&HashTag{}, 999),
))
}
type link int
func (l link) Icon() string { return "fa-solid fa-tags" }
func (l link) Name() string { return "Hashtags" }
func (l link) Link() string { return "/+/tags" }
type HashTag struct {
ast.BaseInline
value []byte
}
func (h *HashTag) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) {
reg.Register(KindHashTag, renderHashtag)
}
func (h *HashTag) Trigger() []byte {
return []byte{'#'}
}
func (h *HashTag) Parse(parent ast.Node, block text.Reader, pc parser.Context) ast.Node {
line, _ := block.PeekLine()
if len(line) < 1 {
return nil
}
i := 1
for ; i < len(line); i++ {
c := line[i]
if !(util.IsAlphaNumeric(c) || c == '_' || c == '-') {
break
}
}
if i > len(line) || i == 1 {
return nil
}
block.Advance(i)
tag := line[1:i]
return &HashTag{value: tag}
}
func (h *HashTag) Dump(source []byte, level int) {
m := map[string]string{
"value": fmt.Sprintf("%#v", h.value),
}
ast.DumpHelper(h, source, level, m, nil)
}
var KindHashTag = ast.NewNodeKind("Hashtag")
func (h *HashTag) Kind() ast.NodeKind {
return KindHashTag
}
func renderHashtag(writer util.BufWriter, source []byte, n ast.Node, entering bool) (ast.WalkStatus, error) {
if !entering || n.Kind() != KindHashTag {
return ast.WalkContinue, nil
}
tag := n.(*HashTag)
fmt.Fprintf(writer, `<a href="/+/tag/%s" class="tag">%s</a>`, tag.value, tag.value)
RegisterBuildPage(fmt.Sprintf("/+/tag/%s", tag.value), true)
RegisterBuildPage(fmt.Sprintf("/+/tag/%s", strings.ToLower(string(tag.value))), true)
return ast.WalkContinue, nil
}
func tagsHandler(_ Response, r Request) Output {
tags := map[string][]Page{}
var lck sync.Mutex
EachPageCon(context.Background(), func(a Page) {
set := map[string]bool{}
hashes := FindAllInAST[*HashTag](a.AST())
for _, v := range hashes {
val := strings.ToLower(string(v.value))
// don't use same tag twice for same page
if _, ok := set[val]; ok {
continue
}
set[val] = true
lck.Lock()
if ps, ok := tags[val]; ok {
tags[val] = append(ps, a)
} else {
tags[val] = []Page{a}
}
lck.Unlock()
}
})
return Render("tags", Locals{
"title": "Hashtags",
"tags": tags,
})
}
func tagHandler(w Response, r Request) Output {
tag := r.PathValue("tag")
return Render("tag", Locals{
"title": "#" + tag,
"pages": tagPages(r.Context(), tag),
})
}
func tagPages(ctx context.Context, keyword string) []*Page {
return MapPageCon(ctx, func(p Page) *Page {
if p.Name() == INDEX {
return nil
}
tags := FindAllInAST[*HashTag](p.AST())
for _, t := range tags {
if strings.EqualFold(string(t.value), keyword) {
return &p
}
}
return nil
})
}
func relatedPages(p Page) template.HTML {
if p.Name() == INDEX {
return ""
}
found_hashtags := FindAllInAST[*HashTag](p.AST())
hashtags := map[string]bool{}
for _, v := range found_hashtags {
hashtags[strings.ToLower(string(v.value))] = true
}
pages := MapPageCon(context.Background(), func(rp Page) *Page {
if rp.Name() == p.Name() {
return nil
}
page_hashtags := FindAllInAST[*HashTag](rp.AST())
for _, h := range page_hashtags {
if _, ok := hashtags[strings.ToLower(string(h.value))]; ok {
return &rp
}
}
return nil
})
return Partial("related-hashtags-pages", Locals{
"pages": pages,
})
}
type autocomplete int
func (a autocomplete) StartChar() string {
return "#"
}
func (a autocomplete) Suggestions() []*Suggestion {
suggestions := []*Suggestion{}
set := map[string]bool{}
var lck sync.Mutex
EachPageCon(context.Background(), func(a Page) {
hashes := FindAllInAST[*HashTag](a.AST())
lck.Lock()
defer lck.Unlock()
for _, v := range hashes {
set[strings.ToLower(string(v.value))] = true
}
})
for t := range set {
suggestions = append(suggestions, &Suggestion{
Text: "#" + t,
DisplayText: "#" + t,
})
}
return suggestions
}
func hashtagPages(hashtag Markdown) template.HTML {
hashtag_value := strings.Trim(string(hashtag), "# \n")
pages := tagPages(context.Background(), hashtag_value)
output := Partial("hashtag-pages", Locals{"pages": pages})
return template.HTML(output)
}