Skip to content

Commit 7f03379

Browse files
committed
fix: ignore links in title
1 parent bdf176a commit 7f03379

File tree

3 files changed

+109
-16
lines changed

3 files changed

+109
-16
lines changed

.testdata/keepachangelog/git-cliff.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[![animation](https://raw.githubusercontent.com/orhun/git-cliff/main/website/static/img/git-cliff-anim.gif)](https://git-cliff.org)
2+
3+
## [2.6.1](https://github.com/orhun/git-cliff/compare/v2.6.0..v2.6.1) - 2024-09-27
4+
5+
### 🐛 Bug Fixes
6+
7+
- *(npm)* Add missing `--use-branch-tags` flag to TS options ([#874](https://github.com/orhun/git-cliff/issues/874)) - ([e21fb1d](https://github.com/orhun/git-cliff/commit/e21fb1d3895d893fd6a371ecd48aa4632cf4231d))
8+
- *(remote)* Avoid setting multiple remotes ([#885](https://github.com/orhun/git-cliff/issues/885)) - ([a344c68](https://github.com/orhun/git-cliff/commit/a344c68238cf3bb87d4f7eb9be46e97cc964eed9))
9+
10+
### 📚 Documentation
11+
12+
- *(website)* Add conversion to pdf to tips-and-tricks ([#889](https://github.com/orhun/git-cliff/issues/889)) - ([58dc108](https://github.com/orhun/git-cliff/commit/58dc1087ed86794c2f678707f2fbb8199167b0c3))
13+
- *(website)* Add get_env filter example for GitLab CI - ([dfe4459](https://github.com/orhun/git-cliff/commit/dfe4459c5cadd465dec4ea860580ecf82b2b8860))
14+
15+
### ⚙️ Miscellaneous Tasks
16+
17+
- *(ci)* Update pedantic lint command ([#890](https://github.com/orhun/git-cliff/issues/890)) - ([8d10edb](https://github.com/orhun/git-cliff/commit/8d10edb7450aaf189fbce5f78a72274739f73ba9))
18+
- *(docker)* Disable building arm64 docker images temporarily ([#879](https://github.com/orhun/git-cliff/issues/879)) - ([cde2a8e](https://github.com/orhun/git-cliff/commit/cde2a8e3222f5e8f8bdd9ae841fd0e5c42f68846))
19+
- *(fixtures)* Build binaries using dev profile ([#886](https://github.com/orhun/git-cliff/issues/886)) - ([a394f88](https://github.com/orhun/git-cliff/commit/a394f88f1d1742dfa3d30887bcb387361de306bc))

internal/changelog/keepachangelog.go

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ func (g *kparser) parse(read string, rest io.ReadCloser, page Pagination) ParseR
4848
read = ""
4949
}
5050

51-
if strings.HasPrefix(section, "# ") {
52-
// ignore the section with the changelog title
51+
if !strings.HasPrefix(section, "## ") {
52+
// continue if the section doesn't start with ##
5353
continue
5454
}
5555

@@ -85,23 +85,17 @@ func (g *kparser) parseRelease(release string) (ParsedArticle, error) {
8585
firstLine := release[:firstLineIdx]
8686
content := release[firstLineIdx+1:]
8787

88-
h2Parts := strings.SplitN(strings.TrimPrefix(firstLine, "## "), " - ", 2)
89-
title := cleanTitle(h2Parts[0])
88+
title := parseTitle(firstLine)
89+
releaseDate := parseReleaseDate(firstLine)
9090

9191
a := ParsedArticle{
9292
Meta: Meta{
93-
Title: title,
94-
ID: strings.ToLower(title),
93+
Title: title,
94+
ID: strings.ToLower(title),
95+
PublishedAt: releaseDate,
9596
},
9697
}
9798

98-
if len(h2Parts) > 1 {
99-
publishedAt, err := time.Parse("2006-01-02", strings.TrimSpace(h2Parts[1]))
100-
if err == nil {
101-
a.Meta.PublishedAt = publishedAt
102-
}
103-
}
104-
10599
sc := bufio.NewScanner(strings.NewReader(content))
106100
for sc.Scan() {
107101
line := sc.Text()
@@ -122,10 +116,31 @@ func (g *kparser) parseRelease(release string) (ParsedArticle, error) {
122116
return a, nil
123117
}
124118

119+
func parseReleaseDate(firstLine string) time.Time {
120+
parts := strings.SplitN(strings.TrimPrefix(firstLine, "## "), " - ", 2)
121+
if len(parts) > 1 {
122+
publishedAt, err := time.Parse("2006-01-02", strings.TrimSpace(parts[1]))
123+
if err == nil {
124+
return publishedAt
125+
}
126+
}
127+
return time.Time{}
128+
}
129+
130+
func parseTitle(firstLine string) string {
131+
h2Parts := strings.SplitN(strings.TrimPrefix(firstLine, "## "), " - ", 2)
132+
return cleanTitle(h2Parts[0])
133+
}
134+
125135
func cleanTitle(title string) string {
126-
title = strings.TrimSpace(title)
127-
title = strings.Replace(title, "[", "", 1)
128-
return strings.Replace(title, "]", "", 1)
136+
// remove markdown link if present
137+
if idx := strings.Index(title, "]("); idx != -1 {
138+
title = title[:idx]
139+
}
140+
141+
// remove "["" and "]""
142+
title = strings.Trim(title, "[]")
143+
return strings.TrimSpace(title)
129144
}
130145

131146
func splitOnRelease(data []byte, atEOF bool) (advance int, token []byte, err error) {

internal/changelog/keepachangelog_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,27 @@ func TestKParseFull(t *testing.T) {
110110
}
111111
}
112112

113+
func TestKParseGitCliff(t *testing.T) {
114+
p := NewKeepAChangelogParser(createGoldmark())
115+
file, read, err := openKTestDataAndDetect("git-cliff")
116+
if err != nil {
117+
t.Fatal(err)
118+
}
119+
120+
parsed := p.parse(read, file, NoPagination())
121+
if len(parsed.Articles) != 1 {
122+
t.Fatalf("Expected 1 parsed article but got %d", len(parsed.Articles))
123+
}
124+
125+
article := parsed.Articles[0]
126+
127+
expectedTitle := "2.6.1"
128+
if article.Meta.Title != expectedTitle {
129+
t.Errorf("Expected %s to equal %s", article.Meta.Title, expectedTitle)
130+
}
131+
132+
}
133+
113134
func TestKParsePagination(t *testing.T) {
114135
p := NewKeepAChangelogParser(createGoldmark())
115136

@@ -182,3 +203,41 @@ func TestKParsePagination(t *testing.T) {
182203
}
183204
}
184205
}
206+
207+
func TestParseTitle(t *testing.T) {
208+
tables := []struct {
209+
name string
210+
firstLine string
211+
expectedTitle string
212+
}{
213+
{
214+
name: "basic keep a changelog title",
215+
firstLine: "## [2.6.1] - 2024-09-27",
216+
expectedTitle: "2.6.1",
217+
},
218+
{
219+
name: "no []",
220+
firstLine: "## 2.6.1 - 2024-09-27",
221+
expectedTitle: "2.6.1",
222+
},
223+
{
224+
name: "with link",
225+
firstLine: "## [2.6.1](https://github.com/orhun/git-cliff/compare/v2.6.0..v2.6.1) - 2024-09-27",
226+
expectedTitle: "2.6.1",
227+
},
228+
{
229+
name: "no release date",
230+
firstLine: "## [2.6.1]",
231+
expectedTitle: "2.6.1",
232+
},
233+
}
234+
235+
for _, table := range tables {
236+
t.Run(table.name, func(t *testing.T) {
237+
title := parseTitle(table.firstLine)
238+
if title != table.expectedTitle {
239+
t.Errorf("Expected %s to be %s", title, table.expectedTitle)
240+
}
241+
})
242+
}
243+
}

0 commit comments

Comments
 (0)