loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request '[BUG] Restrict when to make link absolute in markdown' (#2403) from gusted/forgejo-custom-url into forgejo

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/2403
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Reviewed-by: Otto <otto@codeberg.org>

Gusted f128b6ef fc10cfea

+38 -4
+11 -4
modules/markup/markdown/goldmark.go
··· 7 7 "bytes" 8 8 "fmt" 9 9 "regexp" 10 + "slices" 10 11 "strings" 11 12 12 13 "code.gitea.io/gitea/modules/container" ··· 129 130 case *ast.Link: 130 131 // Links need their href to munged to be a real value 131 132 link := v.Destination 132 - if len(link) > 0 && !markup.IsLink(link) && 133 - link[0] != '#' && !bytes.HasPrefix(link, byteMailto) { 134 - // special case: this is not a link, a hash link or a mailto:, so it's a 135 - // relative URL 136 133 134 + // Do not process the link if it's not a link, starts with an hashtag 135 + // (indicating it's an anchor link), starts with `mailto:` or any of the 136 + // custom markdown URLs. 137 + processLink := len(link) > 0 && !markup.IsLink(link) && 138 + link[0] != '#' && !bytes.HasPrefix(link, byteMailto) && 139 + !slices.ContainsFunc(setting.Markdown.CustomURLSchemes, func(s string) bool { 140 + return bytes.HasPrefix(link, []byte(s+":")) 141 + }) 142 + 143 + if processLink { 137 144 var base string 138 145 if ctx.IsWiki { 139 146 base = ctx.Links.WikiLink()
+27
modules/markup/markdown/markdown_test.go
··· 15 15 "code.gitea.io/gitea/modules/markup" 16 16 "code.gitea.io/gitea/modules/markup/markdown" 17 17 "code.gitea.io/gitea/modules/setting" 18 + "code.gitea.io/gitea/modules/test" 18 19 "code.gitea.io/gitea/modules/util" 19 20 20 21 "github.com/stretchr/testify/assert" ··· 1170 1171 assert.Equal(t, c.Expected, result, "Unexpected result in testcase %v", i) 1171 1172 } 1172 1173 } 1174 + 1175 + func TestCustomMarkdownURL(t *testing.T) { 1176 + defer test.MockVariableValue(&setting.Markdown.CustomURLSchemes, []string{"abp"})() 1177 + 1178 + setting.AppURL = AppURL 1179 + setting.AppSubURL = AppSubURL 1180 + 1181 + test := func(input, expected string) { 1182 + buffer, err := markdown.RenderString(&markup.RenderContext{ 1183 + Ctx: git.DefaultContext, 1184 + Links: markup.Links{ 1185 + Base: setting.AppSubURL, 1186 + BranchPath: "branch/main", 1187 + }, 1188 + }, input) 1189 + assert.NoError(t, err) 1190 + assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer)) 1191 + } 1192 + 1193 + test("[test](abp:subscribe?location=https://codeberg.org/filters.txt&amp;title=joy)", 1194 + `<p><a href="abp:subscribe?location=https://codeberg.org/filters.txt&amp;title=joy" rel="nofollow">test</a></p>`) 1195 + 1196 + // Ensure that the schema itself without `:` is still made absolute. 1197 + test("[test](abp)", 1198 + `<p><a href="http://localhost:3000/gogits/gogs/src/branch/main/abp" rel="nofollow">test</a></p>`) 1199 + }