diff --git a/reader/rewrite/rewrite_functions.go b/reader/rewrite/rewrite_functions.go index ea162af568ed8c514e36a6b509a4eb5342820d85..17bff9f09008fd6c7aaf54189bd455ef8a3e8720 100644 --- a/reader/rewrite/rewrite_functions.go +++ b/reader/rewrite/rewrite_functions.go @@ -147,7 +147,31 @@ func fixMediumImages(entryURL, entryContent string) string { doc.Find("figure.paragraph-image").Each(func(i int, paragraphImage *goquery.Selection) { noscriptElement := paragraphImage.Find("noscript") - paragraphImage.ReplaceWithHtml(noscriptElement.Text()) + if noscriptElement.Length() > 0 { + paragraphImage.ReplaceWithHtml(noscriptElement.Text()) + } + }) + + output, _ := doc.Find("body").First().Html() + return output +} + +func useNoScriptImages(entryURL, entryContent string) string { + doc, err := goquery.NewDocumentFromReader(strings.NewReader(entryContent)) + if err != nil { + return entryContent + } + + doc.Find("figure").Each(func(i int, figureElement *goquery.Selection) { + imgElement := figureElement.Find("img") + if imgElement.Length() > 0 { + noscriptElement := figureElement.Find("noscript") + if noscriptElement.Length() > 0 { + figureElement.PrependHtml(noscriptElement.Text()) + imgElement.Remove() + noscriptElement.Remove() + } + } }) output, _ := doc.Find("body").First().Html() diff --git a/reader/rewrite/rewriter.go b/reader/rewrite/rewriter.go index 8c26719c6b358b285f05958750313abe4980434d..91955247190e86ab60f00951075a0d15363dc973 100644 --- a/reader/rewrite/rewriter.go +++ b/reader/rewrite/rewriter.go @@ -45,6 +45,8 @@ func Rewriter(entryURL, entryContent, customRewriteRules string) string { entryContent = replaceTextLinks(entryContent) case "fix_medium_images": entryContent = fixMediumImages(entryURL, entryContent) + case "use_noscript_figure_images": + entryContent = useNoScriptImages(entryURL, entryContent) } } diff --git a/reader/rewrite/rewriter_test.go b/reader/rewrite/rewriter_test.go index 04f4c657aa8270792cffd8cc0bfa876004b59981..d8d78fd6de007297bc3d28c3512cc7748a691cd6 100644 --- a/reader/rewrite/rewriter_test.go +++ b/reader/rewrite/rewriter_test.go @@ -208,3 +208,25 @@ func TestMediumImage(t *testing.T) { t.Errorf(`Not expected output: %s`, output) } } + +func TestRewriteNoScriptImageWithoutNoScriptTag(t *testing.T) { + content := `<figure><img src="https://developer.mozilla.org/static/img/favicon144.png" alt="The beautiful MDN logo."><figcaption>MDN Logo</figcaption></figure>` + expected := `<figure><img src="https://developer.mozilla.org/static/img/favicon144.png" alt="The beautiful MDN logo."/><figcaption>MDN Logo</figcaption></figure>` + output := Rewriter("https://example.org/article", content, "use_noscript_figure_images") + output = strings.TrimSpace(output) + + if expected != output { + t.Errorf(`Not expected output: %s`, output) + } +} + +func TestRewriteNoScriptImageWithNoScriptTag(t *testing.T) { + content := `<figure><img src="https://developer.mozilla.org/static/img/favicon144.png" alt="The beautiful MDN logo."><noscript><img src="http://example.org/logo.svg"></noscript><figcaption>MDN Logo</figcaption></figure>` + expected := `<figure><img src="http://example.org/logo.svg"/><figcaption>MDN Logo</figcaption></figure>` + output := Rewriter("https://example.org/article", content, "use_noscript_figure_images") + output = strings.TrimSpace(output) + + if expected != output { + t.Errorf(`Not expected output: %s`, output) + } +}