mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-08 03:33:56 +00:00
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
This commit is contained in:
parent
484645e817
commit
0bff705acd
@ -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)))
|
||||
|
@ -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<type>.<fieldName>[.<fieldName>]\n\nLine2",
|
||||
out: "Line1\n\n <type>.<fieldName>[.<fieldName>]\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<type>.<fieldName>[.<fieldName>]\n\nLine2",
|
||||
out: "Line1\n\n <type>.<fieldName>[.<fieldName>]\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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user