Skip to content

Commit 31fc417

Browse files
author
Anonymous Indian
committed
Quick Fix, Issue occured due to git pull
1 parent 40a6cef commit 31fc417

File tree

5 files changed

+112
-3
lines changed

5 files changed

+112
-3
lines changed

content.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package telegraph
2+
3+
import (
4+
"bytes"
5+
"errors"
6+
"io"
7+
"strings"
8+
9+
"golang.org/x/net/html"
10+
)
11+
12+
func ContentFormat(data interface{}) (n []Node, err error) {
13+
var dst *html.Node
14+
15+
switch src := data.(type) {
16+
case string:
17+
dst, err = html.Parse(strings.NewReader(src))
18+
case []byte:
19+
dst, err = html.Parse(bytes.NewReader(src))
20+
case io.Reader:
21+
dst, err = html.Parse(src)
22+
default:
23+
return nil, errors.New("INVALID DATA TYPE")
24+
}
25+
26+
if err != nil {
27+
return nil, err
28+
}
29+
30+
n = append(n, domToNode(dst.FirstChild))
31+
32+
return n, nil
33+
}
34+
35+
func domToNode(domNode *html.Node) interface{} {
36+
if domNode.Type == html.TextNode {
37+
return domNode.Data
38+
}
39+
40+
if domNode.Type != html.ElementNode {
41+
return nil
42+
}
43+
44+
nodeElement := new(NodeElement)
45+
46+
switch strings.ToLower(domNode.Data) {
47+
case "a", "aside", "b", "blockquote", "br", "code", "em", "figcaption", "figure", "h3", "h4", "hr", "i",
48+
"iframe", "img", "li", "ol", "p", "pre", "s", "strong", "u", "ul", "video":
49+
nodeElement.Tag = domNode.Data
50+
51+
for i := range domNode.Attr {
52+
switch strings.ToLower(domNode.Attr[i].Key) {
53+
case "href", "src":
54+
nodeElement.Attrs = map[string]string{domNode.Attr[i].Key: domNode.Attr[i].Val}
55+
default:
56+
continue
57+
}
58+
}
59+
}
60+
61+
for child := domNode.FirstChild; child != nil; child = child.NextSibling {
62+
nodeElement.Children = append(nodeElement.Children, domToNode(child))
63+
}
64+
65+
return nodeElement
66+
}

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module github.com/anonyindian/telegraph-go
22

33
go 1.16
4+
5+
require golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d

go.sum

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d h1:LO7XpTYMwTqxjLcGWPijK3vRXg1aWdlNOVOHRq45d7c=
2+
golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
3+
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
4+
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
5+
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
6+
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
7+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

methods.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,15 @@ func CreatePage(accessToken string, title string, content string, opts *PageOpts
111111
)
112112
u.Add("access_token", accessToken)
113113
u.Add("title", title)
114-
u.Add("content", content)
114+
cNode, err := ContentFormat(content)
115+
if err != nil {
116+
return nil, err
117+
}
118+
cNodeB, err := json.Marshal(cNode)
119+
if err != nil {
120+
return nil, err
121+
}
122+
u.Add("content", string(cNodeB))
115123

116124
if opts != nil {
117125
u.Add("author_name", opts.AuthorName)
@@ -142,7 +150,15 @@ func EditPage(accessToken string, path string, title string, content string, opt
142150
u.Add("access_token", accessToken)
143151
u.Add("path", path)
144152
u.Add("title", title)
145-
u.Add("content", content)
153+
cNode, err := ContentFormat(content)
154+
if err != nil {
155+
return nil, err
156+
}
157+
cNodeB, err := json.Marshal(cNode)
158+
if err != nil {
159+
return nil, err
160+
}
161+
u.Add("content", string(cNodeB))
146162

147163
if opts != nil {
148164
u.Add("author_name", opts.AuthorName)

types.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ type Page struct {
5151
// Optional. Image URL of the page.
5252
ImageUrl string `json:"image_url"`
5353
// Optional. Content of the page.
54-
Content string `json:"content"`
54+
Content []Node `json:"content"`
5555
// Number of page views for the page.
5656
Views int64 `json:"views"`
5757
// Optional. Only returned if access_token passed. True, if the target Telegraph account can edit the page.
@@ -101,3 +101,21 @@ type PageViewsOpts struct {
101101
// If passed, the number of page views for the requested hour will be returned.
102102
Hour int64 `json:"hour"`
103103
}
104+
105+
// Node is abstract object represents a DOM Node. It can be a String which represents a DOM text node or a
106+
// NodeElement object.
107+
type Node interface{}
108+
109+
// NodeElement represents a DOM element node.
110+
type NodeElement struct {
111+
// Name of the DOM element. Available tags: a, aside, b, blockquote, br, code, em, figcaption, figure,
112+
// h3, h4, hr, i, iframe, img, li, ol, p, pre, s, strong, u, ul, video.
113+
Tag string `json:"tag"`
114+
115+
// Attributes of the DOM element. Key of object represents name of attribute, value represents value
116+
// of attribute. Available attributes: href, src.
117+
Attrs map[string]string `json:"attrs,omitempty"`
118+
119+
// List of child nodes for the DOM element.
120+
Children []Node `json:"children,omitempty"`
121+
}

0 commit comments

Comments
 (0)