feat: improve annotation UI and set iframes into anchor tags

This commit is contained in:
rramiachraf 2024-03-05 22:01:32 +01:00
parent 23b11d2edf
commit e1b3885680
2 changed files with 40 additions and 5 deletions

View File

@ -7,7 +7,9 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"regexp" "regexp"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/rramiachraf/dumb/data" "github.com/rramiachraf/dumb/data"
"github.com/rramiachraf/dumb/views" "github.com/rramiachraf/dumb/views"
@ -82,11 +84,29 @@ func Annotations(l *logrus.Logger) http.HandlerFunc {
} }
func cleanBody(body string) string { func cleanBody(body string) string {
re := regexp.MustCompile(`(?i)https:\/\/images\.(rapgenius|genius)\.com\/`) if doc, err := goquery.NewDocumentFromReader(strings.NewReader(body)); err == nil {
clean := re.ReplaceAllString(body, "/images/") doc.Find("iframe").Each(func(i int, s *goquery.Selection) {
src, exists := s.Attr("src")
re = regexp.MustCompile(`https?:\/\/[a-z]*.?genius.com`) if exists {
clean = re.ReplaceAllString(clean, "") html := fmt.Sprintf(`<a id="iframed-link" href="%s">Link</a>`, src)
s.ReplaceWithHtml(html)
return clean }
})
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, "")
} }

View File

@ -152,6 +152,12 @@ a {
flex-grow: 1; flex-grow: 1;
} }
.annotation #iframed-link{
font-weight: 500;
background-color: #ffcd38;
padding: 2px 6px;
}
#credits p { #credits p {
font-size: 1.3rem; font-size: 1.3rem;
padding: 0.5rem; padding: 0.5rem;
@ -180,6 +186,10 @@ a {
.annotation { .annotation {
padding: 1rem; padding: 1rem;
border-radius: 4px;
background: #eee;
border: 1px solid #ddd;
color: #222;
} }
.annotation img { .annotation img {
@ -187,6 +197,11 @@ a {
height: auto; height: auto;
} }
.annotation a {
background: none;
font-weight: 500;
}
.annotation ul { .annotation ul {
padding-left: 1em; padding-left: 1em;
} }