aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Adriano Di Luzio <aldur@users.noreply.github.com> 2023-09-23 22:54:48 +0200
committerGravatar GitHub <noreply@github.com> 2023-09-23 13:54:48 -0700
commit54cb8fa0286e4a2f1a81c32b5a89722d93b30bf7 (patch)
tree219d0462300edd87ba86a03acc817048d092df3e
parentace2699e79347c84ea1583e11abe6cabba85bf20 (diff)
downloadv2-54cb8fa0286e4a2f1a81c32b5a89722d93b30bf7.tar.gz
v2-54cb8fa0286e4a2f1a81c32b5a89722d93b30bf7.tar.zst
v2-54cb8fa0286e4a2f1a81c32b5a89722d93b30bf7.zip
Added new rewrite rules `add_hn_links_using_hack` and `add_hn_links_using_opener` to open HN comments with iOS apps
-rw-r--r--internal/reader/rewrite/rewrite_functions.go50
-rw-r--r--internal/reader/rewrite/rewriter.go4
-rw-r--r--internal/reader/rewrite/rewriter_test.go46
-rw-r--r--internal/reader/sanitizer/sanitizer.go4
4 files changed, 104 insertions, 0 deletions
diff --git a/internal/reader/rewrite/rewrite_functions.go b/internal/reader/rewrite/rewrite_functions.go
index 0e4aadbb..6950c333 100644
--- a/internal/reader/rewrite/rewrite_functions.go
+++ b/internal/reader/rewrite/rewrite_functions.go
@@ -12,6 +12,7 @@ import (
"strings"
"miniflux.app/v2/internal/config"
+ "miniflux.app/v2/internal/logger"
"github.com/PuerkitoBio/goquery"
"github.com/yuin/goldmark"
@@ -321,6 +322,55 @@ func decodeBase64Content(entryContent string) string {
}
}
+func addHackerNewsLinksUsing(entryContent, app string) string {
+ doc, err := goquery.NewDocumentFromReader(strings.NewReader(entryContent))
+ if err != nil {
+ return entryContent
+ }
+
+ hn_prefix := "https://news.ycombinator.com/"
+ matches := doc.Find(`a[href^="` + hn_prefix + `"]`)
+
+ if matches.Length() > 0 {
+ matches.Each(func(i int, a *goquery.Selection) {
+ hrefAttr, _ := a.Attr("href")
+
+ hn_uri, err := url.Parse(hrefAttr)
+ if err != nil {
+ return
+ }
+
+ if app == "opener" {
+ params := url.Values{}
+ params.Add("url", hn_uri.String())
+
+ url := url.URL{
+ Scheme: "opener",
+ Host: "x-callback-url",
+ Path: "show-options",
+ RawQuery: params.Encode(),
+ }
+
+ open_with_opener := `<a href="` + url.String() + `">Open with Opener</a>`
+ a.Parent().AppendHtml(" " + open_with_opener)
+ } else if app == "hack" {
+ url := strings.Replace(hn_uri.String(), hn_prefix, "hack://", 1)
+
+ open_with_hack := `<a href="` + url + `">Open with HACK</a>`
+ a.Parent().AppendHtml(" " + open_with_hack)
+ } else {
+ logger.Error("[openHackerNewsLinksWith] unknown app provided: %q", app)
+ return
+ }
+ })
+
+ output, _ := doc.Find("body").First().Html()
+ return output
+ }
+
+ return entryContent
+}
+
func parseMarkdown(entryContent string) string {
var sb strings.Builder
md := goldmark.New(
diff --git a/internal/reader/rewrite/rewriter.go b/internal/reader/rewrite/rewriter.go
index 40ca492f..65c66ff2 100644
--- a/internal/reader/rewrite/rewriter.go
+++ b/internal/reader/rewrite/rewriter.go
@@ -113,6 +113,10 @@ func applyRule(entryURL string, entry *model.Entry, rule rule) {
} else {
entry.Content = applyFuncOnTextContent(entry.Content, "body", decodeBase64Content)
}
+ case "add_hn_links_using_hack":
+ entry.Content = addHackerNewsLinksUsing(entry.Content, "hack")
+ case "add_hn_links_using_opener":
+ entry.Content = addHackerNewsLinksUsing(entry.Content, "opener")
case "parse_markdown":
entry.Content = parseMarkdown(entry.Content)
case "remove_tables":
diff --git a/internal/reader/rewrite/rewriter_test.go b/internal/reader/rewrite/rewriter_test.go
index 6b9d2013..bbf5bfdd 100644
--- a/internal/reader/rewrite/rewriter_test.go
+++ b/internal/reader/rewrite/rewriter_test.go
@@ -577,3 +577,49 @@ func TestRemoveClickbait(t *testing.T) {
t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
}
}
+
+func TestAddHackerNewsLinksUsingHack(t *testing.T) {
+ testEntry := &model.Entry{
+ Title: `A title`,
+ Content: `<p>Article URL: <a href="https://example.org/url">https://example.org/article</a></p>
+ <p>Comments URL: <a href="https://news.ycombinator.com/item?id=37620043">https://news.ycombinator.com/item?id=37620043</a></p>
+ <p>Points: 23</p>
+ <p># Comments: 38</p>`,
+ }
+
+ controlEntry := &model.Entry{
+ Title: `A title`,
+ Content: `<p>Article URL: <a href="https://example.org/url">https://example.org/article</a></p>
+ <p>Comments URL: <a href="https://news.ycombinator.com/item?id=37620043">https://news.ycombinator.com/item?id=37620043</a> <a href="hack://item?id=37620043">Open with HACK</a></p>
+ <p>Points: 23</p>
+ <p># Comments: 38</p>`,
+ }
+ Rewriter("https://example.org/article", testEntry, `add_hn_links_using_hack`)
+
+ if !reflect.DeepEqual(testEntry, controlEntry) {
+ t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
+ }
+}
+
+func TestAddHackerNewsLinksUsingOpener(t *testing.T) {
+ testEntry := &model.Entry{
+ Title: `A title`,
+ Content: `<p>Article URL: <a href="https://example.org/url">https://example.org/article</a></p>
+ <p>Comments URL: <a href="https://news.ycombinator.com/item?id=37620043">https://news.ycombinator.com/item?id=37620043</a></p>
+ <p>Points: 23</p>
+ <p># Comments: 38</p>`,
+ }
+
+ controlEntry := &model.Entry{
+ Title: `A title`,
+ Content: `<p>Article URL: <a href="https://example.org/url">https://example.org/article</a></p>
+ <p>Comments URL: <a href="https://news.ycombinator.com/item?id=37620043">https://news.ycombinator.com/item?id=37620043</a> <a href="opener://x-callback-url/show-options?url=https%3A%2F%2Fnews.ycombinator.com%2Fitem%3Fid%3D37620043">Open with Opener</a></p>
+ <p>Points: 23</p>
+ <p># Comments: 38</p>`,
+ }
+ Rewriter("https://example.org/article", testEntry, `add_hn_links_using_opener`)
+
+ if !reflect.DeepEqual(testEntry, controlEntry) {
+ t.Errorf(`Not expected output: got "%+v" instead of "%+v"`, testEntry, controlEntry)
+ }
+}
diff --git a/internal/reader/sanitizer/sanitizer.go b/internal/reader/sanitizer/sanitizer.go
index 84c15dc3..958058ed 100644
--- a/internal/reader/sanitizer/sanitizer.go
+++ b/internal/reader/sanitizer/sanitizer.go
@@ -297,6 +297,10 @@ func hasValidURIScheme(src string) bool {
"tel:",
"webcal://",
"xmpp:",
+
+ // iOS Apps
+ "opener://", // https://www.opener.link
+ "hack://", // https://apps.apple.com/it/app/hack-for-hacker-news-reader/id1464477788?l=en-GB
}
for _, prefix := range whitelist {