Skip to content

Commit

Permalink
feat: add support for embedded content (iframes & gists)
Browse files Browse the repository at this point in the history
Signed-off-by: Sphericalkat <[email protected]>
  • Loading branch information
SphericalKat committed May 29, 2023
1 parent 03b74fd commit 7efffe6
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
4 changes: 4 additions & 0 deletions frontend/src/show.css
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ html, body {
line-height: 1.7777778;
}
}

.gist tbody tr {
border: none !important;
}
34 changes: 34 additions & 0 deletions pkg/converters/embedded_converter.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
4 changes: 3 additions & 1 deletion pkg/converters/paragraph_converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
13 changes: 12 additions & 1 deletion pkg/entities/medium_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,25 @@ type BodyModel struct {
Paragraphs []Paragraph `json:"paragraphs"`
}

type MediaResource struct {
Href string `json:"href"`
IframeSrc string `json:"iframeSrc"`
IframeWidth int64 `json:"iframeWidth"`
IframeHeight int64 `json:"iframeHeight"`
}

type Iframe struct {
MediaResource MediaResource `json:"mediaResource"`
}

type Paragraph struct {
Name string `json:"name"`
Text string `json:"text"`
Type Type `json:"type"`
Href interface{} `json:"href"`
Layout *string `json:"layout"`
Markups []Markup `json:"markups"`
Iframe interface{} `json:"iframe"`
Iframe *Iframe `json:"iframe"`
Metadata *Metadata `json:"metadata"`
}

Expand Down

0 comments on commit 7efffe6

Please sign in to comment.