Skip to content

Commit

Permalink
Prefix links to wiki pages from comments in ticket (#8)
Browse files Browse the repository at this point in the history
Signed-off-by: Benoit Donneaux <[email protected]>
  • Loading branch information
btlogy authored Jun 13, 2024
1 parent 7f291e8 commit 450ee29
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions markdown/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,25 +256,31 @@ func (converter *DefaultConverter) resolveTicketLink(link string) string {
return markLink(issueURL)
}

func (converter *DefaultConverter) resolveWikiLink(link string) string {
func (converter *DefaultConverter) resolveWikiLink(path string, link string) string {
wikiPageName := wikiLinkRegexp.ReplaceAllString(link, `$1`)
wikiPageAnchor := wikiLinkRegexp.ReplaceAllString(link, `$2`)
translatedPageName := converter.giteaAccessor.TranslateWikiPageName(wikiPageName)
var suffix string
if wikiPageAnchor == "" {
return markLink(translatedPageName)
suffix = ""
} else {
suffix = "#" + wikiPageAnchor
}
return markLink(translatedPageName + "#" + wikiPageAnchor)
return markLink(path + translatedPageName + suffix)
}

func (converter *DefaultConverter) resolveWikiCamelCaseLink(link string) string {
func (converter *DefaultConverter) resolveWikiCamelCaseLink(path string, link string) string {
leadingChar := wikiCamelCaseLinkRegexp.ReplaceAllString(link, `$1`)
wikiPageName := wikiCamelCaseLinkRegexp.ReplaceAllString(link, `$2`)
wikiPageAnchor := wikiCamelCaseLinkRegexp.ReplaceAllString(link, `$3`)
translatedPageName := converter.giteaAccessor.TranslateWikiPageName(wikiPageName)
var suffix string
if wikiPageAnchor == "" {
return leadingChar + markLink(translatedPageName)
suffix = ""
} else {
suffix = "#" + wikiPageAnchor
}
return leadingChar + markLink(translatedPageName+"#"+wikiPageAnchor)
return leadingChar + markLink(path + translatedPageName + suffix)
}

// convertBrackettedTracLinks converts the various forms of (square) bracketted Trac links into an unbracketted form.
Expand Down Expand Up @@ -374,12 +380,17 @@ func (converter *DefaultConverter) convertUnbrackettedTracLinks(ticketID int64,
return converter.resolveTicketLink(match)
})

// add a 'wiki/' path for link from ticket
var path = ""
if ticketID != trac.NullID {
path = "wiki/"
}
out = wikiLinkRegexp.ReplaceAllStringFunc(out, func(match string) string {
return converter.resolveWikiLink(match)
return converter.resolveWikiLink(path, match)
})

out = wikiCamelCaseLinkRegexp.ReplaceAllStringFunc(out, func(match string) string {
return converter.resolveWikiCamelCaseLink(match)
return converter.resolveWikiCamelCaseLink(path, match)
})

return out
Expand Down

0 comments on commit 450ee29

Please sign in to comment.