Skip to content

Commit

Permalink
fix: multiline blocks incorrectly indented
Browse files Browse the repository at this point in the history
  • Loading branch information
viktomas committed Aug 5, 2023
1 parent f701a62 commit a48b7ef
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
9 changes: 6 additions & 3 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:]
})
}
Expand Down Expand Up @@ -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),
Expand Down
24 changes: 24 additions & 0 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a48b7ef

Please sign in to comment.