Repository: terraform-docs/terraform-docs (go)

Issue:
Formatting issue indented code snippets
<!--
Thank you for helping to improve terraform-docs!

Please be sure to search for open issues before raising a new one. We use issues
for bug reports and feature requests. Please note, this template is for bugs
report, not feature requests. For more information, see the Contributing Guidelines
at https://git.io/JtEzg. Please find us at https://slack.terraform-docs.io/ for
questions, support, and discussion.
-->

### Describe the bug

<!--
Please let us know what behaviour you expected and how terraform-docs diverged
from that behaviour.
-->
Code snippets that use "```" to format the code break the generated markdown when the code snippet has been indented, for instance when used in a list.

### How can we reproduce it?

<!--
Help us to reproduce your bug as succinctly and precisely as possible. Any and
all steps or script that triggers the issue are highly appreciated!

Do you have long logs to share? Please go to https://ghostbin.com and insert the
link here.
-->
The following code breaks the formatting of the markdown after the code snippet:

```md
1. Entry one with code

    ```json
    {
      "key": "value"
    }
    ```

1. This line is now broken
```

The generated code looks like this:
````md
1. Entry one with code

    ```json
    {
      "key": "value"
    }

```

1. This line is now broken
````

I.e. a line break has been added before the ``` and the spaces were removed.

### Environment information

<!--
- terraform-docs version (use `terraform-docs --version`):
- Go version (if you manually built. use `go version`):
- OS (e.g. Windows, MacOS):
-->
terraform-docs: v0.14.1
Go: go version go1.16.5 linux/amd64
OS: Linux/Ubuntu (Windows WSL)

Pull request:
Properly format indented code blocks
<!--
Thank you for helping to improve terraform-docs!

Please read through https://git.io/JtEzg if this is your first time opening a
terraform-docs pull request. Find us in https://slack.terraform-docs.io/messages/dev if
you need any help contributing.
-->

### Description of your changes

<!--
Briefly describe what this pull request does. Be sure to direct your reviewers'
attention to anything that needs special consideration.

We love pull requests that resolve an open terraform-docs issue. If yours does, you
can uncomment the below line to indicate which issue your PR fixes, for example
"Fixes #500":
-->

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.


Fixes #521 

I have:

- [x] Read and followed terraform-docs' [contribution process].
- [x] All tests pass when I run `make test`.

### How has this code been tested

<!--
Before reviewers can be confident in the correctness of this pull request, it
needs to tested and shown to be correct. Briefly describe the testing that has
already been done or which is planned for this change.
-->

Updated `complex.golden` test case to incorporate an indented code block and make sure the tests are passing.

[contribution process]: https://git.io/JtEzg


Patch:
diff --git a/internal/template/sanitizer.go b/internal/template/sanitizer.go
index 75633f4..7bbbdb1 100644
--- a/internal/template/sanitizer.go
+++ b/internal/template/sanitizer.go
@@ -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
 		},
 	)


Test patch:
diff --git a/internal/template/testdata/section/complex.expected b/internal/template/testdata/section/complex.expected
index d284951..cebd4ac 100644
--- a/internal/template/testdata/section/complex.expected
+++ b/internal/template/testdata/section/complex.expected
@@ -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.
 
diff --git a/internal/template/testdata/section/complex.golden b/internal/template/testdata/section/complex.golden
index d3b77d3..5ccbb4a 100644
--- a/internal/template/testdata/section/complex.golden
+++ b/internal/template/testdata/section/complex.golden
@@ -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.