Skip to content

Commit cb249dc

Browse files
committed
feat: parse action tags @action from title
- action tags start with @ instead of # (example: @ArchiveBox)
1 parent f658371 commit cb249dc

File tree

3 files changed

+45
-18
lines changed

3 files changed

+45
-18
lines changed

hooks/marktab.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"fmt"
2727
"os"
2828
"os/exec"
29-
"slices"
3029
"strings"
3130
"time"
3231

@@ -40,12 +39,6 @@ const (
4039
ArchiveBoxTrigger = "@archivebox"
4140
)
4241

43-
var (
44-
BuiltinTriggers = []string{
45-
ArchiveBoxTrigger,
46-
}
47-
)
48-
4942
// MarkTab represents a collection of rules defined in the marktab file as lines.
5043
type MarkTab struct {
5144
Rules []Rule // Rules contains all the parsed rules from the marktab file.
@@ -99,11 +92,6 @@ func processMtabHook(bk *gosuki.Bookmark) error {
9992

10093
runID := utils.GenStringID(4)
10194

102-
// cleanout tags from builtin triggers
103-
bk.Tags = slices.DeleteFunc(bk.Tags, func(in string) bool {
104-
return slices.Contains(BuiltinTriggers, in)
105-
})
106-
10795
log.Debug(
10896
"run marktab",
10997
"id",

pkg/parsing/parsetags_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,20 @@ func TestParseTags(t *testing.T) {
7676
expectedTags: []string{"existing", "newtag"},
7777
expectError: false,
7878
},
79+
{
80+
name: "Appending action to existing tags",
81+
title: "@action",
82+
initialTags: []string{"existing"},
83+
expectedTags: []string{"existing", "@action"},
84+
expectError: false,
85+
},
86+
{
87+
name: "Valid action tags in node title",
88+
title: "This is a @action and #example",
89+
initialTags: nil,
90+
expectedTags: []string{"example", "@action"},
91+
expectError: false,
92+
},
7993
}
8094

8195
for _, tt := range tests {

pkg/parsing/tags.go

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,12 @@ const (
4444
//this is a end of sentence #tag
4545
// ReTags = `\B#(?P<tag>\w+\.?\w+)`
4646
ReTags = `#(?P<tag>[a-zA-Z0-9_.-]+)`
47+
4748
// #tag:notify
4849
ReNotify = `\b(?P<tag>[a-zA-Z0-9_.-]+):notify`
50+
51+
// Action tags start with @, the regex includes the @ sign
52+
ReActionTag = `@(?P<tag>[a-zA-Z0-9_.-]+)`
4953
)
5054

5155
var log = logging.GetLogger("parse")
@@ -54,40 +58,61 @@ func stripHashTag(s string) string {
5458
return regexp.MustCompile(ReTags).ReplaceAllString(s, "")
5559
}
5660

61+
// compelte this function
62+
func stripActionTags(s string) string {
63+
return regexp.MustCompile(ReActionTag).ReplaceAllString(s, "")
64+
}
65+
5766
// parseTags is a [gosuki.Hook] that extracts tags like #tag from the title of
5867
// the bookmark or node.
5968
// It takes an item of type *tree.Node or *gosuki.Bookmark, extracts all tags
6069
// matching the regex pattern defined in ReTags, appends them to the item's Tags
6170
// field, and removes the matched tags from the title. If the item is of an
6271
// unsupported type, it returns an error.
6372
func parseTags(item any) error {
64-
var regex = regexp.MustCompile(ReTags)
73+
tagRe := regexp.MustCompile(ReTags)
74+
actionTagRe := regexp.MustCompile(ReActionTag)
6575
switch v := item.(type) {
6676
case *tree.Node:
6777
if v.Tags == nil {
6878
v.Tags = []string{}
6979
}
70-
processTags(regex, &v.Title, &v.Tags)
80+
processTags(tagRe, &v.Title, &v.Tags, false)
81+
v.Title = stripHashTag(v.Title)
82+
processTags(actionTagRe, &v.Title, &v.Tags, true)
83+
v.Title = stripActionTags(v.Title)
7184
case *gosuki.Bookmark:
7285
if v.Tags == nil {
7386
v.Tags = []string{}
7487
}
75-
processTags(regex, &v.Title, &v.Tags)
88+
processTags(tagRe, &v.Title, &v.Tags, false)
89+
v.Title = stripHashTag(v.Title)
90+
processTags(actionTagRe, &v.Title, &v.Tags, true)
91+
v.Title = stripActionTags(v.Title)
7692
default:
7793
return fmt.Errorf("unsupported type")
7894
}
7995
return nil
8096
}
8197

82-
func processTags(regex *regexp.Regexp, title *string, tags *[]string) {
98+
func processTags(
99+
regex *regexp.Regexp,
100+
title *string,
101+
tags *[]string,
102+
withSymbol bool,
103+
) {
104+
83105
matches := regex.FindAllStringSubmatch(*title, -1)
84106
for _, m := range matches {
85-
*tags = append(*tags, m[1])
107+
if withSymbol {
108+
*tags = append(*tags, m[0])
109+
} else {
110+
*tags = append(*tags, m[1])
111+
}
86112
}
87113
if len(*tags) > 0 {
88114
log.Tracef("[hook] found following tags: %s", *tags)
89115
}
90-
*title = stripHashTag(*title)
91116
}
92117

93118
func ParseNodeTags(n *tree.Node) error {

0 commit comments

Comments
 (0)