Skip to content

Commit 0eea133

Browse files
committed
Support inline comments variation for Markdown and AsciiDoc
Some of the Markdown engines (e.g. Bitbucket Cloud) are not able to process HTML comment but as a workaround they support variation of inline comments as follow: - `<!-- This is a comment -->` - `[]: # (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` Signed-off-by: Khosrow Moossavi <khos2ow@gmail.com>
1 parent 4031c66 commit 0eea133

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

docs/user-guide/configuration.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,40 @@ output:
197197
{{ .Content }}
198198
```
199199

200+
### Template Comment
201+
202+
Markdown doesn't officially support inline commenting, there are multiple ways
203+
to do it as a workaround, though. The following formats are supported as begin
204+
and end comments of a template:
205+
206+
- `<!-- This is a comment -->`
207+
- `[]: # (This is a comment)`
208+
- `[]: # "This is a comment"`
209+
- `[]: # 'This is a comment'`
210+
- `[//]: # (This is a comment)`
211+
- `[comment]: # (This is a comment)`
212+
213+
The following is also supported for AsciiDoc format:
214+
215+
- `// This is a comment`
216+
217+
The following can be used where HTML comments are not supported (e.g. Bitbucket
218+
Cloud):
219+
220+
```yaml
221+
output:
222+
file: README.md
223+
mode: inject
224+
template: |-
225+
[//]: # (BEGIN_TF_DOCS)
226+
{{ .Content }}
227+
228+
[//]: # (END_TF_DOCS)
229+
```
230+
231+
Note: The empty line before `[//]: # (END_TF_DOCS)` is mandatory in order for
232+
Markdown engine to properly process the comment line after the paragraph.
233+
200234
## Sort
201235

202236
To enable sorting of elements `sort.enabled` (or `--sort bool` CLI flag) can be

internal/cli/config.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,19 +199,45 @@ func (o *output) validate() error { //nolint:gocyclo
199199
return fmt.Errorf("value of '--output-template' should contain at least 3 lines (begin comment, {{ .Content }}, and end comment)")
200200
}
201201

202-
if !strings.Contains(lines[0], "<!--") || !strings.Contains(lines[0], "-->") {
202+
if !isInlineComment(lines[0]) {
203203
return fmt.Errorf("value of '--output-template' is missing begin comment")
204204
}
205205
o.BeginComment = strings.TrimSpace(lines[0])
206206

207-
if !strings.Contains(lines[len(lines)-1], "<!--") || !strings.Contains(lines[len(lines)-1], "-->") {
207+
if !isInlineComment(lines[len(lines)-1]) {
208208
return fmt.Errorf("value of '--output-template' is missing end comment")
209209
}
210210
o.EndComment = strings.TrimSpace(lines[len(lines)-1])
211211
}
212212
return nil
213213
}
214214

215+
// Detect if a particular line is a Markdown comment
216+
//
217+
// ref: https://www.jamestharpe.com/markdown-comments/
218+
func isInlineComment(line string) bool {
219+
switch {
220+
// Markdown specific
221+
case strings.HasPrefix(line, "<!--") && strings.HasSuffix(line, "-->"):
222+
return true
223+
case strings.HasPrefix(line, "[]: # ("):
224+
return true
225+
case strings.HasPrefix(line, "[]: # \""):
226+
return true
227+
case strings.HasPrefix(line, "[]: # '"):
228+
return true
229+
case strings.HasPrefix(line, "[//]: # ("):
230+
return true
231+
case strings.HasPrefix(line, "[comment]: # ("):
232+
return true
233+
234+
// AsciiDoc specific
235+
case strings.HasPrefix(line, "//"):
236+
return true
237+
}
238+
return false
239+
}
240+
215241
type outputvalues struct {
216242
Enabled bool `yaml:"enabled"`
217243
From string `yaml:"from"`

0 commit comments

Comments
 (0)