Skip to content

Commit

Permalink
Fix removal of query parameters on youtube embed links
Browse files Browse the repository at this point in the history
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 writefreely#328
  • Loading branch information
Obayanju committed Aug 7, 2020
1 parent 849e5b8 commit 9b614bc
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions postrender.go
Expand Up @@ -16,6 +16,7 @@ import (
"html"
"html/template"
"net/http"
"net/url"
"regexp"
"strings"
"unicode"
Expand Down Expand Up @@ -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 |
Expand Down Expand Up @@ -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, "</$1></$2>")
// 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
}

Expand Down

0 comments on commit 9b614bc

Please sign in to comment.