diff options
author | Frédéric Guillot <fred@miniflux.net> | 2018-10-08 20:47:10 -0700 |
---|---|---|
committer | Frédéric Guillot <fred@miniflux.net> | 2018-10-08 20:47:10 -0700 |
commit | 9606126196e520bbae405e2a837daa00ca512c64 (patch) | |
tree | 6f9e4c3ea9556151d90df7b6fd8d5b3a8d4010f1 /reader/rewrite | |
parent | d4c1677e3860667f7b7ebf61d11d33b9495da64d (diff) |
Convert text links and line feeds to HTML in YouTube channels
Diffstat (limited to 'reader/rewrite')
-rw-r--r-- | reader/rewrite/rewrite_functions.go | 15 | ||||
-rw-r--r-- | reader/rewrite/rewriter_test.go | 24 |
2 files changed, 34 insertions, 5 deletions
diff --git a/reader/rewrite/rewrite_functions.go b/reader/rewrite/rewrite_functions.go index fee2a85..6ce9693 100644 --- a/reader/rewrite/rewrite_functions.go +++ b/reader/rewrite/rewrite_functions.go @@ -13,8 +13,9 @@ import ( ) var ( - youtubeRegex = regexp.MustCompile(`youtube\.com/watch\?v=(.*)`) - imgRegex = regexp.MustCompile(`<img [^>]+>`) + youtubeRegex = regexp.MustCompile(`youtube\.com/watch\?v=(.*)`) + imgRegex = regexp.MustCompile(`<img [^>]+>`) + textLinkRegex = regexp.MustCompile(`(?mi)(\bhttps?:\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])`) ) func addImageTitle(entryURL, entryContent string) string { @@ -108,7 +109,7 @@ func addYoutubeVideo(entryURL, entryContent string) string { if len(matches) == 2 { video := `<iframe width="650" height="350" frameborder="0" src="https://www.youtube-nocookie.com/embed/` + matches[1] + `" allowfullscreen></iframe>` - return video + "<p>" + entryContent + "</p>" + return video + "<p>" + replaceLineFeeds(replaceTextLinks(entryContent)) + "</p>" } return entryContent } @@ -119,3 +120,11 @@ func addPDFLink(entryURL, entryContent string) string { } return entryContent } + +func replaceTextLinks(input string) string { + return textLinkRegex.ReplaceAllString(input, `<a href="${1}">${1}</a>`) +} + +func replaceLineFeeds(input string) string { + return strings.Replace(input, "\n", "<br>", -1) +} diff --git a/reader/rewrite/rewriter_test.go b/reader/rewrite/rewriter_test.go index 7ceb0dc..fe37b53 100644 --- a/reader/rewrite/rewriter_test.go +++ b/reader/rewrite/rewriter_test.go @@ -6,6 +6,26 @@ package rewrite // import "miniflux.app/reader/rewrite" import "testing" +func TestReplaceTextLinks(t *testing.T) { + scenarios := map[string]string{ + `This is a link to example.org`: `This is a link to example.org`, + `This is a link to ftp://example.org`: `This is a link to ftp://example.org`, + `This is a link to www.example.org`: `This is a link to www.example.org`, + `This is a link to http://example.org`: `This is a link to <a href="http://example.org">http://example.org</a>`, + `This is a link to http://example.org, end of sentence.`: `This is a link to <a href="http://example.org">http://example.org</a>, end of sentence.`, + `This is a link to https://example.org`: `This is a link to <a href="https://example.org">https://example.org</a>`, + `This is a link to https://www.example.org/path/to?q=s`: `This is a link to <a href="https://www.example.org/path/to?q=s">https://www.example.org/path/to?q=s</a>`, + `This is a link to https://example.org/index#hash-tag, http://example.org/.`: `This is a link to <a href="https://example.org/index#hash-tag">https://example.org/index#hash-tag</a>, <a href="http://example.org/">http://example.org/</a>.`, + } + + for input, expected := range scenarios { + actual := replaceTextLinks(input) + if actual != expected { + t.Errorf(`Unexpected link replacement, got "%s" instead of "%s"`, actual, expected) + } + } +} + func TestRewriteWithNoMatchingRule(t *testing.T) { output := Rewriter("https://example.org/article", `Some text.`, ``) expected := `Some text.` @@ -16,8 +36,8 @@ func TestRewriteWithNoMatchingRule(t *testing.T) { } func TestRewriteWithYoutubeLink(t *testing.T) { - output := Rewriter("https://www.youtube.com/watch?v=1234", `Video Description`, ``) - expected := `<iframe width="650" height="350" frameborder="0" src="https://www.youtube-nocookie.com/embed/1234" allowfullscreen></iframe><p>Video Description</p>` + output := Rewriter("https://www.youtube.com/watch?v=1234", "Video Description\nhttp://example.org/path", ``) + expected := `<iframe width="650" height="350" frameborder="0" src="https://www.youtube-nocookie.com/embed/1234" allowfullscreen></iframe><p>Video Description<br><a href="http://example.org/path">http://example.org/path</a></p>` if expected != output { t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected) |