From 5c3813b593415615116acbe8d21f262c1f20e9f8 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Wed, 15 Jan 2025 21:30:28 +0200 Subject: fix bug in regards to inline tags extractions involving newlines --- internal/tags/inline.go | 5 +++-- internal/tags/inline_test.go | 10 ++++++++-- 2 files changed, 11 insertions(+), 4 deletions(-) (limited to 'internal') diff --git a/internal/tags/inline.go b/internal/tags/inline.go index da31acf..a4d90cc 100644 --- a/internal/tags/inline.go +++ b/internal/tags/inline.go @@ -57,7 +57,7 @@ func inlineExtractTagsFromContent(content string) ([]string, string, error) { isShare := func(tag string) bool { return strings.HasPrefix(tag, "share:") } - parts := strings.Split(content, " ") + parts := strings.Fields(content) // First word must contain certain symbols to clarify as (inline) tags. if !inlineTagRE.MatchString(parts[0]) { return []string{}, content, nil @@ -77,7 +77,8 @@ func inlineExtractTagsFromContent(content string) ([]string, string, error) { } } } - return tags, strings.TrimSpace(strings.Join(parts[1:], " ")), nil + content = strings.TrimPrefix(content, parts[0]) + return tags, strings.TrimSpace(content), nil } return []string{}, content, nil diff --git a/internal/tags/inline_test.go b/internal/tags/inline_test.go index 9fafcf8..af089ed 100644 --- a/internal/tags/inline_test.go +++ b/internal/tags/inline_test.go @@ -17,6 +17,7 @@ func TestInlineExtractTagsToFilePath(t *testing.T) { "share:li,foo this is the main content": "./gosdir/foo.golang.rox.share:linkedin.foo.extracted.txt", "share:li:ma this is the main content": "./gosdir/foo.golang.rox.share:linkedin:mastodon.extracted.txt", "share:li:ma,now this is the main content": "./gosdir/foo.golang.rox.share:linkedin:mastodon.now.extracted.txt", + "share,soon this will be shared soon": "./gosdir/foo.golang.rox.share.soon.extracted.txt", } for content, expectedFilePath := range table { @@ -42,6 +43,11 @@ func TestInlineExtractTagsFromContent(t *testing.T) { "share:li,foo this is the main content": {"share:linkedin", "foo"}, "shar()e:li,foo this is the main content": {"shar()e:li", "foo"}, "share this post": {}, + "share,soon the main content here": {"share", "soon"}, + `share,soon + + the main content here + #foo`: {"share", "soon"}, } for input, expectedTags := range table { @@ -63,9 +69,9 @@ func TestInlineExtractTagsFromContent(t *testing.T) { } expectedMainContent := input - parts := strings.Split(input, " ") + parts := strings.Fields(input) if inlineTagRE.MatchString(parts[0]) { - expectedMainContent = strings.Join(parts[1:], " ") + expectedMainContent = strings.TrimPrefix(expectedMainContent, parts[0]) } if contentWithoutTags != strings.TrimSpace(expectedMainContent) { -- cgit v1.2.3