Skip to content

Commit 94c48c2

Browse files
committed
chore: add tests for replaceYoutubeURL
1 parent a65286b commit 94c48c2

File tree

4 files changed

+39
-9
lines changed

4 files changed

+39
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ $ make build_prod
7474
1. [x] Set the WordPress homepage correctly
7575
1. [x] Migrate the RSS feed with existing UUIDs, so that entries appear the same - this is important for anyone with a significant feed following, see more details of a [failed migration](https://theorangeone.net/posts/rss-guids/)
7676
1. [x] Migrate favicon.ico
77-
1. [x] Migrate [YouTube embeds](https://support.google.com/youtube/answer/171780)
77+
1. [x] Migrate [YouTube embeds](https://support.google.com/youtube/answer/171780), including WordPress-style plain-text URLs [pasted](https://wordpress.org/documentation/article/youtube-embed/) into the content
7878
1. [x] Migrate [Google Map embed](https://developers.google.com/maps/documentation/embed/get-started) via a custom shortcode `googlemaps`
7979
1. [x] Migrate [GitHub gists](https://gist.github.com/)
8080
1. [x] Migrate `caption` (WordPress) to `figure` (Hugo)

src/wp2hugo/internal/hugogenerator/hugopage/hugo_page.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ func (page *Page) getMarkdown(provider ImageURLProvider, htmlContent string, foo
280280

281281
markdown = replaceOrderedListNumbers(markdown)
282282
markdown = replaceConsecutiveNewlines(markdown)
283-
markdown = replaceYoutubeURL(markdown)
283+
markdown = replacePlaintextYoutubeURL(markdown)
284284
markdown = removeTrailingSpaces(markdown)
285285

286286
return &markdown, nil

src/wp2hugo/internal/hugogenerator/hugopage/wordpress_youtube_converter.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,14 @@ import (
88
)
99

1010
// Example: Plain-text Youtube URLs on their own line in post content are turned by WP into embeds
11-
var _YoutubeRegEx = regexp.MustCompile(`(?m)(?:^|\s)https?://(?:m.|www.)?(?:youtu.be|youtube.com)/(?:watch|w)\?v=([^&\s]+)`)
11+
var _YoutubeRegEx = regexp.MustCompile(`(?m)(^|\s)https?://(?:m.|www.)?(?:youtu.be|youtube.com)/(?:watch|w)\?v=([^&\s]+)`)
1212

13-
func replaceYoutubeURL(htmlData string) string {
13+
func replacePlaintextYoutubeURL(htmlData string) string {
1414
log.Debug().
1515
Msg("Replacing Youtube URLs with embeds")
16-
17-
htmlData = replaceAllStringSubmatchFunc(_YoutubeRegEx, htmlData, YoutubeReplacementFunction)
18-
19-
return htmlData
16+
return replaceAllStringSubmatchFunc(_YoutubeRegEx, htmlData, YoutubeReplacementFunction)
2017
}
2118

2219
func YoutubeReplacementFunction(groups []string) string {
23-
return fmt.Sprintf(`{{< youtube %s >}}`, groups[1])
20+
return fmt.Sprintf(`%s{{< youtube %s >}}`, groups[1], groups[2])
2421
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package hugopage
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestReplaceYoutubeURL1(t *testing.T) {
10+
const htmlData = "This is a test with a youtube link:\nhttps://www.youtube.com/watch?v=gL0-m1Qlohg"
11+
const expected = "This is a test with a youtube link:\n{{< youtube gL0-m1Qlohg >}}"
12+
assert.Equal(t, expected, replacePlaintextYoutubeURL(htmlData))
13+
}
14+
15+
func TestReplaceYoutubeURL2(t *testing.T) {
16+
const htmlData = "This is a test with a youtube link: https://www.youtube.com/watch?v=8K7PdBH3W_I"
17+
const expected = "This is a test with a youtube link: {{< youtube 8K7PdBH3W_I >}}"
18+
assert.Equal(t, expected, replacePlaintextYoutubeURL(htmlData))
19+
}
20+
21+
func TestReplaceYoutubeURL3(t *testing.T) {
22+
const htmlData = "This is a test with a youtube link:\thttps://www.youtube.com/watch?v=gJ7AAJXHeeg whatever"
23+
const expected = "This is a test with a youtube link:\t{{< youtube gJ7AAJXHeeg >}} whatever"
24+
assert.Equal(t, expected, replacePlaintextYoutubeURL(htmlData))
25+
}
26+
27+
func TestReplaceNonPlaintextYouTubeURL(t *testing.T) {
28+
const htmlData = `This is a test with a youtube link <a href="https://www.youtube.com/watch?v=gJ7AAJXHeeg" and
29+
embed <iframe width="560" height="315" src="https://www.youtube.com/embed/Wz6ml5SpkKM?si=rrx_5_80TE3Mz7Co"
30+
title="YouTube video player" frameborder="0" allowfullscreen></iframe>`
31+
// Assert that the function does not replace the youtube URL in the iframe or the link
32+
assert.Equal(t, htmlData, replacePlaintextYoutubeURL(htmlData))
33+
}

0 commit comments

Comments
 (0)