Skip to content

Commit

Permalink
fix: replace too many consecutive new lines
Browse files Browse the repository at this point in the history
  • Loading branch information
ashishb committed Aug 18, 2024
1 parent 78f627a commit ca3c27e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/wp2hugo/internal/hugogenerator/hugopage/hugo_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ func (page *Page) getMarkdown(provider ImageURLProvider, htmlContent string, foo
}

markdown = replaceOrderedListNumbers(markdown)
markdown = replaceConsecutiveNewlines(markdown)

return &markdown, nil
}
Expand Down Expand Up @@ -299,6 +300,13 @@ func replaceOrderedListNumbers(markdown string) string {
return reg1.ReplaceAllString(markdown, `${1}1.$3`)
}

func replaceConsecutiveNewlines(markdown string) string {
// Ref: https://github.com/markdownlint/markdownlint/blob/main/docs/RULES.md#md012---multiple-consecutive-blank-lines
// Replace multiple consecutive newlines with just two newlines
reg1 := regexp.MustCompile(`\n{3,}`)
return reg1.ReplaceAllString(markdown, "\n\n")
}

func (page Page) writeContent(w io.Writer) error {
if _, err := w.Write([]byte(page.markdown)); err != nil {
return fmt.Errorf("error writing to page file: %s", err)
Expand Down
9 changes: 9 additions & 0 deletions src/wp2hugo/internal/hugogenerator/hugopage/hugo_page_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ const (
_sampleMarkdownOutput3 = "1. First item\n1. Second item\n1. Third item"
)

const (
_sampleHTMLInput4 = `This is<br><br>some<br><br><br>text`
_sampleMarkdownOutput4 = "This is\n\nsome\n\ntext"
)

func TestMarkdownExtractorWithLink1(t *testing.T) {
testMarkdownExtractor(t, _sampleHTMLInput2, _sampleMarkdownOutput2)
}
Expand All @@ -41,6 +46,10 @@ func TestListExtractor(t *testing.T) {
testMarkdownExtractor(t, _sampleHTMLInput3, _sampleMarkdownOutput3)
}

func TestConsecutiveNewlines(t *testing.T) {
testMarkdownExtractor(t, _sampleHTMLInput4, _sampleMarkdownOutput4)
}

func testMarkdownExtractor(t *testing.T, htmlInput string, markdownOutput string) {
url1, err := url.Parse("https://example.com")
assert.Nil(t, err)
Expand Down

0 comments on commit ca3c27e

Please sign in to comment.