Skip to content

Commit

Permalink
fix: exclude all punctuation chars except underscore in tags (usememo…
Browse files Browse the repository at this point in the history
…s#1974)

* Change tag regex

* Update tests

* Add more tag tests
  • Loading branch information
pipe01 authored Jul 17, 2023
1 parent b5d4b8e commit 8c61531
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion api/v1/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func convertTagFromStore(tag *store.Tag) *Tag {
}
}

var tagRegexp = regexp.MustCompile(`#([^\s#,]+)`)
var tagRegexp = regexp.MustCompile(`#((?:[^\s\p{P}]|_)+)`)

func findTagListFromMemoContent(memoContent string) []string {
tagMapSet := make(map[string]bool)
Expand Down
10 changes: 8 additions & 2 deletions api/v1/tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package v1

import (
"testing"

"golang.org/x/exp/slices"
)

func TestFindTagListFromMemoContent(t *testing.T) {
Expand Down Expand Up @@ -35,12 +37,16 @@ func TestFindTagListFromMemoContent(t *testing.T) {
},
{
memoContent: "#tag1 http://123123.com?123123#tag2 \n#tag3 #tag4 http://123123.com?123123#tag2) ",
want: []string{"tag1", "tag2", "tag2)", "tag3", "tag4"},
want: []string{"tag1", "tag2", "tag3", "tag4"},
},
{
memoContent: "#tag1,#tag2! #tag3.. #tag_4",
want: []string{"tag1", "tag2", "tag3", "tag_4"},
},
}
for _, test := range tests {
result := findTagListFromMemoContent(test.memoContent)
if len(result) != len(test.want) {
if !slices.Equal(result, test.want) {
t.Errorf("Find tag list %s: got result %v, want %v.", test.memoContent, result, test.want)
}
}
Expand Down
2 changes: 1 addition & 1 deletion web/src/labs/marked/parser/Tag.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { matcher } from "../matcher";

export const TAG_REG = /#([^\s#,]+)/;
export const TAG_REG = /#((?:[^\s\p{P}]|_)+)/u;

const renderer = (rawStr: string) => {
const matchResult = matcher(rawStr, TAG_REG);
Expand Down

0 comments on commit 8c61531

Please sign in to comment.