Skip to content

Commit

Permalink
fix(renderer): image alt without separators (#1025)
Browse files Browse the repository at this point in the history
replace underscores and hyphens with spaces

Fixes #1019

Signed-off-by: Xavier Coulon <xcoulon@redhat.com>
  • Loading branch information
xcoulon authored May 16, 2022
1 parent ceef387 commit c680ac4
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pkg/renderer/sgml/html5/delimited_block_open_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ image::another-image.png[]
</div></div></td>
<td class="tableblock halign-center valign-top"><div class="content"><div id="another-id" class="imageblock">
<div class="content">
<img src="another-image.png" alt="another-image">
<img src="another-image.png" alt="another image">
</div>
<div class="title">Figure 2. Another title</div>
</div></div></td>
Expand Down
16 changes: 13 additions & 3 deletions pkg/renderer/sgml/html5/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 := `<div class="imageblock">
<div class="content">
<img src="an_image_here.png" alt="an image here">
</div>
</div>
`
Expect(RenderHTML(source)).To(MatchHTML(expected))
})

source := "image::foo.png[]"
It("alone with dashes in filename", func() {
source := "image::an-image-here.png[]"
expected := `<div class="imageblock">
<div class="content">
<img src="foo.png" alt="foo">
<img src="an-image-here.png" alt="an image here">
</div>
</div>
`
Expand Down
4 changes: 2 additions & 2 deletions pkg/renderer/sgml/html5/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ image::another-image.png[]
</div></div></td>
<td class="tableblock halign-center valign-top"><div class="content"><div class="imageblock">
<div class="content">
<img src="another-image.png" alt="another-image">
<img src="another-image.png" alt="another image">
</div>
</div></div></td>
</tr>
Expand Down Expand Up @@ -682,7 +682,7 @@ image::another-image.png[]
</div></div></td>
<td class="tableblock halign-center valign-top"><div class="content"><div id="another-id" class="imageblock">
<div class="content">
<img src="another-image.png" alt="another-image">
<img src="another-image.png" alt="another image">
</div>
<div class="title">Figure 2. Another title</div>
</div></div></td>
Expand Down
6 changes: 5 additions & 1 deletion pkg/renderer/sgml/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit c680ac4

Please sign in to comment.