Skip to content

Commit 12c0487

Browse files
authored
Make cross-reference issue links work in markdown documents again (#28682)
In #26365 issue references were disabled entirely for documents, intending to match GitHub behavior. However cross-references do appear to work in documents on GitHub. This is useful for example to write release notes in a markdown document and reference issues. While the simpler syntax may create links when not intended, hopefully the cross-reference syntax is unique enough to avoid it.
1 parent 91aa263 commit 12c0487

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

modules/markup/html.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -852,11 +852,14 @@ func fullIssuePatternProcessor(ctx *RenderContext, node *html.Node) {
852852
}
853853

854854
func issueIndexPatternProcessor(ctx *RenderContext, node *html.Node) {
855-
// FIXME: the use of "mode" is quite dirty and hacky, for example: what is a "document"? how should it be rendered?
856-
// The "mode" approach should be refactored to some other more clear&reliable way.
857-
if ctx.Metas == nil || (ctx.Metas["mode"] == "document" && !ctx.IsWiki) {
855+
if ctx.Metas == nil {
858856
return
859857
}
858+
859+
// FIXME: the use of "mode" is quite dirty and hacky, for example: what is a "document"? how should it be rendered?
860+
// The "mode" approach should be refactored to some other more clear&reliable way.
861+
crossLinkOnly := (ctx.Metas["mode"] == "document" && !ctx.IsWiki)
862+
860863
var (
861864
found bool
862865
ref *references.RenderizableReference
@@ -870,7 +873,7 @@ func issueIndexPatternProcessor(ctx *RenderContext, node *html.Node) {
870873
// Repos with external issue trackers might still need to reference local PRs
871874
// We need to concern with the first one that shows up in the text, whichever it is
872875
isNumericStyle := ctx.Metas["style"] == "" || ctx.Metas["style"] == IssueNameStyleNumeric
873-
foundNumeric, refNumeric := references.FindRenderizableReferenceNumeric(node.Data, hasExtTrackFormat && !isNumericStyle)
876+
foundNumeric, refNumeric := references.FindRenderizableReferenceNumeric(node.Data, hasExtTrackFormat && !isNumericStyle, crossLinkOnly)
874877

875878
switch ctx.Metas["style"] {
876879
case "", IssueNameStyleNumeric:

modules/markup/html_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,11 +561,16 @@ func TestPostProcess_RenderDocument(t *testing.T) {
561561
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(res.String()))
562562
}
563563

564-
// Issue index shouldn't be post processing in an document.
564+
// Issue index shouldn't be post processing in a document.
565565
test(
566566
"#1",
567567
"#1")
568568

569+
// But cross-referenced issue index should work.
570+
test(
571+
"go-gitea/gitea#12345",
572+
`<a href="`+util.URLJoin(markup.TestAppURL, "go-gitea", "gitea", "issues", "12345")+`" class="ref-issue">go-gitea/gitea#12345</a>`)
573+
569574
// Test that other post processing still works.
570575
test(
571576
":gitea:",

modules/references/references.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,11 @@ func FindAllIssueReferences(content string) []IssueReference {
331331
}
332332

333333
// FindRenderizableReferenceNumeric returns the first unvalidated reference found in a string.
334-
func FindRenderizableReferenceNumeric(content string, prOnly bool) (bool, *RenderizableReference) {
335-
match := issueNumericPattern.FindStringSubmatchIndex(content)
334+
func FindRenderizableReferenceNumeric(content string, prOnly, crossLinkOnly bool) (bool, *RenderizableReference) {
335+
var match []int
336+
if !crossLinkOnly {
337+
match = issueNumericPattern.FindStringSubmatchIndex(content)
338+
}
336339
if match == nil {
337340
if match = crossReferenceIssueNumericPattern.FindStringSubmatchIndex(content); match == nil {
338341
return false, nil

0 commit comments

Comments
 (0)