Properly format indented code blocks

If a code block (surrounded by triple backticks) are indented, in other
words the code block is placed inside ordered or unordered list, the
closing backticks of codeblock should honor the original indentation
properly.

Signed-off-by: Khosrow Moossavi <khos2ow@gmail.com>
This commit is contained in:
Khosrow Moossavi
2021-07-28 19:23:44 -04:00
parent 5a4a4aa034
commit c14edafbf0
3 changed files with 30 additions and 1 deletions

View File

@@ -55,7 +55,16 @@ func sanitizeSection(s string, settings *print.Settings) string {
if !strings.HasSuffix(segment, "\n") {
lastbreak = "\n"
}
segment = fmt.Sprintf("```%s%s```", segment, lastbreak)
// Adjust indention and linebreak for indented codeblock
// https://github.com/terraform-docs/terraform-docs/issues/521
lastindent := ""
lines := strings.Split(segment, "\n")
if len(strings.TrimSpace(lines[len(lines)-1])) == 0 {
lastbreak = ""
}
segment = fmt.Sprintf("```%s%s%s```", segment, lastindent, lastbreak)
return segment
},
)

View File

@@ -28,6 +28,16 @@ module "foo_bar" {
}
```
1. Entry one with code
```json
{
"key": "value"
}
```
1. This line should not be broken
Here is some trailing text after code block,
followed by another line of text.

View File

@@ -28,6 +28,16 @@ module "foo_bar" {
}
```
1. Entry one with code
```json
{
"key": "value"
}
```
1. This line should not be broken
Here is some trailing text after code block,
followed by another line of text.