forked from forgejo/forgejo
[BUG] Restrict when to make link absolute in markdown
- In markdown, links are proccessed to be made absolute against the relevant base in that context. Such that `./src` will be transformed into `http://example.com/owner/repo/src/branch/main/src`. - Don't try to make the link absolute if the link has a schema that's defined in `[markdown].CUSTOM_URL_SCHEMES`, because they can't be made absolute and doing so could lead to problems (see test case, double slash was transformed to single slash). - Adds unit test. - Resolves https://codeberg.org/Codeberg/Community/issues/1489
This commit is contained in:
parent
cfd6420a0e
commit
65b9a959b8
2 changed files with 38 additions and 4 deletions
|
@ -7,6 +7,7 @@ import (
|
|||
"bytes"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/gitea/modules/container"
|
||||
|
@ -129,11 +130,17 @@ func (g *ASTTransformer) Transform(node *ast.Document, reader text.Reader, pc pa
|
|||
case *ast.Link:
|
||||
// Links need their href to munged to be a real value
|
||||
link := v.Destination
|
||||
if len(link) > 0 && !markup.IsLink(link) &&
|
||||
link[0] != '#' && !bytes.HasPrefix(link, byteMailto) {
|
||||
// special case: this is not a link, a hash link or a mailto:, so it's a
|
||||
// relative URL
|
||||
|
||||
// Do not process the link if it's not a link, starts with an hashtag
|
||||
// (indicating it's an anchor link), starts with `mailto:` or any of the
|
||||
// custom markdown URLs.
|
||||
processLink := len(link) > 0 && !markup.IsLink(link) &&
|
||||
link[0] != '#' && !bytes.HasPrefix(link, byteMailto) &&
|
||||
!slices.ContainsFunc(setting.Markdown.CustomURLSchemes, func(s string) bool {
|
||||
return bytes.HasPrefix(link, []byte(s+":"))
|
||||
})
|
||||
|
||||
if processLink {
|
||||
var base string
|
||||
if ctx.IsWiki {
|
||||
base = ctx.Links.WikiLink()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue