-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bookmarkdown.go
55 lines (47 loc) · 951 Bytes
/
bookmarkdown.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
package main
import (
"bufio"
"fmt"
"os"
"regexp"
"sort"
"strings"
)
func main() {
var tab = make(map[string]string)
var keys []string
content, err := os.ReadFile(os.Args[1])
if err != nil {
panic(err)
}
markdownRegex := regexp.MustCompile(`\[([^][]+)]\((https?://[^()]+)\)`)
results := markdownRegex.FindAllStringSubmatch(string(content), -1)
for v := range results {
title := strings.ReplaceAll(
strings.ReplaceAll(
results[v][1],
"\n",
""),
" ",
" ")
link := results[v][2]
tab[title] = link
keys = append(keys, title)
}
sort.Strings(keys)
if len(os.Args) == 2 {
for _, title := range keys {
fmt.Println(title)
}
} else if len(os.Args) == 3 {
var input string = os.Args[2]
if input == "-" {
reader := bufio.NewReader(os.Stdin)
input, _ = reader.ReadString('\n')
input = strings.TrimRight(input, "\n")
}
if v, exists := tab[input]; exists {
fmt.Println(v)
}
}
}