From c92789100c1e57176921913af1969201c0ca385f Mon Sep 17 00:00:00 2001 From: Robert Wolff Date: Mon, 13 Jan 2025 11:51:00 +0100 Subject: [PATCH] Remove automatic alt text in markdown image rendering --- modules/markup/html.go | 6 --- modules/markup/html_test.go | 26 +++++----- modules/markup/markdown/markdown_test.go | 60 ++++++++++++------------ modules/templates/util_render_test.go | 4 +- 4 files changed, 45 insertions(+), 51 deletions(-) diff --git a/modules/markup/html.go b/modules/markup/html.go index 3355c021ce..ca9857d2bf 100644 --- a/modules/markup/html.go +++ b/modules/markup/html.go @@ -759,9 +759,6 @@ func shortLinkProcessor(ctx *RenderContext, node *html.Node) { title = path.Base(name) } alt := props["alt"] - if alt == "" { - alt = name - } // make the childNode an image - if we can, we also place the alt childNode.Type = html.ElementNode @@ -772,9 +769,6 @@ func shortLinkProcessor(ctx *RenderContext, node *html.Node) { {Key: "title", Val: title}, {Key: "alt", Val: alt}, } - if alt == "" { - childNode.Attr = childNode.Attr[:2] - } } else { if !absoluteLink { if ctx.IsWiki { diff --git a/modules/markup/html_test.go b/modules/markup/html_test.go index 05ba321548..6c00c5e7c8 100644 --- a/modules/markup/html_test.go +++ b/modules/markup/html_test.go @@ -416,7 +416,7 @@ func TestRender_ShortLinks(t *testing.T) { otherImgurlWiki := util.URLJoin(markup.TestRepoURL, "wiki", "raw", "Link+Other.jpg") encodedImgurlWiki := util.URLJoin(markup.TestRepoURL, "wiki", "raw", "Link+%23.jpg") notencodedImgurlWiki := util.URLJoin(markup.TestRepoURL, "wiki", "raw", "some", "path", "Link+#.jpg") - favicon := "http://google.com/favicon.ico" + favicon := "https://forgejo.org/favicon.ico" test( "[[Link]]", @@ -424,28 +424,28 @@ func TestRender_ShortLinks(t *testing.T) { `

Link

`) test( "[[Link.jpg]]", - `

Link.jpg

`, - `

Link.jpg

`) + `

`, + `

`) test( "[["+favicon+"]]", - `

`+favicon+`

`, - `

`+favicon+`

`) + `

`, + `

`) test( "[[Name|Link]]", `

Name

`, `

Name

`) test( "[[Name|Link.jpg]]", - `

Name

`, - `

Name

`) + `

`, + `

`) test( "[[Name|Link.jpg|alt=AltName]]", `

AltName

`, `

AltName

`) test( "[[Name|Link.jpg|title=Title]]", - `

Title

`, - `

Title

`) + `

`, + `

`) test( "[[Name|Link.jpg|alt=AltName|title=Title]]", `

AltName

`, @@ -472,16 +472,16 @@ func TestRender_ShortLinks(t *testing.T) { `

Link Other Link Link?

`) test( "[[Link #.jpg]]", - `

Link #.jpg

`, - `

Link #.jpg

`) + `

`, + `

`) test( "[[Name|Link #.jpg|alt=\"AltName\"|title='Title']]", `

AltName

`, `

AltName

`) test( "[[some/path/Link #.jpg]]", - `

some/path/Link #.jpg

`, - `

some/path/Link #.jpg

`) + `

`, + `

`) test( "

[[foobar]]

", `

[[foobar]]

`, diff --git a/modules/markup/markdown/markdown_test.go b/modules/markup/markdown/markdown_test.go index 4c9ab42806..e64ab6a6f4 100644 --- a/modules/markup/markdown/markdown_test.go +++ b/modules/markup/markdown/markdown_test.go @@ -108,7 +108,7 @@ func TestRender_Images(t *testing.T) { test( "[["+title+"|"+url+"]]", - `

`+title+`

`) + `

`) test( "[!["+title+"]("+url+")]("+href+")", `

`+title+`

`) @@ -119,7 +119,7 @@ func TestRender_Images(t *testing.T) { test( "[["+title+"|"+url+"]]", - `

`+title+`

`) + `

`) test( "[!["+title+"]("+url+")]("+href+")", `

`+title+`

`) @@ -149,13 +149,13 @@ func testAnswers(baseURLContent, baseURLImages string) []string { - + - + @@ -164,9 +164,9 @@ func testAnswers(baseURLContent, baseURLImages string) []string { `

Excelsior JET allows you to create native executables for Windows, Linux and Mac OS X.

  1. Package your libGDX application
    -images/1.png
  2. +
  3. Perform a test run by hitting the Run! button.
    -images/2.png
  4. +

More tests

(from https://www.markdownguide.org/extended-syntax/)

@@ -849,8 +849,8 @@ mail@domain.com local image
local image
remote image
-local image
-remote link
+
+
https://example.com/user/repo/compare/88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb#hash
com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb pare
https://example.com/user/repo/commit/88fc37a3c0a4dda553bdcfc80c178a58247f42fb
@@ -876,8 +876,8 @@ space

local image
local image
remote image
-local image
-remote link
+
+
https://example.com/user/repo/compare/88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb#hash
com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb pare
https://example.com/user/repo/commit/88fc37a3c0a4dda553bdcfc80c178a58247f42fb
@@ -905,8 +905,8 @@ space

local image
local image
remote image
-local image
-remote link
+
+
https://example.com/user/repo/compare/88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb#hash
com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb pare
https://example.com/user/repo/commit/88fc37a3c0a4dda553bdcfc80c178a58247f42fb
@@ -934,8 +934,8 @@ space

local image
local image
remote image
-local image
-remote link
+
+
https://example.com/user/repo/compare/88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb#hash
com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb pare
https://example.com/user/repo/commit/88fc37a3c0a4dda553bdcfc80c178a58247f42fb
@@ -963,8 +963,8 @@ space

local image
local image
remote image
-local image
-remote link
+
+
https://example.com/user/repo/compare/88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb#hash
com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb pare
https://example.com/user/repo/commit/88fc37a3c0a4dda553bdcfc80c178a58247f42fb
@@ -992,8 +992,8 @@ space

local image
local image
remote image
-local image
-remote link
+
+
https://example.com/user/repo/compare/88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb#hash
com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb pare
https://example.com/user/repo/commit/88fc37a3c0a4dda553bdcfc80c178a58247f42fb
@@ -1022,8 +1022,8 @@ space

local image
local image
remote image
-local image
-remote link
+
+
https://example.com/user/repo/compare/88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb#hash
com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb pare
https://example.com/user/repo/commit/88fc37a3c0a4dda553bdcfc80c178a58247f42fb
@@ -1052,8 +1052,8 @@ space

local image
local image
remote image
-local image
-remote link
+
+
https://example.com/user/repo/compare/88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb#hash
com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb pare
https://example.com/user/repo/commit/88fc37a3c0a4dda553bdcfc80c178a58247f42fb
@@ -1082,8 +1082,8 @@ space

local image
local image
remote image
-local image
-remote link
+
+
https://example.com/user/repo/compare/88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb#hash
com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb pare
https://example.com/user/repo/commit/88fc37a3c0a4dda553bdcfc80c178a58247f42fb
@@ -1112,8 +1112,8 @@ space

local image
local image
remote image
-local image
-remote link
+
+
https://example.com/user/repo/compare/88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb#hash
com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb pare
https://example.com/user/repo/commit/88fc37a3c0a4dda553bdcfc80c178a58247f42fb
@@ -1143,8 +1143,8 @@ space

local image
local image
remote image
-local image
-remote link
+
+
https://example.com/user/repo/compare/88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb#hash
com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb pare
https://example.com/user/repo/commit/88fc37a3c0a4dda553bdcfc80c178a58247f42fb
@@ -1174,8 +1174,8 @@ space

local image
local image
remote image
-local image
-remote link
+
+
https://example.com/user/repo/compare/88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb#hash
com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb pare
https://example.com/user/repo/commit/88fc37a3c0a4dda553bdcfc80c178a58247f42fb
diff --git a/modules/templates/util_render_test.go b/modules/templates/util_render_test.go index 3479d94039..028cd2ba76 100644 --- a/modules/templates/util_render_test.go +++ b/modules/templates/util_render_test.go @@ -194,8 +194,8 @@ func TestRenderMarkdownToHtml(t *testing.T) { remote linklocal imageremote image -local image -remote link + +88fc37a3c0...12fc37a3c0 (hash) com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb pare 88fc37a3c0
images/icon-install.png Installation
images/icon-usage.png Usage