feat: add article page

This commit is contained in:
Solomon Victorino
2024-06-23 16:38:53 -06:00
parent 941cafc664
commit 0ced2495ee
10 changed files with 279 additions and 34 deletions

View File

@ -6,10 +6,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"regexp"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/gorilla/mux"
"github.com/rramiachraf/dumb/data"
"github.com/rramiachraf/dumb/utils"
@ -67,7 +64,7 @@ func annotations(l *utils.Logger) http.HandlerFunc {
}
body := data.Response.Referent.Annotations[0].Body
body.HTML = cleanBody(body.HTML)
body.HTML = utils.CleanBody(body.HTML)
w.Header().Set("content-type", "application/json")
encoder := json.NewEncoder(w)
@ -82,31 +79,3 @@ func annotations(l *utils.Logger) http.HandlerFunc {
}
}
}
func cleanBody(body string) string {
if doc, err := goquery.NewDocumentFromReader(strings.NewReader(body)); err == nil {
doc.Find("iframe").Each(func(i int, s *goquery.Selection) {
src, exists := s.Attr("src")
if exists {
html := fmt.Sprintf(`<a id="iframed-link" href="%s">Link</a>`, src)
s.ReplaceWithHtml(html)
}
})
doc.Find("img").Each(func(i int, s *goquery.Selection) {
src, exists := s.Attr("src")
if exists {
re := regexp.MustCompile(`(?i)https:\/\/images\.(rapgenius|genius)\.com\/`)
pSrc := re.ReplaceAllString(src, "/images/")
s.SetAttr("src", pSrc)
}
})
if source, err := doc.Html(); err == nil {
body = source
}
}
re := regexp.MustCompile(`https?:\/\/[a-z]*.?genius.com`)
return re.ReplaceAllString(body, "")
}