diff --git a/docs/user-guide/configuration.md b/docs/user-guide/configuration.md index 6b14527..08660fd 100644 --- a/docs/user-guide/configuration.md +++ b/docs/user-guide/configuration.md @@ -197,6 +197,40 @@ output: {{ .Content }} ``` +### Template Comment + +Markdown doesn't officially support inline commenting, there are multiple ways +to do it as a workaround, though. The following formats are supported as begin +and end comments of a template: + +- `` +- `[]: # (This is a comment)` +- `[]: # "This is a comment"` +- `[]: # 'This is a comment'` +- `[//]: # (This is a comment)` +- `[comment]: # (This is a comment)` + +The following is also supported for AsciiDoc format: + +- `// This is a comment` + +The following can be used where HTML comments are not supported (e.g. Bitbucket +Cloud): + +```yaml +output: + file: README.md + mode: inject + template: |- + [//]: # (BEGIN_TF_DOCS) + {{ .Content }} + + [//]: # (END_TF_DOCS) +``` + +Note: The empty line before `[//]: # (END_TF_DOCS)` is mandatory in order for +Markdown engine to properly process the comment line after the paragraph. + ## Sort To enable sorting of elements `sort.enabled` (or `--sort bool` CLI flag) can be diff --git a/internal/cli/config.go b/internal/cli/config.go index e9e0699..e3e4d63 100644 --- a/internal/cli/config.go +++ b/internal/cli/config.go @@ -199,12 +199,12 @@ func (o *output) validate() error { //nolint:gocyclo return fmt.Errorf("value of '--output-template' should contain at least 3 lines (begin comment, {{ .Content }}, and end comment)") } - if !strings.Contains(lines[0], "") { + if !isInlineComment(lines[0]) { return fmt.Errorf("value of '--output-template' is missing begin comment") } o.BeginComment = strings.TrimSpace(lines[0]) - if !strings.Contains(lines[len(lines)-1], "") { + if !isInlineComment(lines[len(lines)-1]) { return fmt.Errorf("value of '--output-template' is missing end comment") } o.EndComment = strings.TrimSpace(lines[len(lines)-1]) @@ -212,6 +212,32 @@ func (o *output) validate() error { //nolint:gocyclo return nil } +// Detect if a particular line is a Markdown comment +// +// ref: https://www.jamestharpe.com/markdown-comments/ +func isInlineComment(line string) bool { + switch { + // Markdown specific + case strings.HasPrefix(line, ""): + return true + case strings.HasPrefix(line, "[]: # ("): + return true + case strings.HasPrefix(line, "[]: # \""): + return true + case strings.HasPrefix(line, "[]: # '"): + return true + case strings.HasPrefix(line, "[//]: # ("): + return true + case strings.HasPrefix(line, "[comment]: # ("): + return true + + // AsciiDoc specific + case strings.HasPrefix(line, "//"): + return true + } + return false +} + type outputvalues struct { Enabled bool `yaml:"enabled"` From string `yaml:"from"`