Skip to content

Commit

Permalink
refactor Reflinker to reuse for all release notes
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysd committed Oct 5, 2024
1 parent 249bbd1 commit 99606a1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
3 changes: 2 additions & 1 deletion changelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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})
Expand Down
32 changes: 15 additions & 17 deletions reflink.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,23 +66,26 @@ 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)
}
u.Path = ""

l := &Reflinker{
repo: repoURL,
home: u.String(),
src: src,
links: nil,
repo: repoURL,
home: u.String(),
}
l.AddExtRef("GH-", repoURL+"/issues/<num>", 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
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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()
}
3 changes: 2 additions & 1 deletion reflink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 99606a1

Please sign in to comment.