From 81f743a4efea9031a02c9d08f78c78d9248f0ce6 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Wed, 27 Nov 2024 21:59:46 +0200 Subject: fix inline tag extraction --- internal/tags/inline.go | 15 ++++++++++----- internal/tags/inline_test.go | 9 ++++++--- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/internal/tags/inline.go b/internal/tags/inline.go index 0509f4d..b952307 100644 --- a/internal/tags/inline.go +++ b/internal/tags/inline.go @@ -6,13 +6,14 @@ import ( "path/filepath" "regexp" "strings" + "testing" "codeberg.org/snonux/gos/internal/colour" "codeberg.org/snonux/gos/internal/oi" "codeberg.org/snonux/gos/internal/platforms" ) -var inlineTagRE = regexp.MustCompile(`^[a-z\.,:]*$`) +var inlineTagRE = regexp.MustCompile(`[.,:]`) // Extracts the inline tags from the content ant inserts them into the file path. func InlineExtract(filePath string) (string, error) { @@ -55,19 +56,23 @@ func inlineExtractTagsToFilePath(filePath, content string) (string, string, erro return newFilePath, newContent, nil } +var T *testing.T + func inlineExtractTagsFromContent(content string) ([]string, string, error) { + isShare := func(tag string) bool { + return strings.HasPrefix(tag, "share:") + } parts := strings.Split(content, " ") + // First word must contain certain symbols to clarify as (inline) tags. if inlineTagRE.MatchString(parts[0]) { var tags []string + // String separator either a dot or a comma. Each element will be a tag. for _, elem := range strings.Split(parts[0], ".") { tags = append(tags, strings.Split(elem, ",")...) } - if len(tags) == 0 { - tags = parts[:1] - } if len(tags) > 0 { for i := range len(tags) { - if strings.HasPrefix(tags[i], "share:") { + if isShare(tags[i]) { var err error if tags[i], err = platforms.ExpandAliases(tags[i]); err != nil { return []string{}, content, err diff --git a/internal/tags/inline_test.go b/internal/tags/inline_test.go index dab8b74..5bd7b0c 100644 --- a/internal/tags/inline_test.go +++ b/internal/tags/inline_test.go @@ -33,6 +33,7 @@ func TestInlineExtractTagsToFilePath(t *testing.T) { } func TestInlineExtractTagsFromContent(t *testing.T) { + T = t table := map[string][]string{ "foo,bar,baz blablablabla...": {"foo", "bar", "baz"}, "foo.bar.baz blablablabla...": {"foo", "bar", "baz"}, @@ -40,7 +41,8 @@ func TestInlineExtractTagsFromContent(t *testing.T) { "foo,bar.baz blablablabla...": {"foo", "bar", "baz"}, "share:li this is the main content": {"share:linkedin"}, "share:li,foo this is the main content": {"share:linkedin", "foo"}, - "shar()e:li,foo this is the main content": {}, + "shar()e:li,foo this is the main content": {"shar()e:li", "foo"}, + "share this post": {}, } for input, expectedTags := range table { @@ -49,9 +51,10 @@ func TestInlineExtractTagsFromContent(t *testing.T) { if err != nil { t.Error(err) } + t.Log(expectedTags, tags) if len(tags) != len(expectedTags) { - t.Errorf("expected %d inline tags (%v) but got %d (%v)", - len(expectedTags), expectedTags, len(tags), tags) + t.Errorf("expected %d inline tags (%v) but got %d (%v) for input '%v'", + len(expectedTags), expectedTags, len(tags), tags, input) } for _, expectedTag := range expectedTags { if !slices.Contains(tags, expectedTag) { -- cgit v1.2.3