Skip to content

Commit

Permalink
refactor function names for auto ref links
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysd committed Sep 30, 2024
1 parent 753ba06 commit 832d004
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
13 changes: 5 additions & 8 deletions reflink.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,7 @@ func (l *Reflinker) linkCommitSHA(begin, end int) int {
return begin + hashLen
}

// DetectLinksInText detects reference links in given markdown text and remembers them to replace all
// references later.
func (l *Reflinker) DetectLinksInText(t *ast.Text) {
func (l *Reflinker) linkGitHubRefs(t *ast.Text) {
o := t.Segment.Start // start offset

for o < t.Segment.Stop-1 { // `-1` means the last character is not checked
Expand All @@ -216,7 +214,7 @@ func (l *Reflinker) DetectLinksInText(t *ast.Text) {

var reGitHubCommitURL = regexp.MustCompile(`^https://github\.com/([^/]+/[^/]+)/commit/([[:xdigit:]]{7,})`)

func (l *Reflinker) detectGitHubURLInAutoLink(n *ast.AutoLink, src []byte) {
func (l *Reflinker) linkGitHubURL(n *ast.AutoLink, src []byte) {
start := 0
p := n.PreviousSibling()
if p != nil {
Expand Down Expand Up @@ -261,8 +259,7 @@ func (l *Reflinker) detectGitHubURLInAutoLink(n *ast.AutoLink, src []byte) {
})
}

// BuildLinkedText builds a markdown text where all references are replaced with links. The links were
// detected by DetectLinksInText() method calls.
// BuildLinkedText builds a markdown text linking all references.
func (l *Reflinker) BuildLinkedText() string {
if len(l.links) == 0 {
return string(l.src)
Expand Down Expand Up @@ -300,10 +297,10 @@ func LinkRefs(input string, repoURL string) string {
case *ast.CodeSpan, *ast.Link:
return ast.WalkSkipChildren, nil
case *ast.AutoLink:
l.detectGitHubURLInAutoLink(n, src)
l.linkGitHubURL(n, src)
return ast.WalkSkipChildren, nil
case *ast.Text:
l.DetectLinksInText(n)
l.linkGitHubRefs(n)
return ast.WalkContinue, nil
default:
return ast.WalkContinue, nil
Expand Down
4 changes: 2 additions & 2 deletions reflink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,8 @@ func TestLinkRefs(t *testing.T) {
},
{
what: "commit URL at end of text",
input: "https://github.com/foo/bar/commit/1d457ba853aa10f9a6c925a1b73d5aed38066ffe test",
want: "[`foo/bar@1d457ba853`](https://github.com/foo/bar/commit/1d457ba853aa10f9a6c925a1b73d5aed38066ffe) test",
input: "test https://github.com/foo/bar/commit/1d457ba853aa10f9a6c925a1b73d5aed38066ffe",
want: "tes [`foo/bar@1d457ba853`](https://github.com/foo/bar/commit/1d457ba853aa10f9a6c925a1b73d5aed38066ffe)",
},
{
what: "commit URL with explicit auto link",
Expand Down

0 comments on commit 832d004

Please sign in to comment.