Skip to content

Commit

Permalink
Support inline comments variation for Markdown and AsciiDoc
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
khos2ow committed Apr 13, 2021
1 parent 4031c66 commit 0eea133
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
34 changes: 34 additions & 0 deletions docs/user-guide/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'`
- `[//]: # (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
Expand Down
30 changes: 28 additions & 2 deletions internal/cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,19 +199,45 @@ 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], "<!--") || !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], "<!--") || !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])
}
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, "<!--") && strings.HasSuffix(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"`
Expand Down

0 comments on commit 0eea133

Please sign in to comment.