diff --git a/modules/markup/html.go b/modules/markup/html.go index b3a241af7a..c312d3cba0 100644 --- a/modules/markup/html.go +++ b/modules/markup/html.go @@ -748,10 +748,10 @@ func shortLinkProcessor(ctx *RenderContext, node *html.Node) { if image { link = strings.ReplaceAll(link, " ", "+") } else { - link = strings.ReplaceAll(link, " ", "-") + link = strings.ReplaceAll(link, " ", "-") // FIXME: it should support dashes in the link, eg: "the-dash-support.-" } if !strings.Contains(link, "/") { - link = url.PathEscape(link) + link = url.PathEscape(link) // FIXME: it doesn't seem right and it might cause double-escaping } } if image { @@ -783,28 +783,7 @@ func shortLinkProcessor(ctx *RenderContext, node *html.Node) { childNode.Attr = childNode.Attr[:2] } } else { - if !absoluteLink { - var base string - if ctx.IsWiki { - switch ext { - case "": - // no file extension, create a regular wiki link - base = ctx.Links.WikiLink() - default: - // we have a file extension: - // return a regular wiki link if it's a renderable file (extension), - // raw link otherwise - if Type(link) != "" { - base = ctx.Links.WikiLink() - } else { - base = ctx.Links.WikiRawLink() - } - } - } else { - base = ctx.Links.SrcLink() - } - link = util.URLJoin(base, link) - } + link, _ = ResolveLink(ctx, link, "") childNode.Type = html.TextNode childNode.Data = name } diff --git a/modules/markup/html_link.go b/modules/markup/html_link.go new file mode 100644 index 0000000000..a41b87e9fa --- /dev/null +++ b/modules/markup/html_link.go @@ -0,0 +1,35 @@ +// Copyright 2024 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package markup + +import ( + "path" + + "code.gitea.io/gitea/modules/util" +) + +func ResolveLink(ctx *RenderContext, link, userContentAnchorPrefix string) (result string, resolved bool) { + isAnchorFragment := link != "" && link[0] == '#' + if !isAnchorFragment && !IsFullURLString(link) { + linkBase := ctx.Links.Base + if ctx.IsWiki { + if ext := path.Ext(link); ext == "" || ext == ".-" { + linkBase = ctx.Links.WikiLink() // the link is for a wiki page + } else if DetectMarkupTypeByFileName(link) != "" { + linkBase = ctx.Links.WikiLink() // the link is renderable as a wiki page + } else { + linkBase = ctx.Links.WikiRawLink() // otherwise, use a raw link instead to view&download medias + } + } else if ctx.Links.BranchPath != "" || ctx.Links.TreePath != "" { + // if there is no BranchPath, then the link will be something like "/owner/repo/src/{the-file-path}" + // and then this link will be handled by the "legacy-ref" code and be redirected to the default branch like "/owner/repo/src/branch/main/{the-file-path}" + linkBase = ctx.Links.SrcLink() + } + link, resolved = util.URLJoin(linkBase, link), true + } + if isAnchorFragment && userContentAnchorPrefix != "" { + link, resolved = userContentAnchorPrefix+link[1:], true + } + return link, resolved +} diff --git a/modules/markup/html_test.go b/modules/markup/html_test.go index aeb619e12d..399488912e 100644 --- a/modules/markup/html_test.go +++ b/modules/markup/html_test.go @@ -444,6 +444,10 @@ func TestRender_ShortLinks(t *testing.T) { "[[Link]]", `
`, ``) + test( + "[[Link.-]]", + ``, + ``) test( "[[Link.jpg]]", ``, diff --git a/modules/markup/markdown/markdown_test.go b/modules/markup/markdown/markdown_test.go index 8c41ec12e3..9a8c39df0a 100644 --- a/modules/markup/markdown/markdown_test.go +++ b/modules/markup/markdown/markdown_test.go @@ -635,7 +635,7 @@ mail@domain.com https://example.com/file.bin


space @mention-user
/just/a/path.bin
https://example.com/file.bin
-local link
+local link
remote link
local link
remote link
@@ -984,7 +984,7 @@ space
@@ -190,7 +190,7 @@ com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb mit
#123
space
`
- assert.EqualValues(t, expected, RenderMarkdownToHtml(context.Background(), testInput()))
+ assert.Equal(t, expected, string(RenderMarkdownToHtml(context.Background(), testInput())))
}
func TestRenderLabels(t *testing.T) {
diff --git a/routers/web/repo/render.go b/routers/web/repo/render.go
index e64db03e20..6aba9e0ac1 100644
--- a/routers/web/repo/render.go
+++ b/routers/web/repo/render.go
@@ -47,7 +47,7 @@ func RenderFile(ctx *context.Context) {
rd := charset.ToUTF8WithFallbackReader(io.MultiReader(bytes.NewReader(buf), dataRc), charset.ConvertOpts{})
ctx.Resp.Header().Add("Content-Security-Policy", "frame-src 'self'; sandbox allow-scripts")
- if markupType := markup.Type(blob.Name()); markupType == "" {
+ if markupType := markup.DetectMarkupTypeByFileName(blob.Name()); markupType == "" {
if isTextFile {
_, _ = io.Copy(ctx.Resp, rd)
} else {
diff --git a/routers/web/repo/view.go b/routers/web/repo/view.go
index 0aa3fe1efd..2c478abacf 100644
--- a/routers/web/repo/view.go
+++ b/routers/web/repo/view.go
@@ -307,7 +307,7 @@ func renderReadmeFile(ctx *context.Context, subfolder string, readmeFile *git.Tr
rd := charset.ToUTF8WithFallbackReader(io.MultiReader(bytes.NewReader(buf), dataRc), charset.ConvertOpts{})
- if markupType := markup.Type(readmeFile.Name()); markupType != "" {
+ if markupType := markup.DetectMarkupTypeByFileName(readmeFile.Name()); markupType != "" {
ctx.Data["IsMarkup"] = true
ctx.Data["MarkupType"] = markupType
@@ -499,7 +499,7 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry) {
readmeExist := util.IsReadmeFileName(blob.Name())
ctx.Data["ReadmeExist"] = readmeExist
- markupType := markup.Type(blob.Name())
+ markupType := markup.DetectMarkupTypeByFileName(blob.Name())
// If the markup is detected by custom markup renderer it should not be reset later on
// to not pass it down to the render context.
detected := false
@@ -607,7 +607,7 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry) {
// TODO: this logic duplicates with "isRepresentableAsText=true", it is not the same as "LFSFileGet" in "lfs.go"
// It is used by "external renders", markupRender will execute external programs to get rendered content.
- if markupType := markup.Type(blob.Name()); markupType != "" {
+ if markupType := markup.DetectMarkupTypeByFileName(blob.Name()); markupType != "" {
rd := io.MultiReader(bytes.NewReader(buf), dataRc)
ctx.Data["IsMarkup"] = true
ctx.Data["MarkupType"] = markupType
diff --git a/routers/web/repo/wiki.go b/routers/web/repo/wiki.go
index df15f61b17..ff6397cd2a 100644
--- a/routers/web/repo/wiki.go
+++ b/routers/web/repo/wiki.go
@@ -532,7 +532,7 @@ func Wiki(ctx *context.Context) {
}
wikiPath := entry.Name()
- if markup.Type(wikiPath) != markdown.MarkupName {
+ if markup.DetectMarkupTypeByFileName(wikiPath) != markdown.MarkupName {
ext := strings.ToUpper(filepath.Ext(wikiPath))
ctx.Data["FormatWarning"] = fmt.Sprintf("%s rendering is not supported at the moment. Rendered as Markdown.", ext)
}