rewriter.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package rewrite
  5. import (
  6. "strings"
  7. "github.com/miniflux/miniflux/url"
  8. )
  9. // Rewriter modify item contents with a set of rewriting rules.
  10. func Rewriter(entryURL, entryContent, customRewriteRules string) string {
  11. rulesList := getPredefinedRewriteRules(entryURL)
  12. if customRewriteRules != "" {
  13. rulesList = customRewriteRules
  14. }
  15. rules := strings.Split(rulesList, ",")
  16. rules = append(rules, "add_pdf_download_link")
  17. for _, rule := range rules {
  18. switch strings.TrimSpace(rule) {
  19. case "add_image_title":
  20. entryContent = addImageTitle(entryURL, entryContent)
  21. case "add_youtube_video":
  22. entryContent = addYoutubeVideo(entryURL, entryContent)
  23. case "add_pdf_download_link":
  24. entryContent = addPDFLink(entryURL, entryContent)
  25. }
  26. }
  27. return entryContent
  28. }
  29. func getPredefinedRewriteRules(entryURL string) string {
  30. urlDomain := url.Domain(entryURL)
  31. for domain, rules := range predefinedRules {
  32. if strings.Contains(urlDomain, domain) {
  33. return rules
  34. }
  35. }
  36. return ""
  37. }