Skip to content

Commit

Permalink
Quick Fix, Issue occured due to git pull
Browse files Browse the repository at this point in the history
  • Loading branch information
Anonymous Indian committed Aug 16, 2021
1 parent 40a6cef commit 31fc417
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 3 deletions.
66 changes: 66 additions & 0 deletions content.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package telegraph

import (
"bytes"
"errors"
"io"
"strings"

"golang.org/x/net/html"
)

func ContentFormat(data interface{}) (n []Node, err error) {
var dst *html.Node

switch src := data.(type) {
case string:
dst, err = html.Parse(strings.NewReader(src))
case []byte:
dst, err = html.Parse(bytes.NewReader(src))
case io.Reader:
dst, err = html.Parse(src)
default:
return nil, errors.New("INVALID DATA TYPE")
}

if err != nil {
return nil, err
}

n = append(n, domToNode(dst.FirstChild))

return n, nil
}

func domToNode(domNode *html.Node) interface{} {
if domNode.Type == html.TextNode {
return domNode.Data
}

if domNode.Type != html.ElementNode {
return nil
}

nodeElement := new(NodeElement)

switch strings.ToLower(domNode.Data) {
case "a", "aside", "b", "blockquote", "br", "code", "em", "figcaption", "figure", "h3", "h4", "hr", "i",
"iframe", "img", "li", "ol", "p", "pre", "s", "strong", "u", "ul", "video":
nodeElement.Tag = domNode.Data

for i := range domNode.Attr {
switch strings.ToLower(domNode.Attr[i].Key) {
case "href", "src":
nodeElement.Attrs = map[string]string{domNode.Attr[i].Key: domNode.Attr[i].Val}
default:
continue
}
}
}

for child := domNode.FirstChild; child != nil; child = child.NextSibling {
nodeElement.Children = append(nodeElement.Children, domToNode(child))
}

return nodeElement
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/anonyindian/telegraph-go

go 1.16

require golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d h1:LO7XpTYMwTqxjLcGWPijK3vRXg1aWdlNOVOHRq45d7c=
golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
20 changes: 18 additions & 2 deletions methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,15 @@ func CreatePage(accessToken string, title string, content string, opts *PageOpts
)
u.Add("access_token", accessToken)
u.Add("title", title)
u.Add("content", content)
cNode, err := ContentFormat(content)
if err != nil {
return nil, err
}
cNodeB, err := json.Marshal(cNode)
if err != nil {
return nil, err
}
u.Add("content", string(cNodeB))

if opts != nil {
u.Add("author_name", opts.AuthorName)
Expand Down Expand Up @@ -142,7 +150,15 @@ func EditPage(accessToken string, path string, title string, content string, opt
u.Add("access_token", accessToken)
u.Add("path", path)
u.Add("title", title)
u.Add("content", content)
cNode, err := ContentFormat(content)
if err != nil {
return nil, err
}
cNodeB, err := json.Marshal(cNode)
if err != nil {
return nil, err
}
u.Add("content", string(cNodeB))

if opts != nil {
u.Add("author_name", opts.AuthorName)
Expand Down
20 changes: 19 additions & 1 deletion types.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type Page struct {
// Optional. Image URL of the page.
ImageUrl string `json:"image_url"`
// Optional. Content of the page.
Content string `json:"content"`
Content []Node `json:"content"`
// Number of page views for the page.
Views int64 `json:"views"`
// Optional. Only returned if access_token passed. True, if the target Telegraph account can edit the page.
Expand Down Expand Up @@ -101,3 +101,21 @@ type PageViewsOpts struct {
// If passed, the number of page views for the requested hour will be returned.
Hour int64 `json:"hour"`
}

// Node is abstract object represents a DOM Node. It can be a String which represents a DOM text node or a
// NodeElement object.
type Node interface{}

// NodeElement represents a DOM element node.
type NodeElement struct {
// Name of the DOM element. Available tags: a, aside, b, blockquote, br, code, em, figcaption, figure,
// h3, h4, hr, i, iframe, img, li, ol, p, pre, s, strong, u, ul, video.
Tag string `json:"tag"`

// Attributes of the DOM element. Key of object represents name of attribute, value represents value
// of attribute. Available attributes: href, src.
Attrs map[string]string `json:"attrs,omitempty"`

// List of child nodes for the DOM element.
Children []Node `json:"children,omitempty"`
}

0 comments on commit 31fc417

Please sign in to comment.