Skip to content

Commit 87f7097

Browse files
earl-warrenGusted
andauthored
Do not highlight #number in documents (#26365)
- Currently the post processing will transform all issue indexes (such as `#6`) into a clickable link. - This makes sense in an situation like issues or PRs, where referencing to other issues is quite common and only referencing their issue index is an handy and efficient way to do it. - Currently this is also run for documents (which is the user profile and viewing rendered files), but in those situations it's less common to reference issues by their index and instead could mean something else. - This patch disables this post processing for issue index for documents. Matches Github's behavior. - Added unit tests. - Resolves https://codeberg.org/Codeberg/Community/issues/1120 Co-authored-by: Gusted <[email protected]>
1 parent e4b1ea6 commit 87f7097

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

modules/markup/html.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -852,7 +852,7 @@ func fullIssuePatternProcessor(ctx *RenderContext, node *html.Node) {
852852
}
853853

854854
func issueIndexPatternProcessor(ctx *RenderContext, node *html.Node) {
855-
if ctx.Metas == nil {
855+
if ctx.Metas == nil || ctx.Metas["mode"] == "document" {
856856
return
857857
}
858858
var (

modules/markup/html_internal_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,30 @@ func TestRender_IssueIndexPattern5(t *testing.T) {
262262
})
263263
}
264264

265+
func TestRender_IssueIndexPattern_Document(t *testing.T) {
266+
setting.AppURL = TestAppURL
267+
metas := map[string]string{
268+
"format": "https://someurl.com/{user}/{repo}/{index}",
269+
"user": "someUser",
270+
"repo": "someRepo",
271+
"style": IssueNameStyleNumeric,
272+
"mode": "document",
273+
}
274+
275+
testRenderIssueIndexPattern(t, "#1", "#1", &RenderContext{
276+
Ctx: git.DefaultContext,
277+
Metas: metas,
278+
})
279+
testRenderIssueIndexPattern(t, "#1312", "#1312", &RenderContext{
280+
Ctx: git.DefaultContext,
281+
Metas: metas,
282+
})
283+
testRenderIssueIndexPattern(t, "!1", "!1", &RenderContext{
284+
Ctx: git.DefaultContext,
285+
Metas: metas,
286+
})
287+
}
288+
265289
func testRenderIssueIndexPattern(t *testing.T, input, expected string, ctx *RenderContext) {
266290
if ctx.URLPrefix == "" {
267291
ctx.URLPrefix = TestAppURL

modules/markup/html_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,42 @@ func Test_ParseClusterFuzz(t *testing.T) {
529529
assert.NotContains(t, res.String(), "<html")
530530
}
531531

532+
func TestPostProcess_RenderDocument(t *testing.T) {
533+
setting.AppURL = TestAppURL
534+
535+
localMetas := map[string]string{
536+
"user": "go-gitea",
537+
"repo": "gitea",
538+
"mode": "document",
539+
}
540+
541+
test := func(input, expected string) {
542+
var res strings.Builder
543+
err := PostProcess(&RenderContext{
544+
Ctx: git.DefaultContext,
545+
URLPrefix: "https://example.com",
546+
Metas: localMetas,
547+
}, strings.NewReader(input), &res)
548+
assert.NoError(t, err)
549+
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(res.String()))
550+
}
551+
552+
// Issue index shouldn't be post processing in an document.
553+
test(
554+
"#1",
555+
"#1")
556+
557+
// Test that other post processing still works.
558+
test(
559+
":gitea:",
560+
`<span class="emoji" aria-label="gitea"><img alt=":gitea:" src="`+setting.StaticURLPrefix+`/assets/img/emoji/gitea.png"/></span>`)
561+
test(
562+
"Some text with 😄 in the middle",
563+
`Some text with <span class="emoji" aria-label="grinning face with smiling eyes">😄</span> in the middle`)
564+
test("http://localhost:3000/person/repo/issues/4#issuecomment-1234",
565+
`<a href="http://localhost:3000/person/repo/issues/4#issuecomment-1234" class="ref-issue">person/repo#4 (comment)</a>`)
566+
}
567+
532568
func TestIssue16020(t *testing.T) {
533569
setting.AppURL = TestAppURL
534570

0 commit comments

Comments
 (0)