Skip to content

Commit 8c008e9

Browse files
committed
fix the <img> conversion, it wouldn't convert an svg link
1 parent ca1c81d commit 8c008e9

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,24 @@ I use the `hudevto preview` command because I do some transformations and I need
187187
```text
188188
![My image](/you-should-write-comments/cover-you-should-write-comments.png)
189189
<------ basePostURL ------>
190-
(basePostURL includes the trailing /)
190+
(basePostURL includes the leading / and trailing /)
191191
```
192192
193+
Since you can also embed `<img>` tags in markdown, these are also converted. For example,
194+
195+
```markdown
196+
<img alt="Super example" src="dnat-google-vpc-how-comes-back.svg" width="80%"/>
197+
```
198+
199+
becomes:
200+
201+
```markdown
202+
<img alt="Super example" src="/you-should-write-comments/dnat-google-vpc-how-comes-back.svg" width="80%"/>
203+
```
204+
205+
Only the following image extensions are converted: png, PNG, jpeg, JPG, jpg,
206+
gif, GIF, svg, SVG.
207+
193208
- The GitHub-style anchor IDs are converted to Devto anchor IDs. This is because
194209
GitHub-style anchor IDs, which is what Hugo produces, are different from the
195210
ones produced by Devto. For example, take the following Markdown:

main.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,7 @@ func convertHugoToLiquid(in string) string {
869869
//
870870
// ![My image](/you-should-write-comments/cover-you-should-write-comments.png)
871871
// <------ basePostURL ------>
872-
// (note that basePostURL includes the trailing '/')
872+
// (basePostURL already includes the leading / and trailing /)
873873
//
874874
// Note: (?s) means multiline, (?U) means non-greedy.
875875
var mdImg = regexp.MustCompile(`(?sU)\!\[([^\]]*)\]\((\S*)\)`)
@@ -878,8 +878,10 @@ func addPostURLInImages(in string, basePostURL string) string {
878878
return mdImg.ReplaceAllString(in, "![$1]("+basePostURL+"$2)")
879879
}
880880

881+
// Same, but for HTML <img> tags embedded in Markdown.
882+
//
881883
// (?s) means multiline, (?U) means non-greedy.
882-
var htmlImg = regexp.MustCompile(`(?sU)src="([^"]*(png|PNG|jpeg|JPG|jpg|gif|GIF))"`)
884+
var htmlImg = regexp.MustCompile(`(?sU)src="([^"]*(png|PNG|jpeg|JPG|jpg|gif|GIF|svg|SVG))"`)
883885

884886
func addPostURLInHTMLImages(in string, basePostURL string) string {
885887
return htmlImg.ReplaceAllString(in, `src="`+basePostURL+`$1"`)

main_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,19 @@ func Test_convertAnchorIDs(t *testing.T) {
3232
})
3333
}
3434
}
35+
36+
func Test_addPostURLInHTMLImages(t *testing.T) {
37+
tests := []struct {
38+
given, expect string
39+
}{
40+
{
41+
`<img alt="Super example" src="dnat-google-vpc-how-comes-back.svg" width="80%"/>`,
42+
`<img alt="Super example" src="/you-should-write-comments/dnat-google-vpc-how-comes-back.svg" width="80%"/>`,
43+
},
44+
}
45+
for i, tt := range tests {
46+
t.Run(strconv.Itoa(i), func(t *testing.T) {
47+
assert.Equal(t, tt.expect, addPostURLInHTMLImages(tt.given, "/you-should-write-comments/"))
48+
})
49+
}
50+
}

0 commit comments

Comments
 (0)