- commit
- ff5f1e2
- parent
- 5f4b1c3
- author
- Eric Bower
- date
- 2024-03-21 15:56:36 +0000 UTC
refactor(prose): dedupe string slice for embedded tags
2 files changed,
+16,
-13
M
go.mod
+1,
-1
1@@ -36,6 +36,7 @@ require (
2 github.com/yuin/goldmark-highlighting v0.0.0-20220208100518-594be1970594
3 github.com/yuin/goldmark-meta v1.1.0
4 go.abhg.dev/goldmark/anchor v0.1.1
5+ go.abhg.dev/goldmark/hashtag v0.3.1
6 golang.org/x/crypto v0.18.0
7 gopkg.in/yaml.v2 v2.4.0
8 )
9@@ -116,7 +117,6 @@ require (
10 github.com/tklauser/go-sysconf v0.3.12 // indirect
11 github.com/tklauser/numcpus v0.6.1 // indirect
12 github.com/yusufpapurcu/wmi v1.2.3 // indirect
13- go.abhg.dev/goldmark/hashtag v0.3.1 // indirect
14 golang.org/x/exp v0.0.0-20231108232855-2478ac86f678 // indirect
15 golang.org/x/net v0.19.0 // indirect
16 golang.org/x/sync v0.6.0 // indirect
1@@ -3,7 +3,6 @@ package shared
2 import (
3 "bytes"
4 "fmt"
5- "sort"
6 "strings"
7 "time"
8
9@@ -189,7 +188,7 @@ func ParseText(text string) (*ParsedText, error) {
10 extension.GFM,
11 extension.Footnote,
12 meta.Meta,
13- &hashtag.Extender{}, // TODO: resolver to make them links
14+ &hashtag.Extender{},
15 hili,
16 &anchor.Extender{
17 Position: anchor.Before,
18@@ -329,21 +328,25 @@ func AstTags(doc ast.Node) []string {
19 return ast.WalkContinue, nil
20 })
21 if err != nil {
22- panic(err)
23+ panic(err) // unreachable
24 }
25
26 // sort and deduplicate results
27- sort.Strings(tags)
28- e := 1
29- for i := 1; i < len(tags); i++ {
30- // this works because we're keeping tags[0]
31- if tags[i] == tags[i-1] {
32- continue
33+ dedupe := removeDuplicateStr(tags)
34+ return dedupe
35+}
36+
37+// https://stackoverflow.com/a/66751055
38+func removeDuplicateStr(strSlice []string) []string {
39+ allKeys := make(map[string]bool)
40+ list := []string{}
41+ for _, item := range strSlice {
42+ if _, value := allKeys[item]; !value {
43+ allKeys[item] = true
44+ list = append(list, item)
45 }
46- tags[e] = tags[i]
47- e++
48 }
49- return tags[:e]
50+ return list
51 }
52
53 // AstTitle extracts the title (if any) from a parsed markdown document.