🔥 Remove youtube/vimeo/twitter cases in ContentFormat

This commit is contained in:
Maxim Lebedev 2017-06-13 08:04:39 +05:00
parent 4d524abf98
commit 9efccaa529
No known key found for this signature in database
GPG Key ID: F8978F46FF0FFA4F
1 changed files with 35 additions and 56 deletions

View File

@ -3,43 +3,44 @@ package telegraph
import (
"bytes"
"errors"
"fmt"
"strings"
html "golang.org/x/net/html"
)
var availableTags = map[string]bool{
"a": true,
"aside": true,
"b": true,
"blockquote": true,
"br": true,
"code": true,
"em": true,
"figcaption": true,
"figure": true,
"h3": true,
"h4": true,
"hr": true,
"i": true,
"iframe": true,
"img": true,
"li": true,
"ol": true,
"p": true,
"pre": true,
"s": true,
"strong": true,
"u": true,
"ul": true,
"video": true,
}
var (
availableTags = map[string]bool{
"a": true,
"aside": true,
"b": true,
"blockquote": true,
"br": true,
"code": true,
"em": true,
"figcaption": true,
"figure": true,
"h3": true,
"h4": true,
"hr": true,
"i": true,
"iframe": true,
"img": true,
"li": true,
"ol": true,
"p": true,
"pre": true,
"s": true,
"strong": true,
"u": true,
"ul": true,
"video": true,
}
var availableAttributes = map[string]bool{
"href": true,
"src": true,
}
availableAttributes = map[string]bool{
"href": true,
"src": true,
}
)
// ContentFormat transforms data to a DOM-based format to represent the
// content of the page.
@ -80,20 +81,10 @@ func domToNode(domNode *html.Node) interface{} {
var nodeElement NodeElement
if _, ok := availableTags[strings.ToLower(domNode.Data)]; ok {
nodeElement.Tag = strings.ToLower(domNode.Data)
for i := range domNode.Attr {
attr := domNode.Attr[i]
for _, attr := range domNode.Attr {
if _, ok := availableAttributes[strings.ToLower(attr.Key)]; ok {
switch {
case attr.Key == "src" && strings.Contains(attr.Val, "vimeo.com"):
nodeElement.Attrs = parseEmbed("vimeo", attr.Val)
case attr.Key == "src" && (strings.Contains(attr.Val, "youtube.com") || strings.Contains(attr.Val, "youtu.be")):
nodeElement.Attrs = parseEmbed("youtube", attr.Val)
case attr.Key == "src" && strings.Contains(attr.Val, "twitter.com"):
nodeElement.Attrs = parseEmbed("twitter", attr.Val)
default:
nodeElement.Attrs = map[string]string{
strings.ToLower(attr.Key): strings.ToLower(attr.Val),
}
nodeElement.Attrs = map[string]string{
strings.ToLower(attr.Key): strings.ToLower(attr.Val),
}
}
}
@ -105,15 +96,3 @@ func domToNode(domNode *html.Node) interface{} {
return nodeElement
}
func parseEmbed(service, url string) map[string]string {
return map[string]string{
"src": fmt.Sprint("/embed/", service, "?url=", url),
"width": "640",
"height": "360",
"frameborder": "0",
"allowtransparency": "true",
"allowfullscreen": "true",
"scrolling": "no",
}
}