Skip to content

Fix footnote jump behavior on the issue page. #34621

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions modules/markup/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,8 @@ func visitNode(ctx *RenderContext, procs []processor, node *html.Node) *html.Nod
}

processNodeAttrID(node)
processFootnoteID(ctx, node)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we can merge these two functions, as they aren't used elsewhere.

processFootnoteBackHref(ctx, node)

if isEmojiNode(node) {
// TextNode emoji will be converted to `<span class="emoji">`, then the next iteration will visit the "span"
Expand Down
19 changes: 19 additions & 0 deletions modules/markup/html_issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func TestRender_IssueList(t *testing.T) {
rctx := markup.NewTestRenderContext(markup.TestAppURL, map[string]string{
"user": "test-user", "repo": "test-repo",
"markupAllowShortIssuePattern": "true",
"issue_comment_id": "12345",
})
out, err := markdown.RenderString(rctx, input)
require.NoError(t, err)
Expand Down Expand Up @@ -69,4 +70,22 @@ func TestRender_IssueList(t *testing.T) {
</ul>`,
)
})

t.Run("IssueFootnote", func(t *testing.T) {
test(
"foo[^1][^2]\n\n[^1]: bar\n[^2]: baz",
`<p>foo<sup id="fnref:user-content-1-12345"><a href="#fn:user-content-1-12345" rel="nofollow">1</a></sup><sup id="fnref:user-content-2-12345"><a href="#fn:user-content-2-12345" rel="nofollow">2</a></sup></p>
<div>
<hr/>
<ol>
<li id="fn:user-content-1-12345">
<p>bar <a href="#fnref:user-content-1-12345" rel="nofollow">↩︎</a></p>
</li>
<li id="fn:user-content-2-12345">
<p>baz <a href="#fnref:user-content-2-12345" rel="nofollow">↩︎</a></p>
</li>
</ol>
</div>`,
)
})
}
36 changes: 36 additions & 0 deletions modules/markup/html_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ func isAnchorIDUserContent(s string) bool {
return strings.HasPrefix(s, "user-content-") || strings.Contains(s, ":user-content-")
}

func isAnchorIDFootnote(s string) bool {
return strings.HasPrefix(s, "fnref:user-content-") || strings.HasPrefix(s, "fn:user-content-")
}

func isAnchorHrefFootnote(s string) bool {
return strings.HasPrefix(s, "#fnref:user-content-") || strings.HasPrefix(s, "#fn:user-content-")
}

func processNodeAttrID(node *html.Node) {
// Add user-content- to IDs and "#" links if they don't already have them,
// and convert the link href to a relative link to the host root
Expand All @@ -27,6 +35,34 @@ func processNodeAttrID(node *html.Node) {
}
}

func processFootnoteID(ctx *RenderContext, node *html.Node) {
for idx, attr := range node.Attr {
if attr.Key == "id" {
if !isAnchorIDFootnote(attr.Val) {
return
}
if issueNum, ok := ctx.RenderOptions.Metas["issue_comment_id"]; ok {
node.Attr[idx].Val = attr.Val + "-" + issueNum
}
return
}
}
}

func processFootnoteBackHref(ctx *RenderContext, node *html.Node) {
for idx, attr := range node.Attr {
if attr.Key == "href" {
if !isAnchorHrefFootnote(attr.Val) {
return
}
if issueNum, ok := ctx.RenderOptions.Metas["issue_comment_id"]; ok {
node.Attr[idx].Val = attr.Val + "-" + issueNum
}
return
}
}
}

func processNodeA(ctx *RenderContext, node *html.Node) {
for idx, attr := range node.Attr {
if attr.Key == "href" {
Expand Down
4 changes: 4 additions & 0 deletions routers/web/repo/issue_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"net/url"
"sort"
"strconv"

activities_model "code.gitea.io/gitea/models/activities"
"code.gitea.io/gitea/models/db"
Expand Down Expand Up @@ -625,6 +626,7 @@ func prepareIssueViewCommentsAndSidebarParticipants(ctx *context.Context, issue

if comment.Type == issues_model.CommentTypeComment || comment.Type == issues_model.CommentTypeReview {
rctx := renderhelper.NewRenderContextRepoComment(ctx, issue.Repo)
rctx.RenderOptions.Metas["issue_comment_id"] = strconv.FormatInt(comment.ID, 10)
comment.RenderedContent, err = markdown.RenderString(rctx, comment.Content)
if err != nil {
ctx.ServerError("RenderString", err)
Expand Down Expand Up @@ -982,6 +984,8 @@ func preparePullViewReviewAndMerge(ctx *context.Context, issue *issues_model.Iss
func prepareIssueViewContent(ctx *context.Context, issue *issues_model.Issue) {
var err error
rctx := renderhelper.NewRenderContextRepoComment(ctx, ctx.Repo.Repository)
// the first issue index set to 0
rctx.RenderOptions.Metas["issue_comment_id"] = "0"
issue.RenderedContent, err = markdown.RenderString(rctx, issue.Content)
if err != nil {
ctx.ServerError("RenderString", err)
Expand Down
3 changes: 2 additions & 1 deletion web_src/js/markup/anchors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ const hasPrefix = (str: string): boolean => str.startsWith('user-content-');

// scroll to anchor while respecting the `user-content` prefix that exists on the target
function scrollToAnchor(encodedId: string): void {
if (!encodedId) return;
// Ignore the footnote case, as the link is rendered by the backend.
if (!encodedId || encodedId.startsWith('fn:') || encodedId.startsWith('fnref:')) return;
const id = decodeURIComponent(encodedId);
const prefixedId = addPrefix(id);
let el = document.querySelector(`#${prefixedId}`);
Expand Down