Documentation
¶
Overview ¶
Package instant enables creation and publishing of Facebook Instant Articles.
Full Facebook instant articles documentation can be found here https://developers.facebook.com/docs/instant-articles
Struct instant.Article represents Facebook instant article as described on https://developers.facebook.com/docs/instant-articles/guides/articlecreate Use instant.Article and then use helper functions like SetTitle(), SetCoverImage() etc. to easily create instant article without setting struct properties directly. Elemenets are nested in inner struct, so using setter functions is much easier. Custom marshaler will generate Facebook instant article valid html.
Example:
package main
import (
"encoding/xml"
"time"
"github.com/mileusna/facebook-instant-articles"
)
func main() {
a := instant.Article{}
// required
a.SetTitle("My article title")
a.SetCanonical("http://mysite/url-to-this-article")
a.SetPublish(time.Now())
a.SetContent("<p>My content</p><p>Other paragraph</p>")
// optional
a.SetSubtitle("My article subtitle")
a.SetKick("Exclusive")
a.SetFooter("", "(C)2016 MyComp")
a.AddAuthor("Michael", "http://facebook.com/mmichael", "Guest writter")
html, err := xml.Marshal(a)
if err != nil {
return
}
// html contains Facebook instant article html as []byte
}
Struct instant.Feed represents Facebook instant article RSS feed as described on https://developers.facebook.com/docs/instant-articles/publishing/setup-rss-feed Use instant.NewFeed() to create initial struct with all headers set up and use helper functions like AddArticles to add instant.Article to feed. Custom marshaler provides valid Facebook instant article RSS feed.
Example:
package main
import (
"encoding/xml"
"net/http"
"time"
"github.com/mileusna/facebook-instant-articles"
)
func main() {
http.HandleFunc("/instant-articles/", handleInstantArticles)
http.ListenAndServe(":8080", nil)
}
func handleInstantArticles(w http.ResponseWriter, r *http.Request) {
var f instant.Feed
// I don't believe Facebook cares about feed title, link and description, but you can set them
f.SetTitle("Title feed")
f.SetLink("http://www.mysite.com")
f.SetDescription("Feed description")
var a instant.Article
a.SetTitle("My article title")
a.SetCanonical("http://mysite/url-to-this-article")
a.SetPublish(time.Now())
a.SetContent("<p>Pragraph 1</p><p>paragraph 2</p>")
f.AddArticle(a) // add article, GUID will be hash of URL
a = instant.Article{}
a.SetTitle("My other title")
a.SetCanonical("http://mysite/url-to-this-article-number-two")
a.SetPublish(time.Now())
a.SetContent("<p>Pragraph 1</p><p>paragraph 2</p>")
f.AddArticleWithGUID(a, "12333") // add article and use for example mysql id as GUID
feed, err := xml.Marshal(f)
if err != nil {
return
}
w.Write(feed)
}
Index ¶
- type Article
- func (a *Article) AddAd(position int, src string, width, height int, style, code string)
- func (a *Article) AddAuthor(name, link, description string)
- func (a *Article) AddFigure(f Figure)
- func (a *Article) AddParagraph(html string)
- func (a Article) HTML() ([]byte, error)
- func (a *Article) InsertAd(position int, src string, width, height int, style, code string)
- func (a *Article) InsertFigure(position int, f Figure)
- func (a Article) MarshalXML(e *xml.Encoder, start xml.StartElement) error
- func (a *Article) SetAutomaticAd(src string, width, height int, style, code string)
- func (a *Article) SetCanonical(url string)
- func (a *Article) SetContent(html string)
- func (a *Article) SetCoverImage(url, caption string)
- func (a *Article) SetCoverVideo(url, videoType, caption string)
- func (a *Article) SetFooter(credits, copyright string)
- func (a *Article) SetKick(kick string)
- func (a *Article) SetLang(lang string)
- func (a *Article) SetModified(date time.Time)
- func (a *Article) SetPublish(date time.Time)
- func (a *Article) SetStyle(style string)
- func (a *Article) SetSubtitle(subtitle string)
- func (a *Article) SetTitle(title string)
- func (a *Article) SetTrackerCode(code string)
- func (a *Article) SetTrackerURL(url string)
- type Content
- type ContentTag
- type Feed
- func (f *Feed) AddArticle(a Article) error
- func (f *Feed) AddArticleWithGUID(a Article, guid string) error
- func (f Feed) MarshalXML(e *xml.Encoder, start xml.StartElement) error
- func (f Feed) RSS() ([]byte, error)
- func (f *Feed) SetDescription(s string)
- func (f *Feed) SetLanguage(l string)
- func (f *Feed) SetLink(url string)
- func (f *Feed) SetTitle(s string)
- type Figure
- type IFrame
- type Img
- type Meta
- type P
- type Time
- type Video
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Article ¶
type Article struct {
Prefix string `xml:"prefix,attr"`
Lang string `xml:"lang,attr"`
Head head `xml:"head"`
Body body `xml:"body"`
}
Article struct
func (*Article) AddAuthor ¶
AddAuthor adds article author. Link and description are optional (use "")
func (*Article) AddParagraph ¶
AddParagraph to Instant Article.
func (*Article) InsertFigure ¶
InsertFigure in content on specified position within existing elements (paragraphs)
func (Article) MarshalXML ¶
MarshalXML for xml.Marshaler interface, marshal Article struct to Facebook Instant Article format.
func (*Article) SetAutomaticAd ¶
SetAutomaticAd in header that Facebook will place automatically in article
func (*Article) SetCanonical ¶
SetCanonical sets public web url for this article. Setting canonical link is required.
func (*Article) SetContent ¶
SetContent of instant article. All html should be in <p> ... </p> elements. If no <p> elements found, entire HTML param will be added as one paragraph. Images and videos can be added later using InsertFigure() but they can also be contained in HTML param if formated properly. See https://developers.facebook.com/docs/instant-articles/reference for more info.
func (*Article) SetCoverImage ¶
SetCoverImage of instant article. Caption can be empty string.
func (*Article) SetCoverVideo ¶
SetCoverVideo of instant article. videoType in format video/mp4 See the list of supported formats here https://www.facebook.com/help/218673814818907 Caption can be empty string.
func (*Article) SetModified ¶
SetModified sets modified date if article has been modified.
func (*Article) SetPublish ¶
SetPublish sets published date.
func (*Article) SetStyle ¶
SetStyle set user defined style for this article. See https://developers.facebook.com/docs/instant-articles/guides/design#style for more info.
func (*Article) SetSubtitle ¶
SetSubtitle sets article subtitle.
func (*Article) SetTrackerCode ¶
SetTrackerCode for 3rd party analytics (Google Analytics for example) Visit https://developers.facebook.com/docs/instant-articles/reference/analytics for more info.
func (*Article) SetTrackerURL ¶
SetTrackerURL for 3rd party analytics that can be included with url. Visit https://developers.facebook.com/docs/instant-articles/reference/analytics for more info.
type Content ¶
type Content []ContentTag
Content of Facebook Instant Article, currently support <p> and <figure>
func (Content) MarshalXML ¶
MarshalXML for elements in body article content
type ContentTag ¶
type ContentTag interface {
StartElement() xml.StartElement
}
ContentTag interface for supported tag elements in Facebook Article Content
type Feed ¶
type Feed struct {
Version string `xml:"version,attr"`
Content string `xml:"xmlns:content,attr"`
Channel channel `xml:"channel"`
}
Feed struct represents Facebook Instant Articles RSS feed. See https://developers.facebook.com/docs/instant-articles/publishing/setup-rss-feed for more info.
func (*Feed) AddArticle ¶
AddArticle to feed. Md5 checksum of URL will be used as GUID.
func (*Feed) AddArticleWithGUID ¶
AddArticleWithGUID to feed.
func (Feed) MarshalXML ¶
MarshalXML for xml.Marshaler interface
func (*Feed) SetDescription ¶
SetDescription set feed description. Optional.
func (*Feed) SetLanguage ¶
SetLanguage sets feed language, default is set to en-us.
type Figure ¶
type Figure struct {
Img *Img `xml:"img,omitempty"`
IFrame *IFrame `xml:"iframe,omitempty"`
Video *Video `xml:"video,omitempty"`
Figcaption string `xml:"figcaption,omitempty"`
Class string `xml:"class,attr,omitempty"`
}
Figure HTML <figure>
func (Figure) StartElement ¶
func (f Figure) StartElement() xml.StartElement
StartElement for ContentElement interface
type IFrame ¶
type IFrame struct {
Src string `xml:"src,attr,omitempty"`
Height string `xml:"height,attr,omitempty"`
Width string `xml:"width,attr,omitempty"`
Style string `xml:"style,attr,omitempty"`
Hidden string `xml:"hidden,attr,omitempty"`
Text string `xml:",innerxml"`
}
IFrame selement
type Meta ¶
type Meta struct {
Charset string `xml:"charset,attr,omitempty"`
Property string `xml:"property,attr,omitempty"`
Content string `xml:"content,attr,omitempty"`
}
Meta element
type P ¶
type P struct {
Text string `xml:",innerxml"`
}
P HTML <p>
func (P) StartElement ¶
func (p P) StartElement() xml.StartElement
StartElement for ContentElement interface