If not in a code block or a code span, kramdown treats pipes ("|") as table column separators, even if there's no table header, or left/right table row borders (i.e. lines starting and ending with a pipe). We need to assert there's no pipe in the text unless it's in a code-block or code-span. (See
(t *testing.T, cmd *cobra.Command)
| 101 | // |
| 102 | // (See https://github.com/cli/cli/issues/10348) |
| 103 | func assertPipesAreInCodeBlocks(t *testing.T, cmd *cobra.Command) { |
| 104 | md := goldmark.New() |
| 105 | reader := text.NewReader([]byte(cmd.Long)) |
| 106 | doc := md.Parser().Parse(reader) |
| 107 | |
| 108 | var checkNode func(node ast.Node) |
| 109 | checkNode = func(node ast.Node) { |
| 110 | if node.Kind() == ast.KindCodeSpan || node.Kind() == ast.KindCodeBlock { |
| 111 | return |
| 112 | } |
| 113 | |
| 114 | if node.Kind() == ast.KindText { |
| 115 | text := string(node.(*ast.Text).Segment.Value(reader.Source())) |
| 116 | require.NotContains(t, text, "|", `found pipe ("|") in plain text in %q docs`, cmd.CommandPath()) |
| 117 | } |
| 118 | |
| 119 | for child := node.FirstChild(); child != nil; child = child.NextSibling() { |
| 120 | checkNode(child) |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | checkNode(doc) |
| 125 | } |
no test coverage detected