From 0bff705acd8982e34b937116eb2016c9d6e4c4a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arda=20G=C3=BC=C3=A7l=C3=BC?= Date: Wed, 24 May 2023 09:20:49 +0300 Subject: [PATCH] Preserve code blocks in templates.Normalizer (#118029) * Preserve code blocks in templates.Normalizer Currently, if templates.LongDesc function is invoked multiple times for a string including a code block, this code block is converted to invalid code block. The reason of this issue is that blackfriday package, which templates package uses, relies on 4 * indentation to detect code blocks. This PR always preserves 4 * indentation in code blocks to always detect it code blocks in also post invocations. * Add indentations to test for code blocks * Use strings.repeat for repetition --- .../kubectl/pkg/util/templates/markdown.go | 4 +- .../pkg/util/templates/normalizers_test.go | 55 ++++++++++++++++++- 2 files changed, 57 insertions(+), 2 deletions(-) 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) + } + }) + } +}