Fix up RSS/Atom feeds
This commit is contained in:
parent
e67df627ce
commit
77bf2b9dd3
|
@ -1,15 +1,26 @@
|
||||||
package postctl
|
package postctl
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gorilla/feeds"
|
"github.com/gorilla/feeds"
|
||||||
"github.com/kirsle/blog/src/responses"
|
|
||||||
"github.com/kirsle/blog/models/posts"
|
"github.com/kirsle/blog/models/posts"
|
||||||
"github.com/kirsle/blog/models/settings"
|
"github.com/kirsle/blog/models/settings"
|
||||||
"github.com/kirsle/blog/models/users"
|
"github.com/kirsle/blog/models/users"
|
||||||
|
"github.com/kirsle/blog/src/markdown"
|
||||||
|
"github.com/kirsle/blog/src/responses"
|
||||||
|
"github.com/kirsle/blog/src/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Feed configuration. TODO make configurable.
|
||||||
|
var (
|
||||||
|
FeedPostsPerPage = 20
|
||||||
|
|
||||||
|
reRelativeLink = regexp.MustCompile(` (src|href|poster)=(['"])/([^'"]+)['"]`)
|
||||||
)
|
)
|
||||||
|
|
||||||
func feedHandler(w http.ResponseWriter, r *http.Request) {
|
func feedHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
@ -34,19 +45,37 @@ func feedHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
feed.Items = []*feeds.Item{}
|
feed.Items = []*feeds.Item{}
|
||||||
for i, p := range RecentPosts(r, "", "") {
|
for i, p := range RecentPosts(r, "", "") {
|
||||||
post, _ := posts.Load(p.ID)
|
post, _ := posts.Load(p.ID)
|
||||||
var suffix string
|
|
||||||
if strings.Contains(post.Body, "<snip>") {
|
// Render the post to HTML.
|
||||||
post.Body = strings.Split(post.Body, "<snip>")[0]
|
var rendered string
|
||||||
suffix = "..."
|
if post.ContentType == string(types.MARKDOWN) {
|
||||||
|
rendered = markdown.RenderTrustedMarkdown(post.Body)
|
||||||
|
} else {
|
||||||
|
rendered = post.Body
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make relative links absolute.
|
||||||
|
matches := reRelativeLink.FindAllStringSubmatch(rendered, -1)
|
||||||
|
for _, match := range matches {
|
||||||
|
var (
|
||||||
|
attr = match[1]
|
||||||
|
quote = match[2]
|
||||||
|
uri = match[3]
|
||||||
|
absURI = config.Site.URL + "/" + uri
|
||||||
|
new = fmt.Sprintf(" %s%s%s%s",
|
||||||
|
attr, quote, absURI, quote,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
rendered = strings.Replace(rendered, match[0], new, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
feed.Items = append(feed.Items, &feeds.Item{
|
feed.Items = append(feed.Items, &feeds.Item{
|
||||||
Title: p.Title,
|
Title: p.Title,
|
||||||
Link: &feeds.Link{Href: config.Site.URL + p.Fragment},
|
Link: &feeds.Link{Href: config.Site.URL + p.Fragment},
|
||||||
Description: post.Body + suffix,
|
Description: rendered,
|
||||||
Created: p.Created,
|
Created: p.Created,
|
||||||
})
|
})
|
||||||
if i == 9 { // 10 -1
|
if i == FeedPostsPerPage-1 {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -58,7 +87,7 @@ func feedHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Write([]byte(atom))
|
w.Write([]byte(atom))
|
||||||
} else {
|
} else {
|
||||||
rss, _ := feed.ToRss()
|
rss, _ := feed.ToRss()
|
||||||
w.Header().Set("Content-Type", "application/rss+xml")
|
w.Header().Set("Content-Type", "application/rss+xml; encoding=utf-8")
|
||||||
w.Write([]byte(rss))
|
w.Write([]byte(rss))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var ageGateSuffixes = []string{
|
var ageGateSuffixes = []string{
|
||||||
|
"/blog.rss", // Allow public access to RSS and Atom feeds.
|
||||||
|
"/blog.atom",
|
||||||
".js",
|
".js",
|
||||||
".css",
|
".css",
|
||||||
".txt",
|
".txt",
|
||||||
|
@ -39,8 +41,8 @@ func AgeGate(verifyHandler func(http.ResponseWriter, *http.Request)) negroni.Han
|
||||||
}
|
}
|
||||||
|
|
||||||
// Allow static files and things through.
|
// Allow static files and things through.
|
||||||
for _, prefix := range ageGateSuffixes {
|
for _, suffix := range ageGateSuffixes {
|
||||||
if strings.HasSuffix(path, prefix) {
|
if strings.HasSuffix(path, suffix) {
|
||||||
next(w, r)
|
next(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user