From b6ea2cc6424198b11f03ed1db53c26a189b21f3b Mon Sep 17 00:00:00 2001 From: Dmitri Shuralyov Date: Mon, 17 Jun 2024 15:16:07 -0400 Subject: [PATCH] internal/task: unify handling of regular and fenced code blocks The Markdown parser used here emits separate AST nodes for regular and fenced code blocks. Despite that partial support for the former was started in the initial CL 411575, it appears incomplete. Code blocks weren't exercised by the existing announcement templates, and there wasn't coverage for them in the TestMarkdownToText sample input either, which is why things stayed as they were for this long. They're about to be used more actively, so unify their handling, and render them as plain text without additional indentation. This seems to work out better in this context. For golang/go#67618. Change-Id: Ia1b3cfeb56716624916535c51b45c49bb078101e Co-authored-by: Robert Findley Reviewed-on: https://go-review.googlesource.com/c/build/+/593057 Reviewed-by: Dmitri Shuralyov Auto-Submit: Dmitri Shuralyov Reviewed-by: Robert Findley LUCI-TryBot-Result: Go LUCI --- internal/task/announce.go | 16 ++++++++++------ internal/task/announce_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/internal/task/announce.go b/internal/task/announce.go index 0a619f6a..cc2ee9b3 100644 --- a/internal/task/announce.go +++ b/internal/task/announce.go @@ -661,8 +661,8 @@ func renderMarkdown(r io.Reader) (html, text string, _ error) { // of our test data, since without a browser plain text is more readable than HTML.) // // The output is mostly plain text that doesn't preserve Markdown syntax (for example, -// `code` is rendered without backticks), though there is very lightweight formatting -// applied (links are written as "text "). +// `code` is rendered without backticks, code blocks aren't indented, and so on), +// though there is very lightweight formatting applied (links are written as "text "). // // We can in theory choose to delete this renderer at any time if its maintenance costs // start to outweight its benefits, since Markdown by definition is designed to be human @@ -685,7 +685,7 @@ func (markdownToTextRenderer) Render(w io.Writer, source []byte, n ast.Node) err switch n.PreviousSibling().Kind() { default: fmt.Fprint(w, "\n\n") - case ast.KindCodeBlock: + case ast.KindCodeBlock, ast.KindFencedCodeBlock: // A code block always ends with a newline, so only need one more. fmt.Fprintln(w) } @@ -707,11 +707,15 @@ func (markdownToTextRenderer) Render(w io.Writer, source []byte, n ast.Node) err // If we're in a list, indent accordingly. fmt.Fprint(w, strings.Repeat("\t", len(markers))) } - case *ast.CodeBlock: - indent := strings.Repeat("\t", len(markers)+1) // Indent if in a list, plus one more since it's a code block. + case *ast.CodeBlock, *ast.FencedCodeBlock: + // Code blocks are printed as is in plain text. for i := 0; i < n.Lines().Len(); i++ { s := n.Lines().At(i) - fmt.Fprint(w, indent, string(source[s.Start:s.Stop])) + if i != 0 { + // If we're in a list, indent inner lines accordingly. + fmt.Fprint(w, strings.Repeat("\t", len(markers))) + } + fmt.Fprint(w, string(source[s.Start:s.Stop])) } case *ast.AutoLink: // Auto-links are printed as is in plain text. diff --git a/internal/task/announce_test.go b/internal/task/announce_test.go index c0eed11c..79492fdc 100644 --- a/internal/task/announce_test.go +++ b/internal/task/announce_test.go @@ -401,6 +401,20 @@ There may be security fixes following the [security policy](https://go.dev/secur Some description of the problem here. + Regular Code Block + Can + Be + Here + + Another paragraph. + + ` + "```" + ` + Fenced Code Block + Can + Be + Here + ` + "```" + ` + Markdown allows one to use backslash escapes, like \_underscore\_ or \*literal asterisks\*, so we might encounter that. @@ -415,8 +429,11 @@ To builds from source, use An easy way to try go1.19beta1 is by using the go command: + +` + "```" + ` $ go install example.org@latest $ example download +` + "```" + ` That's all for now. ` @@ -445,6 +462,18 @@ There may be security fixes following the security policy