Skip to content

Commit

Permalink
chore: add tests for replaceYoutubeURL
Browse files Browse the repository at this point in the history
  • Loading branch information
ashishb committed Sep 11, 2024
1 parent a65286b commit 1735445
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/wp2hugo/internal/hugogenerator/hugopage/hugo_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ func (page *Page) getMarkdown(provider ImageURLProvider, htmlContent string, foo

markdown = replaceOrderedListNumbers(markdown)
markdown = replaceConsecutiveNewlines(markdown)
markdown = replaceYoutubeURL(markdown)
markdown = replacePlaintextYoutubeURL(markdown)
markdown = removeTrailingSpaces(markdown)

return &markdown, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
)

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

func replaceYoutubeURL(htmlData string) string {
func replacePlaintextYoutubeURL(htmlData string) string {
log.Debug().
Msg("Replacing Youtube URLs with embeds")

Expand All @@ -20,5 +20,5 @@ func replaceYoutubeURL(htmlData string) string {
}

func YoutubeReplacementFunction(groups []string) string {
return fmt.Sprintf(`{{< youtube %s >}}`, groups[1])
return fmt.Sprintf(`%s{{< youtube %s >}}`, groups[1], groups[2])
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package hugopage

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestReplaceYoutubeURL1(t *testing.T) {
const htmlData = "This is a test with a youtube link:\nhttps://www.youtube.com/watch?v=1234567890"
const expected = "This is a test with a youtube link:\n{{< youtube 1234567890 >}}"
assert.Equal(t, expected, replacePlaintextYoutubeURL(htmlData))
}

func TestReplaceYoutubeURL2(t *testing.T) {
const htmlData = "This is a test with a youtube link: https://www.youtube.com/watch?v=1234567890"
const expected = "This is a test with a youtube link: {{< youtube 1234567890 >}}"
assert.Equal(t, expected, replacePlaintextYoutubeURL(htmlData))
}

func TestReplaceYoutubeURL3(t *testing.T) {
const htmlData = "This is a test with a youtube link:\thttps://www.youtube.com/watch?v=1234567890 whatever"
const expected = "This is a test with a youtube link:\t{{< youtube 1234567890 >}} whatever"
assert.Equal(t, expected, replacePlaintextYoutubeURL(htmlData))
}

0 comments on commit 1735445

Please sign in to comment.