diff --git a/modules/markup/markdown/goldmark.go b/modules/markup/markdown/goldmark.go index 67d81488fd..8a3da3b08f 100644 --- a/modules/markup/markdown/goldmark.go +++ b/modules/markup/markdown/goldmark.go @@ -9,6 +9,7 @@ import ( "strings" "forgejo.org/modules/markup" + "forgejo.org/modules/markup/common" markdownutil "forgejo.org/modules/markup/markdown/util" "forgejo.org/modules/setting" @@ -74,6 +75,18 @@ func (g *ASTTransformer) Transform(node *ast.Document, reader text.Reader, pc pa } case *ast.CodeSpan: g.transformCodeSpan(ctx, v, reader) + case *common.Footnote: + if scope, found := ctx.Metas["scope"]; found { + v.Name = fmt.Appendf(v.Name, "-%s", scope) + } + case *common.FootnoteLink: + if scope, found := ctx.Metas["scope"]; found { + v.Name = fmt.Appendf(v.Name, "-%s", scope) + } + case *common.FootnoteBackLink: + if scope, found := ctx.Metas["scope"]; found { + v.Name = fmt.Appendf(v.Name, "-%s", scope) + } } return ast.WalkContinue, nil }) diff --git a/modules/markup/markdown/markdown_test.go b/modules/markup/markdown/markdown_test.go index c854861031..f9306b99a2 100644 --- a/modules/markup/markdown/markdown_test.go +++ b/modules/markup/markdown/markdown_test.go @@ -802,6 +802,49 @@ Citation needed[^0].`, } } +func TestFootnoteWithScope(t *testing.T) { + testcases := []struct { + testcase string + expected string + }{ + { + `Citation needed[^0]. +[^0]: Source`, + `

Citation needed1.

+
+
+
    +
  1. +

    Source ↩︎

    +
  2. +
+
+`, + }, { + `[^0]: Source + +Citation needed[^0].`, + `

Citation needed1.

+
+
+
    +
  1. +

    Source ↩︎

    +
  2. +
+
+`, + }, + } + + for _, test := range testcases { + metas := map[string]string{"scope": "comment-999"} + res, err := markdown.RenderString(&markup.RenderContext{Ctx: git.DefaultContext, Metas: metas}, test.testcase) + require.NoError(t, err, "Unexpected error in testcase: %q", test.testcase) + assert.Equal(t, test.expected, string(res), "Unexpected result in testcase %q", test.testcase) + } +} + func TestTaskList(t *testing.T) { testcases := []struct { testcase string diff --git a/routers/web/repo/issue.go b/routers/web/repo/issue.go index cab508e6c6..715f5dca8f 100644 --- a/routers/web/repo/issue.go +++ b/routers/web/repo/issue.go @@ -1590,6 +1590,7 @@ func ViewIssue(ctx *context.Context) { ok bool marked = make(map[int64]issues_model.RoleDescriptor) comment *issues_model.Comment + commentIdx int participants = make([]*user_model.User, 1, 10) latestCloseCommentID int64 ) @@ -1645,15 +1646,17 @@ func ViewIssue(ctx *context.Context) { return } - for _, comment = range issue.Comments { + for commentIdx, comment = range issue.Comments { comment.Issue = issue + metas := ctx.Repo.Repository.ComposeMetas(ctx) + metas["scope"] = fmt.Sprintf("comment-%d", commentIdx) if comment.Type == issues_model.CommentTypeComment || comment.Type == issues_model.CommentTypeReview { comment.RenderedContent, err = markdown.RenderString(&markup.RenderContext{ Links: markup.Links{ Base: ctx.Repo.RepoLink, }, - Metas: ctx.Repo.Repository.ComposeMetas(ctx), + Metas: metas, GitRepo: ctx.Repo.GitRepo, Ctx: ctx, }, comment.Content) @@ -1730,7 +1733,7 @@ func ViewIssue(ctx *context.Context) { Links: markup.Links{ Base: ctx.Repo.RepoLink, }, - Metas: ctx.Repo.Repository.ComposeMetas(ctx), + Metas: metas, GitRepo: ctx.Repo.GitRepo, Ctx: ctx, }, comment.Content)