From 5b2069daf74898f0a0a78707a9ff66ba7938f5de Mon Sep 17 00:00:00 2001 From: Xavier Coulon Date: Mon, 16 May 2022 19:28:29 +0200 Subject: [PATCH] fix(renderer): image alt without separators replace underscores and hyphens with spaces Fixes #1019 Signed-off-by: Xavier Coulon --- .../sgml/html5/delimited_block_open_test.go | 2 +- pkg/renderer/sgml/html5/image_test.go | 16 +++++++++++++--- pkg/renderer/sgml/html5/table_test.go | 4 ++-- pkg/renderer/sgml/image.go | 6 +++++- 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/pkg/renderer/sgml/html5/delimited_block_open_test.go b/pkg/renderer/sgml/html5/delimited_block_open_test.go index 40200e4d..12743338 100644 --- a/pkg/renderer/sgml/html5/delimited_block_open_test.go +++ b/pkg/renderer/sgml/html5/delimited_block_open_test.go @@ -63,7 +63,7 @@ image::another-image.png[]
-another-image +another image
Figure 2. Another title
diff --git a/pkg/renderer/sgml/html5/image_test.go b/pkg/renderer/sgml/html5/image_test.go index 889c3eb5..b9b8b65e 100644 --- a/pkg/renderer/sgml/html5/image_test.go +++ b/pkg/renderer/sgml/html5/image_test.go @@ -11,12 +11,22 @@ var _ = Describe("images", func() { Context("block images", func() { - It("alone", func() { + It("alone with underscores in filename", func() { + source := "image::an_image_here.png[]" + expected := `
+
+an image here +
+
+` + Expect(RenderHTML(source)).To(MatchHTML(expected)) + }) - source := "image::foo.png[]" + It("alone with dashes in filename", func() { + source := "image::an-image-here.png[]" expected := `
-foo +an image here
` diff --git a/pkg/renderer/sgml/html5/table_test.go b/pkg/renderer/sgml/html5/table_test.go index 04840055..fc3d045c 100644 --- a/pkg/renderer/sgml/html5/table_test.go +++ b/pkg/renderer/sgml/html5/table_test.go @@ -645,7 +645,7 @@ image::another-image.png[]
-another-image +another image
@@ -682,7 +682,7 @@ image::another-image.png[]
-another-image +another image
Figure 2. Another title
diff --git a/pkg/renderer/sgml/image.go b/pkg/renderer/sgml/image.go index 829773c6..abe93709 100644 --- a/pkg/renderer/sgml/image.go +++ b/pkg/renderer/sgml/image.go @@ -164,5 +164,9 @@ func (r *sgmlRenderer) renderImageAlt(attrs types.Attributes, path string) (stri return "", errors.Wrap(err, "unable to render image") } // return base path without its extension - return strings.TrimSuffix(filepath.Base(u.Path), filepath.Ext(u.Path)), nil + result := strings.TrimSuffix(filepath.Base(u.Path), filepath.Ext(u.Path)) + // replace separators + result = strings.ReplaceAll(result, "-", " ") + result = strings.ReplaceAll(result, "_", " ") + return result, nil }