| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591 |
- // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
- // SPDX-License-Identifier: Apache-2.0
- package rewrite // import "miniflux.app/v2/internal/reader/rewrite"
- import (
- "encoding/base64"
- "fmt"
- "html"
- "log/slog"
- "net/url"
- "regexp"
- "strconv"
- "strings"
- "unicode"
- "miniflux.app/v2/internal/config"
- nethtml "golang.org/x/net/html"
- "github.com/PuerkitoBio/goquery"
- )
- var (
- youtubeIdRegex = regexp.MustCompile(`youtube_id"?\s*[:=]\s*"([a-zA-Z0-9_-]{11})"`)
- textLinkRegex = regexp.MustCompile(`(?mi)(\bhttps?:\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])`)
- )
- // titlelize returns a copy of the string s with all Unicode letters that begin words
- // mapped to their Unicode title case.
- func titlelize(s string) string {
- // A closure is used here to remember the previous character
- // so that we can check if there is a space preceding the current
- // character.
- previous := ' '
- return strings.Map(
- func(current rune) rune {
- if unicode.IsSpace(previous) {
- previous = current
- return unicode.ToTitle(current)
- }
- previous = current
- return current
- }, strings.ToLower(s))
- }
- func addImageTitle(entryContent string) string {
- doc, err := goquery.NewDocumentFromReader(strings.NewReader(entryContent))
- if err != nil {
- return entryContent
- }
- matches := doc.Find("img[src][title]")
- if matches.Length() > 0 {
- matches.Each(func(i int, img *goquery.Selection) {
- altAttr := img.AttrOr("alt", "")
- srcAttr, _ := img.Attr("src")
- titleAttr, _ := img.Attr("title")
- img.ReplaceWithHtml(`<figure><img src="` + srcAttr + `" alt="` + altAttr + `"/><figcaption><p>` + html.EscapeString(titleAttr) + `</p></figcaption></figure>`)
- })
- output, _ := doc.FindMatcher(goquery.Single("body")).Html()
- return output
- }
- return entryContent
- }
- func addMailtoSubject(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.FindMatcher(goquery.Single("body")).Html()
- return output
- }
- return entryContent
- }
- func addDynamicImage(entryContent string) string {
- parserHtml, err := nethtml.ParseWithOptions(strings.NewReader(entryContent), nethtml.ParseOptionEnableScripting(false))
- if err != nil {
- return entryContent
- }
- doc := goquery.NewDocumentFromNode(parserHtml)
- // Ordered most preferred to least preferred.
- candidateAttrs := [...]string{
- "data-src",
- "data-original",
- "data-orig",
- "data-url",
- "data-orig-file",
- "data-large-file",
- "data-medium-file",
- "data-original-mos",
- "data-2000src",
- "data-1000src",
- "data-800src",
- "data-655src",
- "data-500src",
- "data-380src",
- }
- candidateSrcsetAttrs := [...]string{
- "data-srcset",
- }
- changed := false
- doc.Find("img,div").Each(func(i int, img *goquery.Selection) {
- // Src-linked candidates
- for _, candidateAttr := range candidateAttrs {
- if srcAttr, found := img.Attr(candidateAttr); found {
- changed = true
- if img.Is("img") {
- img.SetAttr("src", srcAttr)
- } else {
- altAttr := img.AttrOr("alt", "")
- img.ReplaceWithHtml(`<img src="` + srcAttr + `" alt="` + altAttr + `"/>`)
- }
- break
- }
- }
- // Srcset-linked candidates
- for _, candidateAttr := range candidateSrcsetAttrs {
- if srcAttr, found := img.Attr(candidateAttr); found {
- changed = true
- if img.Is("img") {
- img.SetAttr("srcset", srcAttr)
- } else {
- altAttr := img.AttrOr("alt", "")
- img.ReplaceWithHtml(`<img srcset="` + srcAttr + `" alt="` + altAttr + `"/>`)
- }
- break
- }
- }
- })
- if !changed {
- doc.Find("noscript").Each(func(i int, noscript *goquery.Selection) {
- if img := noscript.Find("img"); img.Length() == 1 {
- img.Unwrap()
- changed = true
- }
- })
- }
- if changed {
- output, _ := doc.FindMatcher(goquery.Single("body")).Html()
- return output
- }
- return entryContent
- }
- func addDynamicIframe(entryContent string) string {
- doc, err := goquery.NewDocumentFromReader(strings.NewReader(entryContent))
- if err != nil {
- return entryContent
- }
- // Ordered most preferred to least preferred.
- candidateAttrs := []string{
- "data-src",
- "data-original",
- "data-orig",
- "data-url",
- "data-lazy-src",
- }
- changed := false
- doc.Find("iframe").Each(func(i int, iframe *goquery.Selection) {
- for _, candidateAttr := range candidateAttrs {
- if srcAttr, found := iframe.Attr(candidateAttr); found {
- changed = true
- iframe.SetAttr("src", srcAttr)
- break
- }
- }
- })
- if changed {
- output, _ := doc.FindMatcher(goquery.Single("body")).Html()
- return output
- }
- return entryContent
- }
- func fixMediumImages(entryContent string) string {
- doc, err := goquery.NewDocumentFromReader(strings.NewReader(entryContent))
- if err != nil {
- return entryContent
- }
- doc.Find("figure.paragraph-image").Each(func(i int, paragraphImage *goquery.Selection) {
- noscriptElement := paragraphImage.Find("noscript")
- if noscriptElement.Length() > 0 {
- paragraphImage.ReplaceWithHtml(noscriptElement.Text())
- }
- })
- output, _ := doc.FindMatcher(goquery.Single("body")).Html()
- return output
- }
- func useNoScriptImages(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.FindMatcher(goquery.Single("body")).Html()
- return output
- }
- func getYoutubVideoIDFromURL(entryURL string) string {
- u, err := url.Parse(entryURL)
- if err != nil {
- return ""
- }
- if !strings.HasSuffix(u.Hostname(), "youtube.com") {
- return ""
- }
- if u.Path == "/watch" {
- if v := u.Query().Get("v"); v != "" {
- return v
- }
- return ""
- }
- if id, found := strings.CutPrefix(u.Path, "/shorts/"); found {
- if len(id) == 11 {
- // youtube shorts id are always 11 chars.
- return id
- }
- }
- return ""
- }
- func buildVideoPlayerIframe(absoluteVideoURL string) string {
- return `<iframe width="650" height="350" frameborder="0" src="` + absoluteVideoURL + `" allowfullscreen></iframe>`
- }
- func addVideoPlayerIframe(absoluteVideoURL, entryContent string) string {
- return buildVideoPlayerIframe(absoluteVideoURL) + `<br>` + entryContent
- }
- func addYoutubeVideoRewriteRule(entryURL, entryContent string) string {
- if videoURL := getYoutubVideoIDFromURL(entryURL); videoURL != "" {
- return addVideoPlayerIframe(config.Opts.YouTubeEmbedUrlOverride()+videoURL, entryContent)
- }
- return entryContent
- }
- func addYoutubeVideoUsingInvidiousPlayer(entryURL, entryContent string) string {
- if videoURL := getYoutubVideoIDFromURL(entryURL); videoURL != "" {
- return addVideoPlayerIframe(`https://`+config.Opts.InvidiousInstance()+`/embed/`+videoURL, entryContent)
- }
- return entryContent
- }
- // For reference: https://github.com/miniflux/v2/pull/1314
- func addYoutubeVideoFromId(entryContent string) string {
- matches := youtubeIdRegex.FindAllStringSubmatch(entryContent, -1)
- if matches == nil {
- return entryContent
- }
- var videoPlayerHTML strings.Builder
- for _, match := range matches {
- if len(match) == 2 {
- videoPlayerHTML.WriteString(buildVideoPlayerIframe(config.Opts.YouTubeEmbedUrlOverride() + match[1]))
- videoPlayerHTML.WriteString("<br>")
- }
- }
- return videoPlayerHTML.String() + entryContent
- }
- func addInvidiousVideo(entryURL, entryContent string) string {
- u, err := url.Parse(entryURL)
- if err != nil {
- return entryContent
- }
- if u.Path != "/watch" {
- return entryContent
- }
- qs := u.Query()
- videoID := qs.Get("v")
- if videoID == "" {
- return entryContent
- }
- qs.Del("v")
- embedVideoURL := "https://" + u.Hostname() + `/embed/` + videoID
- if len(qs) > 0 {
- embedVideoURL += "?" + qs.Encode()
- }
- return addVideoPlayerIframe(embedVideoURL, entryContent)
- }
- func addPDFLink(entryURL, entryContent string) string {
- if strings.HasSuffix(entryURL, ".pdf") {
- return fmt.Sprintf(`<a href=%q>PDF</a><br>%s`, entryURL, entryContent)
- }
- return entryContent
- }
- func replaceTextLinks(input string) string {
- return textLinkRegex.ReplaceAllString(input, `<a href="${1}">${1}</a>`)
- }
- func replaceCustom(entryContent string, searchTerm string, replaceTerm string) string {
- re, err := regexp.Compile(searchTerm)
- if err == nil {
- return re.ReplaceAllString(entryContent, replaceTerm)
- }
- return entryContent
- }
- func removeCustom(entryContent string, selector string) string {
- doc, err := goquery.NewDocumentFromReader(strings.NewReader(entryContent))
- if err != nil {
- return entryContent
- }
- doc.Find(selector).Remove()
- output, _ := doc.FindMatcher(goquery.Single("body")).Html()
- return output
- }
- func addCastopodEpisode(entryURL, entryContent string) string {
- player := `<iframe width="650" frameborder="0" src="` + entryURL + `/embed/light"></iframe>`
- return player + `<br>` + entryContent
- }
- func applyFuncOnTextContent(entryContent string, selector string, repl func(string) string) string {
- var treatChildren func(i int, s *goquery.Selection)
- treatChildren = func(i int, s *goquery.Selection) {
- if s.Nodes[0].Type == nethtml.TextNode {
- s.ReplaceWithHtml(repl(s.Nodes[0].Data))
- } else {
- s.Contents().Each(treatChildren)
- }
- }
- doc, err := goquery.NewDocumentFromReader(strings.NewReader(entryContent))
- if err != nil {
- return entryContent
- }
- doc.Find(selector).Each(treatChildren)
- output, _ := doc.FindMatcher(goquery.Single("body")).Html()
- return output
- }
- func decodeBase64Content(entryContent string) string {
- if ret, err := base64.StdEncoding.DecodeString(strings.TrimSpace(entryContent)); err != nil {
- return entryContent
- } else {
- return html.EscapeString(string(ret))
- }
- }
- 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
- }
- switch app {
- case "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)
- case "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)
- default:
- slog.Warn("Unknown app provided for openHackerNewsLinksWith rewrite rule",
- slog.String("app", app),
- )
- return
- }
- })
- output, _ := doc.FindMatcher(goquery.Single("body")).Html()
- return output
- }
- return entryContent
- }
- func removeTables(entryContent string) string {
- doc, err := goquery.NewDocumentFromReader(strings.NewReader(entryContent))
- if err != nil {
- return entryContent
- }
- selectors := []string{"table", "tbody", "thead", "td", "th", "td"}
- var loopElement *goquery.Selection
- for _, selector := range selectors {
- for {
- loopElement = doc.FindMatcher(goquery.Single(selector))
- if loopElement.Length() == 0 {
- break
- }
- innerHtml, err := loopElement.Html()
- if err != nil {
- break
- }
- loopElement.Parent().AppendHtml(innerHtml)
- loopElement.Remove()
- }
- }
- output, _ := doc.FindMatcher(goquery.Single("body")).Html()
- return output
- }
- func fixGhostCards(entryContent string) string {
- doc, err := goquery.NewDocumentFromReader(strings.NewReader(entryContent))
- if err != nil {
- return entryContent
- }
- const cardSelector = "figure.kg-card"
- var currentList *goquery.Selection
- doc.Find(cardSelector).Each(func(i int, s *goquery.Selection) {
- title := s.Find(".kg-bookmark-title").First().Text()
- author := s.Find(".kg-bookmark-author").First().Text()
- href := s.Find("a.kg-bookmark-container").First().AttrOr("href", "")
- // if there is no link or title, skip processing
- if href == "" || title == "" {
- return
- }
- link := ""
- if author == "" || strings.HasSuffix(title, author) {
- link = fmt.Sprintf("<a href=\"%s\">%s</a>", href, title)
- } else {
- link = fmt.Sprintf("<a href=\"%s\">%s - %s</a>", href, title, author)
- }
- next := s.Next()
- // if the next element is also a card, start a list
- if next.Is(cardSelector) && currentList == nil {
- currentList = s.BeforeHtml("<ul></ul>").Prev()
- }
- if currentList != nil {
- // add this card to the list, then delete it
- currentList.AppendHtml("<li>" + link + "</li>")
- s.Remove()
- } else {
- // replace single card
- s.ReplaceWithHtml(link)
- }
- // if the next element is not a card, start a new list
- if !next.Is(cardSelector) && currentList != nil {
- currentList = nil
- }
- })
- output, _ := doc.FindMatcher(goquery.Single("body")).Html()
- return strings.TrimSpace(output)
- }
- func removeImgBlurParams(entryContent string) string {
- doc, err := goquery.NewDocumentFromReader(strings.NewReader(entryContent))
- if err != nil {
- return entryContent
- }
- changed := false
- doc.Find("img[src]").Each(func(i int, img *goquery.Selection) {
- srcAttr, exists := img.Attr("src")
- if !exists {
- return
- }
- parsedURL, err := url.Parse(srcAttr)
- if err != nil {
- return
- }
- // Only strip query parameters if this is a blurry placeholder image
- if parsedURL.RawQuery != "" {
- // Check if there's a blur parameter with a non-zero value
- if blurValue := parsedURL.Query().Get("blur"); blurValue != "" {
- if blurInt, err := strconv.Atoi(blurValue); err == nil && blurInt > 0 {
- parsedURL.RawQuery = ""
- img.SetAttr("src", parsedURL.String())
- changed = true
- }
- }
- }
- })
- if changed {
- output, _ := doc.FindMatcher(goquery.Single("body")).Html()
- return output
- }
- return entryContent
- }
|