From 7db53b2b7a3db0e930c9a28a8fb8eda94ae7c834 Mon Sep 17 00:00:00 2001 From: kinensake Date: Mon, 13 Jan 2025 01:14:34 +0700 Subject: [PATCH 1/3] fix render code block in list --- plugin/commonmark/render_list.go | 12 ++++++-- plugin/commonmark/render_list_test.go | 44 +++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 plugin/commonmark/render_list_test.go diff --git a/plugin/commonmark/render_list.go b/plugin/commonmark/render_list.go index 289b537..d99109c 100644 --- a/plugin/commonmark/render_list.go +++ b/plugin/commonmark/render_list.go @@ -9,6 +9,7 @@ import ( "github.com/JohannesKaufmann/dom" "github.com/JohannesKaufmann/html-to-markdown/v2/converter" "github.com/JohannesKaufmann/html-to-markdown/v2/internal/textutils" + "github.com/JohannesKaufmann/html-to-markdown/v2/marker" "golang.org/x/net/html" ) @@ -43,20 +44,27 @@ func (c commonmark) getPrefixFunc(n *html.Node, sliceLength int) func(int) strin func renderMultiLineListItem(w converter.Writer, content []byte, indentCount int) { lines := bytes.Split(content, []byte("\n")) + indent := bytes.Repeat([]byte(" "), indentCount) + + indentedCodeBlockNewline := append(marker.BytesMarkerCodeBlockNewline, indent...) for i := range lines { + // Add indent to code block newlines + line := bytes.ReplaceAll(lines[i], marker.BytesMarkerCodeBlockNewline, indentedCodeBlockNewline) + if i != 0 { // The first line is already indented through the prefix, // all other lines need the correct amount of spaces. - w.Write(bytes.Repeat([]byte(" "), indentCount)) + w.Write(indent) } - w.Write(lines[i]) + w.Write(line) if i < len(lines)-1 { w.WriteRune('\n') } } } + func (c commonmark) renderListContainer(ctx converter.Context, w converter.Writer, n *html.Node) converter.RenderStatus { children := dom.AllChildNodes(n) items := make([][]byte, 0, len(children)) diff --git a/plugin/commonmark/render_list_test.go b/plugin/commonmark/render_list_test.go new file mode 100644 index 0000000..005ddfc --- /dev/null +++ b/plugin/commonmark/render_list_test.go @@ -0,0 +1,44 @@ +package commonmark_test + +import ( + "testing" + + "github.com/JohannesKaufmann/html-to-markdown/v2/converter" + "github.com/JohannesKaufmann/html-to-markdown/v2/plugin/base" + "github.com/JohannesKaufmann/html-to-markdown/v2/plugin/commonmark" +) + +func TestNewCommonmarkPlugin_List(t *testing.T) { + const nonBreakingSpace = '\u00A0' + const zeroWidthSpace = '\u200b' + + runs := []struct { + desc string + input string + expected string + }{ + { + desc: "nested code block in list item", + input: "", + expected: "- list item:\n \n ```\n line 1\n line 2\n ```", + }, + } + for _, run := range runs { + t.Run(run.desc, func(t *testing.T) { + conv := converter.NewConverter( + converter.WithPlugins( + base.NewBasePlugin(), + commonmark.NewCommonmarkPlugin(), + ), + ) + + out, err := conv.ConvertString(run.input) + if err != nil { + t.Error(err) + } + if out != run.expected { + t.Errorf("expected %q but got %q", run.expected, out) + } + }) + } +} From 6a359b11e77ceff6a5f8442e18ed327982414d69 Mon Sep 17 00:00:00 2001 From: kinensake Date: Mon, 13 Jan 2025 02:19:57 +0700 Subject: [PATCH 2/3] remove unused constants in render list test --- plugin/commonmark/render_list_test.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/plugin/commonmark/render_list_test.go b/plugin/commonmark/render_list_test.go index 005ddfc..2cd133b 100644 --- a/plugin/commonmark/render_list_test.go +++ b/plugin/commonmark/render_list_test.go @@ -9,9 +9,6 @@ import ( ) func TestNewCommonmarkPlugin_List(t *testing.T) { - const nonBreakingSpace = '\u00A0' - const zeroWidthSpace = '\u200b' - runs := []struct { desc string input string From 87f20d33b09fd1bf29e558b25df16bc1c4c29f34 Mon Sep 17 00:00:00 2001 From: kinensake Date: Mon, 13 Jan 2025 08:27:28 +0700 Subject: [PATCH 3/3] add code block in list test to golden files --- plugin/commonmark/render_list_test.go | 41 ------------------- .../testdata/GoldenFiles/list.in.html | 32 +++++++++++++++ .../testdata/GoldenFiles/list.out.md | 29 +++++++++++++ 3 files changed, 61 insertions(+), 41 deletions(-) delete mode 100644 plugin/commonmark/render_list_test.go diff --git a/plugin/commonmark/render_list_test.go b/plugin/commonmark/render_list_test.go deleted file mode 100644 index 2cd133b..0000000 --- a/plugin/commonmark/render_list_test.go +++ /dev/null @@ -1,41 +0,0 @@ -package commonmark_test - -import ( - "testing" - - "github.com/JohannesKaufmann/html-to-markdown/v2/converter" - "github.com/JohannesKaufmann/html-to-markdown/v2/plugin/base" - "github.com/JohannesKaufmann/html-to-markdown/v2/plugin/commonmark" -) - -func TestNewCommonmarkPlugin_List(t *testing.T) { - runs := []struct { - desc string - input string - expected string - }{ - { - desc: "nested code block in list item", - input: "
  • list item:
    line 1\nline 2
", - expected: "- list item:\n \n ```\n line 1\n line 2\n ```", - }, - } - for _, run := range runs { - t.Run(run.desc, func(t *testing.T) { - conv := converter.NewConverter( - converter.WithPlugins( - base.NewBasePlugin(), - commonmark.NewCommonmarkPlugin(), - ), - ) - - out, err := conv.ConvertString(run.input) - if err != nil { - t.Error(err) - } - if out != run.expected { - t.Errorf("expected %q but got %q", run.expected, out) - } - }) - } -} diff --git a/plugin/commonmark/testdata/GoldenFiles/list.in.html b/plugin/commonmark/testdata/GoldenFiles/list.in.html index 45c5412..2d096b2 100644 --- a/plugin/commonmark/testdata/GoldenFiles/list.in.html +++ b/plugin/commonmark/testdata/GoldenFiles/list.in.html @@ -243,6 +243,38 @@ +
+ + + +
    +
  • + item: +
    line 1
    +line 2
    +
  • +
  • item 2
  • +
+ + +
    +
  • + item 1: +
      +
    • + nested item 1: +
      line 1
      +line 2
      +
    • +
    • nested item 2
    • +
    +
  • +
  • item 2
  • +
+ + +
+ + +- item: + + ``` + line 1 + line 2 + ``` +- item 2 + + + + + +- item 1: + + - nested item 1: + + ``` + line 1 + line 2 + ``` + - nested item 2 +- item 2 + +* * * +