Skip to content

Commit

Permalink
fix: ignore links in title
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasHiltl committed Nov 9, 2024
1 parent bdf176a commit 7f03379
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 16 deletions.
19 changes: 19 additions & 0 deletions .testdata/keepachangelog/git-cliff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[![animation](https://raw.githubusercontent.com/orhun/git-cliff/main/website/static/img/git-cliff-anim.gif)](https://git-cliff.org)

## [2.6.1](https://github.com/orhun/git-cliff/compare/v2.6.0..v2.6.1) - 2024-09-27

### 🐛 Bug Fixes

- *(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))
- *(remote)* Avoid setting multiple remotes ([#885](https://github.com/orhun/git-cliff/issues/885)) - ([a344c68](https://github.com/orhun/git-cliff/commit/a344c68238cf3bb87d4f7eb9be46e97cc964eed9))

### 📚 Documentation

- *(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))
- *(website)* Add get_env filter example for GitLab CI - ([dfe4459](https://github.com/orhun/git-cliff/commit/dfe4459c5cadd465dec4ea860580ecf82b2b8860))

### ⚙️ Miscellaneous Tasks

- *(ci)* Update pedantic lint command ([#890](https://github.com/orhun/git-cliff/issues/890)) - ([8d10edb](https://github.com/orhun/git-cliff/commit/8d10edb7450aaf189fbce5f78a72274739f73ba9))
- *(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))
- *(fixtures)* Build binaries using dev profile ([#886](https://github.com/orhun/git-cliff/issues/886)) - ([a394f88](https://github.com/orhun/git-cliff/commit/a394f88f1d1742dfa3d30887bcb387361de306bc))
47 changes: 31 additions & 16 deletions internal/changelog/keepachangelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ func (g *kparser) parse(read string, rest io.ReadCloser, page Pagination) ParseR
read = ""
}

if strings.HasPrefix(section, "# ") {
// ignore the section with the changelog title
if !strings.HasPrefix(section, "## ") {
// continue if the section doesn't start with ##
continue
}

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

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

a := ParsedArticle{
Meta: Meta{
Title: title,
ID: strings.ToLower(title),
Title: title,
ID: strings.ToLower(title),
PublishedAt: releaseDate,
},
}

if len(h2Parts) > 1 {
publishedAt, err := time.Parse("2006-01-02", strings.TrimSpace(h2Parts[1]))
if err == nil {
a.Meta.PublishedAt = publishedAt
}
}

sc := bufio.NewScanner(strings.NewReader(content))
for sc.Scan() {
line := sc.Text()
Expand All @@ -122,10 +116,31 @@ func (g *kparser) parseRelease(release string) (ParsedArticle, error) {
return a, nil
}

func parseReleaseDate(firstLine string) time.Time {
parts := strings.SplitN(strings.TrimPrefix(firstLine, "## "), " - ", 2)
if len(parts) > 1 {
publishedAt, err := time.Parse("2006-01-02", strings.TrimSpace(parts[1]))
if err == nil {
return publishedAt
}
}
return time.Time{}
}

func parseTitle(firstLine string) string {
h2Parts := strings.SplitN(strings.TrimPrefix(firstLine, "## "), " - ", 2)
return cleanTitle(h2Parts[0])
}

func cleanTitle(title string) string {
title = strings.TrimSpace(title)
title = strings.Replace(title, "[", "", 1)
return strings.Replace(title, "]", "", 1)
// remove markdown link if present
if idx := strings.Index(title, "]("); idx != -1 {
title = title[:idx]
}

// remove "["" and "]""
title = strings.Trim(title, "[]")
return strings.TrimSpace(title)
}

func splitOnRelease(data []byte, atEOF bool) (advance int, token []byte, err error) {
Expand Down
59 changes: 59 additions & 0 deletions internal/changelog/keepachangelog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,27 @@ func TestKParseFull(t *testing.T) {
}
}

func TestKParseGitCliff(t *testing.T) {
p := NewKeepAChangelogParser(createGoldmark())
file, read, err := openKTestDataAndDetect("git-cliff")
if err != nil {
t.Fatal(err)
}

parsed := p.parse(read, file, NoPagination())
if len(parsed.Articles) != 1 {
t.Fatalf("Expected 1 parsed article but got %d", len(parsed.Articles))
}

article := parsed.Articles[0]

expectedTitle := "2.6.1"
if article.Meta.Title != expectedTitle {
t.Errorf("Expected %s to equal %s", article.Meta.Title, expectedTitle)
}

}

func TestKParsePagination(t *testing.T) {
p := NewKeepAChangelogParser(createGoldmark())

Expand Down Expand Up @@ -182,3 +203,41 @@ func TestKParsePagination(t *testing.T) {
}
}
}

func TestParseTitle(t *testing.T) {
tables := []struct {
name string
firstLine string
expectedTitle string
}{
{
name: "basic keep a changelog title",
firstLine: "## [2.6.1] - 2024-09-27",
expectedTitle: "2.6.1",
},
{
name: "no []",
firstLine: "## 2.6.1 - 2024-09-27",
expectedTitle: "2.6.1",
},
{
name: "with link",
firstLine: "## [2.6.1](https://github.com/orhun/git-cliff/compare/v2.6.0..v2.6.1) - 2024-09-27",
expectedTitle: "2.6.1",
},
{
name: "no release date",
firstLine: "## [2.6.1]",
expectedTitle: "2.6.1",
},
}

for _, table := range tables {
t.Run(table.name, func(t *testing.T) {
title := parseTitle(table.firstLine)
if title != table.expectedTitle {
t.Errorf("Expected %s to be %s", title, table.expectedTitle)
}
})
}
}

0 comments on commit 7f03379

Please sign in to comment.