From 6cee3bfa9611a691584824ea23a75cc1ebcbb46b Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Fri, 4 Apr 2025 23:45:23 +0800 Subject: [PATCH] Refactor markup render to fix various path problems (#34114) * Fix #33972 * Use consistent path resolving for links and medias. * No need to make the markup renders to resolve the paths, instead, the paths are all correctly resolved in the "post process" step. * Fix #33274 * Since 1.23, all paths starting with "/" are relative to current render context (for example: the current repo branch) * Introduce `/:root/path-relative-to-root`, then the path will be rendered as relative to "ROOT_URL" --- models/renderhelper/repo_comment.go | 12 +-- models/renderhelper/repo_file.go | 14 ++-- models/renderhelper/repo_file_test.go | 10 +-- models/renderhelper/repo_wiki.go | 18 ++--- models/renderhelper/repo_wiki_test.go | 6 +- models/renderhelper/simple_document.go | 10 ++- models/renderhelper/simple_document_test.go | 2 +- modules/markup/external/external.go | 22 +++--- modules/markup/html.go | 73 +++++++---------- modules/markup/html_commit.go | 9 +-- modules/markup/html_internal_test.go | 10 +-- modules/markup/html_issue.go | 4 +- modules/markup/html_issue_test.go | 4 +- modules/markup/html_link.go | 2 - modules/markup/html_mention.go | 4 +- modules/markup/html_node.go | 87 +++++++++++++++------ modules/markup/html_test.go | 19 ++--- modules/markup/markdown/goldmark.go | 4 - modules/markup/markdown/markdown_test.go | 21 ++++- modules/markup/markdown/transform_image.go | 59 -------------- modules/markup/markdown/transform_link.go | 27 ------- modules/markup/orgmode/orgmode.go | 26 ++---- modules/markup/orgmode/orgmode_test.go | 25 +++--- modules/markup/render.go | 10 ++- modules/markup/render_helper.go | 15 ++-- modules/markup/render_link.go | 18 ++++- modules/templates/util_render_test.go | 4 +- routers/api/v1/misc/markup_test.go | 10 +-- 28 files changed, 239 insertions(+), 286 deletions(-) delete mode 100644 modules/markup/markdown/transform_image.go delete mode 100644 modules/markup/markdown/transform_link.go diff --git a/models/renderhelper/repo_comment.go b/models/renderhelper/repo_comment.go index 6bd5e91ad1..eab85bf6d4 100644 --- a/models/renderhelper/repo_comment.go +++ b/models/renderhelper/repo_comment.go @@ -28,14 +28,14 @@ func (r *RepoComment) IsCommitIDExisting(commitID string) bool { return r.commitChecker.IsCommitIDExisting(commitID) } -func (r *RepoComment) ResolveLink(link string, likeType markup.LinkType) (finalLink string) { - switch likeType { - case markup.LinkTypeApp: - finalLink = r.ctx.ResolveLinkApp(link) +func (r *RepoComment) ResolveLink(link, preferLinkType string) string { + linkType, link := markup.ParseRenderedLink(link, preferLinkType) + switch linkType { + case markup.LinkTypeRoot: + return r.ctx.ResolveLinkRoot(link) default: - finalLink = r.ctx.ResolveLinkRelative(r.repoLink, r.opts.CurrentRefPath, link) + return r.ctx.ResolveLinkRelative(r.repoLink, r.opts.CurrentRefPath, link) } - return finalLink } var _ markup.RenderHelper = (*RepoComment)(nil) diff --git a/models/renderhelper/repo_file.go b/models/renderhelper/repo_file.go index 794828c617..5bf754bf20 100644 --- a/models/renderhelper/repo_file.go +++ b/models/renderhelper/repo_file.go @@ -29,17 +29,17 @@ func (r *RepoFile) IsCommitIDExisting(commitID string) bool { return r.commitChecker.IsCommitIDExisting(commitID) } -func (r *RepoFile) ResolveLink(link string, likeType markup.LinkType) string { - finalLink := link - switch likeType { - case markup.LinkTypeApp: - finalLink = r.ctx.ResolveLinkApp(link) - case markup.LinkTypeDefault: - finalLink = r.ctx.ResolveLinkRelative(path.Join(r.repoLink, "src", r.opts.CurrentRefPath), r.opts.CurrentTreePath, link) +func (r *RepoFile) ResolveLink(link, preferLinkType string) (finalLink string) { + linkType, link := markup.ParseRenderedLink(link, preferLinkType) + switch linkType { + case markup.LinkTypeRoot: + finalLink = r.ctx.ResolveLinkRoot(link) case markup.LinkTypeRaw: finalLink = r.ctx.ResolveLinkRelative(path.Join(r.repoLink, "raw", r.opts.CurrentRefPath), r.opts.CurrentTreePath, link) case markup.LinkTypeMedia: finalLink = r.ctx.ResolveLinkRelative(path.Join(r.repoLink, "media", r.opts.CurrentRefPath), r.opts.CurrentTreePath, link) + default: + finalLink = r.ctx.ResolveLinkRelative(path.Join(r.repoLink, "src", r.opts.CurrentRefPath), r.opts.CurrentTreePath, link) } return finalLink } diff --git a/models/renderhelper/repo_file_test.go b/models/renderhelper/repo_file_test.go index 29cb45f6f7..3b48efba3a 100644 --- a/models/renderhelper/repo_file_test.go +++ b/models/renderhelper/repo_file_test.go @@ -48,8 +48,8 @@ func TestRepoFile(t *testing.T) { assert.Equal(t, `

/test ./test -/image -./image

+/image +./image

`, rendered) }) @@ -62,7 +62,7 @@ func TestRepoFile(t *testing.T) { `) assert.NoError(t, err) assert.Equal(t, `

/test -/image

+/image

`, rendered) }) @@ -77,7 +77,7 @@ func TestRepoFile(t *testing.T) {