diff --git a/staging/src/k8s.io/kubectl/pkg/util/templates/markdown.go b/staging/src/k8s.io/kubectl/pkg/util/templates/markdown.go index ff745c5664c..962cd9eec9b 100644 --- a/staging/src/k8s.io/kubectl/pkg/util/templates/markdown.go +++ b/staging/src/k8s.io/kubectl/pkg/util/templates/markdown.go @@ -58,7 +58,9 @@ func (r *ASCIIRenderer) RenderNode(w io.Writer, node *blackfriday.Node, entering lines := []string{} for _, line := range strings.Split(string(node.Literal), linebreak) { trimmed := strings.Trim(line, " \t") - indented := r.Indentation + trimmed + // Adding 4 times of indentation will let blackfriday to accept + // this literal as Code or CodeBlock again in next invocation + indented := strings.Repeat(r.Indentation, 4) + trimmed lines = append(lines, indented) } w.Write([]byte(strings.Join(lines, linebreak))) diff --git a/staging/src/k8s.io/kubectl/pkg/util/templates/normalizers_test.go b/staging/src/k8s.io/kubectl/pkg/util/templates/normalizers_test.go index d517248d5dc..3d123f45622 100644 --- a/staging/src/k8s.io/kubectl/pkg/util/templates/normalizers_test.go +++ b/staging/src/k8s.io/kubectl/pkg/util/templates/normalizers_test.go @@ -64,7 +64,12 @@ func TestLongDescMarkdown(t *testing.T) { { desc: "Multi lines without order", in: "\t\t\t\t\tDescriptions.\n\n * Item.\n * Item2.", - out: "Descriptions.\n \n * Item.\n * Item2.", + out: "Descriptions.\n \n * Item.\n * Item2.", + }, + { + desc: "With code block", + in: "Line1\n\n\t.[.]\n\nLine2", + out: "Line1\n\n .[.]\n \n Line2", }, } @@ -77,3 +82,51 @@ func TestLongDescMarkdown(t *testing.T) { }) } } + +func TestMultiLongDescInvocation(t *testing.T) { + tests := []struct { + desc string + in string + out string + }{ + { + desc: "Empty input produces empty output", + in: "", + out: "", + }, + { + desc: "Single line text is preserved as is", + in: "Some text", + out: "Some text", + }, + { + desc: "Consecutive new lines are combined into a single paragraph", + in: "Line1\nLine2", + out: "Line1 Line2", + }, + { + desc: "Two paragraphs", + in: "Line1\n\nLine2", + out: "Line1\n\n Line2", + }, + { + desc: "Leading and trailing spaces are stripped (single line)", + in: "\t \nThe text line \n \t", + out: "The text line", + }, + { + desc: "With code block", + in: "Line1\n\n\t.[.]\n\nLine2", + out: "Line1\n\n .[.]\n \n Line2", + }, + } + + for _, test := range tests { + t.Run(test.desc, func(t *testing.T) { + got := LongDesc(LongDesc(test.in)) + if got != test.out { + t.Errorf("expected(%d):\n%s\n=====\ngot(%d):\n%s\n", len(test.out), test.out, len(got), got) + } + }) + } +}