Skip to content

Commit

Permalink
feat(rss): add custom RSS template (#697)
Browse files Browse the repository at this point in the history
  • Loading branch information
razonyang committed Sep 8, 2022
1 parent 5b6ee76 commit 7b40de4
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 4 deletions.
3 changes: 3 additions & 0 deletions exampleSite/config/_default/params.toml
Original file line number Diff line number Diff line change
Expand Up @@ -253,3 +253,6 @@ viewer = true # Image Viewer
[docs]
[docs.nav]
# expand = true # Expand docs navigations.

[feeds]
# content = true
2 changes: 2 additions & 0 deletions exampleSite/content/docs/configuration/site-params/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ The site parameters are located in `config/_default/params.toml` by default.
| `pinnedPost` | Boolean | `true` | Turn on/off pinned posts.
| `pinnedPostCount` | Integer | `1` | The number of pinned posts.
| `rss` | String/Boolean | `true` | Display the RSS button on the social links. Turn it off by `false`, link it to homepage all the times by setting it to `home`.
| `feeds` | Object | - | RSS feeds
| `feeds.content` | Boolean | `false` | Display the full content if ebabled.
| `actionsPanel` | Object | - |
| `actionsPanel.disabled` | Boolean | `false` | Disable actions panel.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ authors = ["RazonYang"]
| `pinnedPost` | Boolean | `true` | 开启/禁用文章置顶。
| `pinnedPostCount` | Integer | `1` | 置顶的文章数量。
| `rss` | String/Boolean | `true` | 在社交链接中显示 RSS 链接。`false` 为不显示,`home` 则总是链接到主页。
| `feeds` | Object | - | RSS feeds
| `feeds.content` | Boolean | `false` | Display the full content if ebabled.
| `actionsPanel` | Object | - |
| `actionsPanel.disabled` | Boolean | `false` | 禁用 actions panel。

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ authors = ["RazonYang"]
| `pinnedPost` | Boolean | `true` | 開啟/禁用文章置頂。
| `pinnedPostCount` | Integer | `1` | 置頂的文章數量。
| `rss` | String/Boolean | `true` | 在社交鏈接中顯示 RSS 鏈接。`false` 為不顯示,`home` 則總是鏈接到主頁。
| `feeds` | Object | - | RSS feeds
| `feeds.content` | Boolean | `false` | Display the full content if ebabled.
| `actionsPanel` | Object | - |
| `actionsPanel.disabled` | Boolean | `false` | 禁用 actions panel。

Expand Down
3 changes: 1 addition & 2 deletions layouts/_default/rss.xml
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
{{- $res := partial "rss" . }}
{{- replace $res "{year}" now.Year 1 | safeHTML -}}
{{- partial "rss" . }}
2 changes: 1 addition & 1 deletion layouts/partials/footer/copyright.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<div class="copyright mb-2">
{{ replace .Site.Copyright "{year}" now.Year }}
{{- partial "helpers/copyright" . }}
</div>
1 change: 1 addition & 0 deletions layouts/partials/helpers/copyright.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ replace .Site.Copyright "{year}" now.Year }}
46 changes: 45 additions & 1 deletion layouts/partials/rss.html
Original file line number Diff line number Diff line change
@@ -1 +1,45 @@
{{- template "_internal/_default/rss.xml" . }}
{{/* Source: https://github.com/gohugoio/hugo/blob/master/tpl/tplimpl/embedded/templates/_default/rss.xml */}}
{{- $pctx := . -}}
{{- if .IsHome -}}{{ $pctx = .Site }}{{- end -}}
{{- $pages := slice -}}
{{- if or $.IsHome $.IsSection -}}
{{- $pages = $pctx.RegularPages -}}
{{- else -}}
{{- $pages = $pctx.Pages -}}
{{- end -}}
{{- $pages = where $pages "Type" "in" $.Site.Params.mainSections }}
{{- $limit := .Site.Config.Services.RSS.Limit -}}
{{- if ge $limit 1 -}}
{{- $pages = $pages | first $limit -}}
{{- end -}}
{{- printf "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>" | safeHTML }}
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
<title>{{ if eq .Title .Site.Title }}{{ .Site.Title }}{{ else }}{{ with .Title }}{{.}} on {{ end }}{{ .Site.Title }}{{ end }}</title>
<link>{{ .Permalink }}</link>
<description>Recent content {{ if ne .Title .Site.Title }}{{ with .Title }}in {{.}} {{ end }}{{ end }}on {{ .Site.Title }}</description>
<generator>Hugo -- gohugo.io</generator>{{ with .Site.LanguageCode }}
<language>{{.}}</language>{{end}}{{ with .Site.Author.email }}
<managingEditor>{{.}}{{ with $.Site.Author.name }} ({{.}}){{end}}</managingEditor>{{end}}{{ with .Site.Author.email }}
<webMaster>{{.}}{{ with $.Site.Author.name }} ({{.}}){{end}}</webMaster>{{end}}
<copyright>{{- partial "helpers/copyright" . }}</copyright>{{ if not .Date.IsZero }}
<lastBuildDate>{{ .Date.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }}</lastBuildDate>{{ end }}
{{- with .OutputFormats.Get "RSS" -}}
{{ printf "<atom:link href=%q rel=\"self\" type=%q />" .Permalink .MediaType | safeHTML }}
{{- end -}}
{{ range $pages }}
<item>
<title>{{ .Title }}</title>
<link>{{ .Permalink }}</link>
<pubDate>{{ .Date.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }}</pubDate>
{{ with .Site.Author.email }}<author>{{.}}{{ with $.Site.Author.name }} ({{.}}){{end}}</author>{{end}}
<guid>{{ .Permalink }}</guid>
<description>{{- printf "<![CDATA[%s]]>" .Summary | safeHTML }}</description>
{{- if default false $.Site.Params.feeds.content }}
<content:encoded>{{- printf "<![CDATA[%s]]>" .Content | safeHTML }}</content:encoded>
{{- end }}
{{- partial "rss/enclosure" . }}
</item>
{{ end }}
</channel>
</rss>
16 changes: 16 additions & 0 deletions layouts/partials/rss/enclosure.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{{- if .Params.images }}
{{- $img := index .Params.images 0 }}
{{- $url := urls.Parse $img }}
{{- $filename := path.Join "static" $img }}
{{- if and (not $url.Scheme) (fileExists $filename) -}}
{{- $file := readFile $filename }}
{{- $imgConfig := imageConfig $filename }}
<enclosure url="{{ $img | absURL }}" length="{{ len $file }}" type="image/{{ path.Ext $filename }}" />
{{- end -}}
{{- else -}}
{{- $images := .Resources.ByType "image" -}}
{{- $featured := $images.GetMatch "*feature*" -}}
{{- with $featured -}}
<enclosure url="{{ $featured.Permalink }}" length="{{ len $featured.Content }}" type="{{ $featured.MediaType }}" />
{{- end -}}
{{- end -}}

0 comments on commit 7b40de4

Please sign in to comment.