From b6f3160dbc3efe7a86d39d526a1780eb320eefd4 Mon Sep 17 00:00:00 2001 From: Peter De Wachter Date: Tue, 13 Aug 2019 17:44:23 +0200 Subject: [PATCH] add_mailto_subject: New rewrite function Dinosaur Comics (qwantz.com) likes to hide jokes in mailto: links, but miniflux's sanitizer strips those out. --- reader/rewrite/rewrite_functions.go | 33 +++++++++++++++++++++++++++++ reader/rewrite/rewriter.go | 2 ++ reader/rewrite/rewriter_test.go | 9 ++++++++ reader/rewrite/rules.go | 1 + 4 files changed, 45 insertions(+) diff --git a/reader/rewrite/rewrite_functions.go b/reader/rewrite/rewrite_functions.go index 667f3591..8988cc5a 100644 --- a/reader/rewrite/rewrite_functions.go +++ b/reader/rewrite/rewrite_functions.go @@ -7,6 +7,7 @@ package rewrite // import "miniflux.app/reader/rewrite" import ( "fmt" "html" + "net/url" "regexp" "strings" @@ -43,6 +44,38 @@ func addImageTitle(entryURL, entryContent string) string { return entryContent } +func addMailtoSubject(entryURL, entryContent string) string { + doc, err := goquery.NewDocumentFromReader(strings.NewReader(entryContent)) + if err != nil { + return entryContent + } + + matches := doc.Find(`a[href^="mailto:"]`) + + if matches.Length() > 0 { + matches.Each(func(i int, a *goquery.Selection) { + hrefAttr, _ := a.Attr("href") + + mailto, err := url.Parse(hrefAttr) + if err != nil { + return + } + + subject := mailto.Query().Get("subject") + if subject == "" { + return + } + + a.AppendHtml(" [" + html.EscapeString(subject) + "]") + }) + + output, _ := doc.Find("body").First().Html() + return output + } + + return entryContent +} + func addDynamicImage(entryURL, entryContent string) string { doc, err := goquery.NewDocumentFromReader(strings.NewReader(entryContent)) if err != nil { diff --git a/reader/rewrite/rewriter.go b/reader/rewrite/rewriter.go index 25c742d5..ad1b9a32 100644 --- a/reader/rewrite/rewriter.go +++ b/reader/rewrite/rewriter.go @@ -27,6 +27,8 @@ func Rewriter(entryURL, entryContent, customRewriteRules string) string { switch strings.TrimSpace(rule) { case "add_image_title": entryContent = addImageTitle(entryURL, entryContent) + case "add_mailto_subject": + entryContent = addMailtoSubject(entryURL, entryContent) case "add_dynamic_image": entryContent = addDynamicImage(entryURL, entryContent) case "add_youtube_video": diff --git a/reader/rewrite/rewriter_test.go b/reader/rewrite/rewriter_test.go index 9bd83e84..0fd49ba5 100644 --- a/reader/rewrite/rewriter_test.go +++ b/reader/rewrite/rewriter_test.go @@ -98,6 +98,15 @@ func TestRewriteWithXkcdAndNoImage(t *testing.T) { } } +func TestRewriteMailtoLink(t *testing.T) { + description := `contact` + output := Rewriter("https://www.qwantz.com/", description, ``) + expected := `contact [blah blah]` + if expected != output { + t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected) + } +} + func TestRewriteWithPDFLink(t *testing.T) { description := "test" output := Rewriter("https://example.org/document.pdf", description, ``) diff --git a/reader/rewrite/rules.go b/reader/rewrite/rules.go index 500459f2..62df1ed7 100644 --- a/reader/rewrite/rules.go +++ b/reader/rewrite/rules.go @@ -22,6 +22,7 @@ var predefinedRules = map[string]string{ "oglaf.com": "add_image_title", "optipess.com": "add_image_title", "peebleslab.com": "add_image_title", + "www.qwantz.com": "add_image_title,add_mailto_subject", "sentfromthemoon.com": "add_image_title", "thedoghousediaries.com": "add_image_title", "treelobsters.com": "add_image_title",