Skip to content

Commit

Permalink
Merge pull request #20 from git-chglog/feat/kac-template-title-id
Browse files Browse the repository at this point in the history
Add tag name header id for keep-a-changelog template
  • Loading branch information
wadackel authored May 4, 2018
2 parents 774bdce + bcfe4d2 commit c36bc89
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ The basic templates are as follows.

```markdown
{{ if .Versions -}}
<a name="unreleased"></a>
## [Unreleased]
{{ if .Unreleased.CommitGroups -}}
Expand All @@ -413,6 +414,7 @@ The basic templates are as follows.
{{ end -}}
{{ range .Versions }}
<a name="{{ .Tag.Name }}"></a>
## {{ if .Tag.Previous }}[{{ .Tag.Name }}]{{ else }}{{ .Tag.Name }}{{ end }} - {{ datetime "2006-01-02" .Tag.Date }}
{{ range .CommitGroups -}}
### {{ .Title }}
Expand Down
4 changes: 2 additions & 2 deletions cmd/git-chglog/custom_template_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ func (*customTemplateBuilderImpl) versionHeader(style, template string) string {
// parts
switch style {
case styleGitHub, styleGitLab:
tpl = "<a name=\"{{ .Tag.Name }}\"></a>\n"
tpl = templateTagNameAnchor
tagName = "{{ if .Tag.Previous }}[{{ .Tag.Name }}]({{ $.Info.RepositoryURL }}/compare/{{ .Tag.Previous.Name }}...{{ .Tag.Name }}){{ else }}{{ .Tag.Name }}{{ end }}"
case styleBitbucket:
tpl = "<a name=\"{{ .Tag.Name }}\"></a>\n"
tpl = templateTagNameAnchor
tagName = "{{ if .Tag.Previous }}[{{ .Tag.Name }}]({{ $.Info.RepositoryURL }}/compare/{{ .Tag.Name }}..{{ .Tag.Previous.Name }}){{ else }}{{ .Tag.Name }}{{ end }}"
}

Expand Down
10 changes: 7 additions & 3 deletions cmd/git-chglog/kac_template_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,36 +46,40 @@ func (t *kacTemplateBuilderImpl) Build(ans *Answer) (string, error) {

func (t *kacTemplateBuilderImpl) unreleased(style, format string) string {
var (
id = ""
title = "Unreleased"
commits = t.commits(".Unreleased.CommitGroups", format)
)

switch style {
case styleGitHub, styleGitLab, styleBitbucket:
id = "<a name=\"unreleased\"></a>\n"
title = fmt.Sprintf("[%s]", title)
}

return fmt.Sprintf(`{{ if .Versions -}}
## %s
%s## %s
{{ if .Unreleased.CommitGroups -}}
%s{{ end -}}
{{ end -}}
`, title, commits)
`, id, title, commits)
}

func (t *kacTemplateBuilderImpl) versionHeader(style string) string {
var (
id = ""
tagName = "{{ .Tag.Name }}"
date = "{{ datetime \"2006-01-02\" .Tag.Date }}"
)

switch style {
case styleGitHub, styleGitLab, styleBitbucket:
id = templateTagNameAnchor
tagName = "{{ if .Tag.Previous }}[{{ .Tag.Name }}]{{ else }}{{ .Tag.Name }}{{ end }}"
}

return fmt.Sprintf("## %s - %s\n", tagName, date)
return fmt.Sprintf("%s## %s - %s\n", id, tagName, date)
}

func (t *kacTemplateBuilderImpl) commits(commitGroups, format string) string {
Expand Down
2 changes: 2 additions & 0 deletions cmd/git-chglog/kac_template_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func TestKACTemplateBuilderDefault(t *testing.T) {

assert.Nil(err)
assert.Equal(`{{ if .Versions -}}
<a name="unreleased"></a>
## [Unreleased]
{{ if .Unreleased.CommitGroups -}}
Expand All @@ -33,6 +34,7 @@ func TestKACTemplateBuilderDefault(t *testing.T) {
{{ end -}}
{{ range .Versions }}
<a name="{{ .Tag.Name }}"></a>
## {{ if .Tag.Previous }}[{{ .Tag.Name }}]{{ else }}{{ .Tag.Name }}{{ end }} - {{ datetime "2006-01-02" .Tag.Date }}
{{ range .CommitGroups -}}
### {{ .Title }}
Expand Down
2 changes: 2 additions & 0 deletions cmd/git-chglog/template_builder.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package main

const templateTagNameAnchor = "<a name=\"{{ .Tag.Name }}\"></a>\n"

// TemplateBuilder ...
type TemplateBuilder interface {
Builder
Expand Down

0 comments on commit c36bc89

Please sign in to comment.