From 9b614bc92286827479df0de0c231498eb8d48c16 Mon Sep 17 00:00:00 2001 From: Dami Date: Fri, 7 Aug 2020 00:05:43 -0600 Subject: [PATCH 1/3] Fix removal of query parameters on youtube embed links This uses go's html and url parser plus regex, instead of using only a single regex for simplicity sake. A single regex expression might be error prone, for example, when trying to matching html entities. Fixes #328 --- postrender.go | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/postrender.go b/postrender.go index f917b6e..ff06b11 100644 --- a/postrender.go +++ b/postrender.go @@ -16,6 +16,7 @@ import ( "html" "html/template" "net/http" + "net/url" "regexp" "strings" "unicode" @@ -73,6 +74,25 @@ func applyMarkdown(data []byte, baseURL string, cfg *config.Config) string { return applyMarkdownSpecial(data, false, baseURL, cfg) } +func disableYoutubeAutoplay(outHTML string) string { + for _, match := range youtubeReg.FindAllString(outHTML, -1) { + u, err := url.Parse(match) + if err != nil { + log.Error("Couldn't parse youtube url: %v", err) + } + u.RawQuery = html.UnescapeString(u.RawQuery) + q := u.Query() + // Set Youtube autoplay url parameter, if any, to 0 + if len(q["autoplay"]) == 1 { + q.Set("autoplay", "0") + } + u.RawQuery = q.Encode() + clean_url := u.String() + outHTML = strings.Replace(outHTML, match, clean_url, 1) + } + return outHTML +} + func applyMarkdownSpecial(data []byte, skipNoFollow bool, baseURL string, cfg *config.Config) string { mdExtensions := 0 | blackfriday.EXTENSION_TABLES | @@ -108,10 +128,7 @@ func applyMarkdownSpecial(data []byte, skipNoFollow bool, baseURL string, cfg *c // Strip newlines on certain block elements that render with them outHTML = blockReg.ReplaceAllString(outHTML, "<$1>") outHTML = endBlockReg.ReplaceAllString(outHTML, "") - // Remove all query parameters on YouTube embed links - // TODO: make this more specific. Taking the nuclear approach here to strip ?autoplay=1 - outHTML = youtubeReg.ReplaceAllString(outHTML, "$1") - + outHTML = disableYoutubeAutoplay(outHTML) return outHTML } From 3a789f5a00510afa9fa4a9ae32765c2102d9029a Mon Sep 17 00:00:00 2001 From: Dami Date: Tue, 8 Sep 2020 23:59:56 -0600 Subject: [PATCH 2/3] Go to next regex match if url parsing error --- postrender.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/postrender.go b/postrender.go index ff06b11..6181626 100644 --- a/postrender.go +++ b/postrender.go @@ -78,7 +78,7 @@ func disableYoutubeAutoplay(outHTML string) string { for _, match := range youtubeReg.FindAllString(outHTML, -1) { u, err := url.Parse(match) if err != nil { - log.Error("Couldn't parse youtube url: %v", err) + continue } u.RawQuery = html.UnescapeString(u.RawQuery) q := u.Query() From f847ade1efb6aa0e7e4e58fbe982dc9fca3b4796 Mon Sep 17 00:00:00 2001 From: Dami Date: Wed, 9 Sep 2020 00:01:32 -0600 Subject: [PATCH 3/3] Use camelCase --- postrender.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/postrender.go b/postrender.go index 6181626..d749317 100644 --- a/postrender.go +++ b/postrender.go @@ -87,8 +87,8 @@ func disableYoutubeAutoplay(outHTML string) string { q.Set("autoplay", "0") } u.RawQuery = q.Encode() - clean_url := u.String() - outHTML = strings.Replace(outHTML, match, clean_url, 1) + cleanURL := u.String() + outHTML = strings.Replace(outHTML, match, cleanURL, 1) } return outHTML }