feat: add support for embedded content (iframes & gists)

Signed-off-by: Sphericalkat <amolele@gmail.com>
This commit is contained in:
sphericalkat committed 2023-05-29 23:52:24 +05:30
1 parent 03b74fd90a
commit 7efffe6acd
4 files changed
+53 -2

No files matched your search

+34
View File
@@ -0,0 +1,34 @@
package converters
import (
"fmt"
"net/url"
"strings"
"github.com/medium.rip/pkg/entities"
log "github.com/sirupsen/logrus"
)
func ConvertEmbedded(media entities.MediaResource) string {
if media.IframeSrc == "" {
return customEmbed(media)
} else {
return fmt.Sprintf("<iframe src=\"%s\" width=\"%d\" height=\"%d\" frameborder=\"0\" allowfullscreen=\"true\" ></iframe>", media.IframeSrc, media.IframeWidth, media.IframeHeight)
}
}
func customEmbed(media entities.MediaResource) string {
if strings.HasPrefix(media.Href, "https://gist.github.com") {
return fmt.Sprintf("<script src=\"%s.js\"></script>", media.Href)
} else {
url, err := url.Parse(media.Href)
var caption string
if err != nil {
log.Warnf("Error parsing url %s", media.Href)
caption = media.Href
} else {
caption = fmt.Sprintf("Embedded content at %s", url.Host)
}
return fmt.Sprintf("<figure><a href=\"%s\">%s</a></figure>", media.Href, caption)
}
}
+3 -1
View File
@@ -85,7 +85,9 @@ func ConvertParagraphs(paragraphs []entities.Paragraph) string {
} else {
ps.WriteString(fmt.Sprintf("<h4>%s</h4>", children))
}
// TODO: handle IFRAME
case "IFRAME":
child := ConvertEmbedded(p.Iframe.MediaResource)
ps.WriteString(child)
case "IMG":
ps.WriteString(convertImg(p))
case "OLI":