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 <rfindley@google.com>
Reviewed-on: https://go-review.googlesource.com/c/build/+/593057
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Auto-Submit: Dmitri Shuralyov <dmitshur@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Dmitri Shuralyov 2024-06-17 15:16:07 -04:00 коммит произвёл Gopher Robot
Родитель ce6736d4c1
Коммит b6ea2cc642
2 изменённых файлов: 40 добавлений и 6 удалений

Просмотреть файл

@ -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 <URL>").
// `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 <URL>").
//
// 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.

Просмотреть файл

@ -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 <https://go.dev/securi
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.
@ -459,6 +488,7 @@ git checkout.
An easy way to try go1.19beta1
is by using the go command:
$ go install example.org@latest
$ example download