From a48b7ef34200abdf09ce3fd166b61abb87c34a67 Mon Sep 17 00:00:00 2001 From: Tomas Vik Date: Sat, 5 Aug 2023 10:01:21 +0200 Subject: [PATCH] fix: multiline blocks incorrectly indented --- parse.go | 9 ++++++--- parse_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/parse.go b/parse.go index 5acfd83..2d17a49 100644 --- a/parse.go +++ b/parse.go @@ -103,8 +103,8 @@ func firstBulletPointsToParagraphs(from string) string { return regexp.MustCompile(`(?m:^- )`).ReplaceAllString(from, "\n") } -func removeTabFromMultiLevelBulletPoints(from string) string { - return regexp.MustCompile(`(?m:^\t{1,}-)`).ReplaceAllStringFunc(from, func(s string) string { +func unindentAllRemainingBulletPoints(from string) string { + return regexp.MustCompile(`(?m:^\t{1,}[^\t])`).ReplaceAllStringFunc(from, func(s string) string { return s[1:] }) } @@ -148,7 +148,10 @@ func parseContent(rawContent string) parsedContent { removeEmptyBulletPoints, unindentMultilineStrings, firstBulletPointsToParagraphs, - removeTabFromMultiLevelBulletPoints, + // 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, ) return parsedContent{ attributes: parseAttributes(rawContent), diff --git a/parse_test.go b/parse_test.go index 11f7512..a30a7d3 100644 --- a/parse_test.go +++ b/parse_test.go @@ -161,6 +161,30 @@ func TestParseContent(t *testing.T) { result := parseContent("\t\t- hello\n\t\t\t- world") require.Equal(t, "\t- hello\n\t\t- world", result.content) }) + + t.Run("handles fenced blocks in second-level bullet points", func(t *testing.T) { + result := parseContent(` +- ## If statement + - ~~~bash + if [-a file];then + ... + else + ... + fi + ~~~ +`) + require.Equal(t, ` +## If statement +- ~~~bash + if [-a file];then + ... + else + ... + fi + ~~~ +`, result.content) + }) + t.Run("removes tabs from all subsequent lines of a bullet point", func(t *testing.T) { result := parseContent(` - ~~~ts