From 99606a15b9d8aa581fed8972c04743c47aabd45a Mon Sep 17 00:00:00 2001 From: rhysd Date: Sat, 5 Oct 2024 08:03:53 -0700 Subject: [PATCH] refactor `Reflinker` to reuse for all release notes --- changelog.go | 3 ++- reflink.go | 32 +++++++++++++++----------------- reflink_test.go | 3 ++- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/changelog.go b/changelog.go index ccf6b41..957e6d5 100644 --- a/changelog.go +++ b/changelog.go @@ -54,6 +54,7 @@ func (cl *ChangeLog) Generate(rels []*github.RepositoryRelease) error { out := bufio.NewWriter(cl.out) heading := strings.Repeat("#", cl.level) + linker := NewReflinker(cl.repoURL) numRels := len(rels) relLinks := make([]link, 0, numRels) @@ -99,7 +100,7 @@ func (cl *ChangeLog) Generate(rels []*github.RepositoryRelease) error { } fmt.Fprintf(out, "%s [%s](%s) - %s\n\n", heading, title, pageURL, created.Format("02 Jan 2006")) - fmt.Fprint(out, LinkRefs(strings.Replace(rel.GetBody(), "\r", "", -1), cl.repoURL)) + fmt.Fprint(out, linker.Link(strings.Replace(rel.GetBody(), "\r", "", -1))) fmt.Fprintf(out, "\n\n[Changes][%s]\n\n\n", tag) relLinks = append(relLinks, link{tag, compareURL}) diff --git a/reflink.go b/reflink.go index b09839e..9c49d37 100644 --- a/reflink.go +++ b/reflink.go @@ -66,7 +66,7 @@ type Reflinker struct { // NewReflinker creates Reflinker instance. repoURL is a repository URL of the service like // https://github.com/user/repo. -func NewReflinker(repoURL string, src []byte) *Reflinker { +func NewReflinker(repoURL string) *Reflinker { u, err := url.Parse(repoURL) if err != nil { panic(err) @@ -74,15 +74,18 @@ func NewReflinker(repoURL string, src []byte) *Reflinker { u.Path = "" l := &Reflinker{ - repo: repoURL, - home: u.String(), - src: src, - links: nil, + repo: repoURL, + home: u.String(), } l.AddExtRef("GH-", repoURL+"/issues/", false) return l } +func (l *Reflinker) reset(src []byte) { + l.src = src + l.links = nil +} + func (l *Reflinker) isBoundaryAt(idx int) bool { if idx < 0 || len(l.src) <= idx { return true @@ -371,11 +374,7 @@ func (l *Reflinker) linkURL(n *ast.AutoLink) { } } -// BuildLinkedText builds a markdown text linking all references. -func (l *Reflinker) BuildLinkedText() string { - if len(l.links) == 0 { - return string(l.src) - } +func (l *Reflinker) buildLinkedText() string { sort.Sort(byStart(l.links)) var b strings.Builder @@ -389,17 +388,16 @@ func (l *Reflinker) BuildLinkedText() string { return b.String() } -// IsLinkDetected returns whether one or more links were detected. -func (l *Reflinker) IsLinkDetected() bool { +func (l *Reflinker) isLinkDetected() bool { return len(l.links) > 0 } -// LinkRefs replaces all references in the given markdown text with actual links. -func LinkRefs(input string, repoURL string) string { +// Link replaces all references in the given markdown text with actual links. +func (l *Reflinker) Link(input string) string { src := []byte(input) md := goldmark.New(goldmark.WithExtensions(extension.GFM)) t := md.Parser().Parse(text.NewReader(src)) - l := NewReflinker(repoURL, src) + l.reset(src) ast.Walk(t, func(n ast.Node, entering bool) (ast.WalkStatus, error) { if !entering { @@ -421,9 +419,9 @@ func LinkRefs(input string, repoURL string) string { } }) - if !l.IsLinkDetected() { + if !l.isLinkDetected() { return input } - return l.BuildLinkedText() + return l.buildLinkedText() } diff --git a/reflink_test.go b/reflink_test.go index a168b71..e658f07 100644 --- a/reflink_test.go +++ b/reflink_test.go @@ -570,7 +570,8 @@ func TestLinkRefs(t *testing.T) { if tc.repoURL != "" { u = tc.repoURL } - have := LinkRefs(tc.input, u) + l := NewReflinker(u) + have := l.Link(tc.input) if have != tc.want { t.Fatalf("wanted %q but got %q", tc.want, have) }