From df6dd00db66c87b09dccae20dec9f63c38f0f62a Mon Sep 17 00:00:00 2001 From: Tomas Vik Date: Thu, 10 Aug 2023 10:15:53 +0200 Subject: [PATCH] fix: logseq accepts attributes with leading spaces, we have to trim them --- parse.go | 7 +++---- parse_test.go | 6 ++++++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/parse.go b/parse.go index 2d17a49..e776184 100644 --- a/parse.go +++ b/parse.go @@ -76,7 +76,7 @@ var dateLinkRegexp = regexp.MustCompile(`^\s*\[\[([^]]+?)]]\s*$`) func parseAttributes(rawContent string) map[string]string { result := attrAndContentRegexp.FindStringSubmatch(rawContent) - attrArray := regexp.MustCompile(`(?m:^(.*?)::\s*(.*)$)`).FindAllStringSubmatch(result[1], -1) + attrArray := regexp.MustCompile(`(?m:^\s*(.*?)::\s*(.*?)$)`).FindAllStringSubmatch(result[1], -1) attributes := map[string]string{} for _, attrStrings := range attrArray { attributes[attrStrings[1]] = attrStrings[2] @@ -103,7 +103,7 @@ func firstBulletPointsToParagraphs(from string) string { return regexp.MustCompile(`(?m:^- )`).ReplaceAllString(from, "\n") } -func unindentAllRemainingBulletPoints(from string) string { +func removeTabFromMultiLevelBulletPoints(from string) string { return regexp.MustCompile(`(?m:^\t{1,}[^\t])`).ReplaceAllStringFunc(from, func(s string) string { return s[1:] }) @@ -150,8 +150,7 @@ func parseContent(rawContent string) parsedContent { firstBulletPointsToParagraphs, // since we turned the first bullet points to paragraphs // we shift all bullet points by one tab to the left - // including subsequent lines for multiline strings - unindentAllRemainingBulletPoints, + removeTabFromMultiLevelBulletPoints, ) return parsedContent{ attributes: parseAttributes(rawContent), diff --git a/parse_test.go b/parse_test.go index a30a7d3..60f0db3 100644 --- a/parse_test.go +++ b/parse_test.go @@ -136,6 +136,12 @@ func TestParseContent(t *testing.T) { require.Equal(t, "true", result.attributes["public"]) }) + t.Run("trims attribute names and values", func(t *testing.T) { + result := parseContent(" public:: true\n") + require.Equal(t, "", result.content) + require.Equal(t, "true", result.attributes["public"]) + }) + t.Run("parses page with one line", func(t *testing.T) { result := parseContent("- a\n") require.Equal(t, "\na\n", result.content)