Skip to content

armgit / memos

Public Imported from GitHub
0

Memos is an open-source, self-hosted note-taking app that brings your everyday thoughts together in a personal timeline. Your data stays yours, and you choose what to keep private or share.

fix: parse tag list (#446)

boojack79af7e8abf26
diff --git a/server/metric_collector.go b/server/metric_collector.go
index 0be3b3687..550db88c9 100644
--- a/server/metric_collector.go
+++ b/server/metric_collector.go
@@ -19,7 +19,7 @@ type MetricCollector struct {
}
const (
- segmentMetricWriteKey = "FqYUl1CmssHytFSnnVd0efV4gyGeH0dx"
+ segmentMetricWriteKey = "fTn5BumOkj352n3TGw9tu0ARH2dOkcoQ"
)
func NewMetricCollector(profile *profile.Profile, store *store.Store) MetricCollector {
diff --git a/server/tag.go b/server/tag.go
index 493b80574..33f4a680d 100644
--- a/server/tag.go
+++ b/server/tag.go
@@ -12,7 +12,7 @@ import (
"github.com/labstack/echo/v4"
)
-var tagRegexpList = []*regexp.Regexp{regexp.MustCompile(`^#([^\s#]+?) `), regexp.MustCompile(`\s#([^\s#]+?) `)}
+var tagRegexpList = []*regexp.Regexp{regexp.MustCompile(`^#([^\s#]+?) `), regexp.MustCompile(`[^\S#]?#([^\s#]+?) `)}
func (s *Server) registerTagRoutes(g *echo.Group) {
g.GET("/tag", func(c echo.Context) error {
@@ -47,22 +47,10 @@ func (s *Server) registerTagRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find memo list").SetInternal(err)
}
- tagMapSet := make(map[string]bool)
-
- for _, memo := range memoList {
- for _, tagRegexp := range tagRegexpList {
- for _, rawTag := range tagRegexp.FindAllString(memo.Content, -1) {
- tag := tagRegexp.ReplaceAllString(rawTag, "$1")
- tagMapSet[tag] = true
- }
- }
- }
-
tagList := []string{}
- for tag := range tagMapSet {
- tagList = append(tagList, tag)
+ for _, memo := range memoList {
+ tagList = append(tagList, findTagListFromMemoContent(memo.Content)...)
}
-
sort.Strings(tagList)
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
@@ -72,3 +60,20 @@ func (s *Server) registerTagRoutes(g *echo.Group) {
return nil
})
}
+
+func findTagListFromMemoContent(memoContent string) []string {
+ tagMapSet := make(map[string]bool)
+ for _, tagRegexp := range tagRegexpList {
+ for _, rawTag := range tagRegexp.FindAllString(memoContent, -1) {
+ tag := tagRegexp.ReplaceAllString(rawTag, "$1")
+ tagMapSet[tag] = true
+ }
+ }
+
+ tagList := []string{}
+ for tag := range tagMapSet {
+ tagList = append(tagList, tag)
+ }
+ sort.Strings(tagList)
+ return tagList
+}
diff --git a/server/tag_test.go b/server/tag_test.go
new file mode 100644
index 000000000..0d0fa9829
--- /dev/null
+++ b/server/tag_test.go
@@ -0,0 +1,39 @@
+package server
+
+import (
+ "testing"
+)
+
+func TestFindTagListFromMemoContent(t *testing.T) {
+ tests := []struct {
+ memoContent string
+ want []string
+ }{
+ {
+ memoContent: "#tag1 ",
+ want: []string{"tag1"},
+ },
+ {
+ memoContent: "#tag1 #tag2 ",
+ want: []string{"tag1", "tag2"},
+ },
+ {
+ memoContent: "#tag1 #tag2 \n#tag3 ",
+ want: []string{"tag1", "tag2", "tag3"},
+ },
+ {
+ memoContent: "#tag1 #tag2 \n#tag3 #tag4 ",
+ want: []string{"tag1", "tag2", "tag3", "tag4"},
+ },
+ {
+ memoContent: "#tag1 #tag2 \n#tag3 #tag4 ",
+ want: []string{"tag1", "tag2", "tag3", "tag4"},
+ },
+ }
+ for _, test := range tests {
+ result := findTagListFromMemoContent(test.memoContent)
+ if len(result) != len(test.want) {
+ t.Errorf("Find tag list %s: got result %v, want %v.", test.memoContent, result, test.want)
+ }
+ }
+}