Skip to content

Commit

Permalink
fix: logseq accepts attributes with leading spaces, we have to trim them
Browse files Browse the repository at this point in the history
  • Loading branch information
viktomas committed Aug 10, 2023
1 parent a48b7ef commit df6dd00
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
7 changes: 3 additions & 4 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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:]
})
Expand Down Expand Up @@ -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),
Expand Down
6 changes: 6 additions & 0 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit df6dd00

Please sign in to comment.