From ac76917b7ba55d9f5575e85abf35fd380379f9a3 Mon Sep 17 00:00:00 2001 From: razonyang Date: Wed, 7 Sep 2022 22:57:58 +0800 Subject: [PATCH 1/4] feat(rss): add custom RSS template --- exampleSite/config/_default/params.toml | 3 ++ layouts/_default/rss.xml | 3 +- layouts/partials/footer/copyright.html | 2 +- layouts/partials/helpers/copyright.html | 1 + layouts/partials/rss.html | 43 ++++++++++++++++++++++++- layouts/partials/rss/enclosure.html | 16 +++++++++ 6 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 layouts/partials/helpers/copyright.html create mode 100644 layouts/partials/rss/enclosure.html diff --git a/exampleSite/config/_default/params.toml b/exampleSite/config/_default/params.toml index 2d2b0abf3..82ca22a19 100644 --- a/exampleSite/config/_default/params.toml +++ b/exampleSite/config/_default/params.toml @@ -250,3 +250,6 @@ viewer = true # Image Viewer [docs] [docs.nav] # expand = true # Expand docs navigations. + +[feeds] + # contentAsDescription = true diff --git a/layouts/_default/rss.xml b/layouts/_default/rss.xml index 8f4560ee8..08b4bd9da 100644 --- a/layouts/_default/rss.xml +++ b/layouts/_default/rss.xml @@ -1,2 +1 @@ -{{- $res := partial "rss" . }} -{{- replace $res "{year}" now.Year 1 | safeHTML -}} +{{- partial "rss" . }} \ No newline at end of file diff --git a/layouts/partials/footer/copyright.html b/layouts/partials/footer/copyright.html index 94e155cb5..990c5f487 100644 --- a/layouts/partials/footer/copyright.html +++ b/layouts/partials/footer/copyright.html @@ -1,3 +1,3 @@ diff --git a/layouts/partials/helpers/copyright.html b/layouts/partials/helpers/copyright.html new file mode 100644 index 000000000..7937a4c10 --- /dev/null +++ b/layouts/partials/helpers/copyright.html @@ -0,0 +1 @@ +{{ replace .Site.Copyright "{year}" now.Year }} diff --git a/layouts/partials/rss.html b/layouts/partials/rss.html index 5a11fe503..79e94235f 100644 --- a/layouts/partials/rss.html +++ b/layouts/partials/rss.html @@ -1 +1,42 @@ -{{- 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 "" | safeHTML }} + + + {{ if eq .Title .Site.Title }}{{ .Site.Title }}{{ else }}{{ with .Title }}{{.}} on {{ end }}{{ .Site.Title }}{{ end }} + {{ .Permalink }} + Recent content {{ if ne .Title .Site.Title }}{{ with .Title }}in {{.}} {{ end }}{{ end }}on {{ .Site.Title }} + Hugo -- gohugo.io{{ with .Site.LanguageCode }} + {{.}}{{end}}{{ with .Site.Author.email }} + {{.}}{{ with $.Site.Author.name }} ({{.}}){{end}}{{end}}{{ with .Site.Author.email }} + {{.}}{{ with $.Site.Author.name }} ({{.}}){{end}}{{end}} + {{- partial "helpers/copyright" . }}{{ if not .Date.IsZero }} + {{ .Date.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }}{{ end }} + {{- with .OutputFormats.Get "RSS" -}} + {{ printf "" .Permalink .MediaType | safeHTML }} + {{- end -}} + {{ range $pages }} + + {{ .Title }} + {{ .Permalink }} + {{ .Date.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }} + {{ with .Site.Author.email }}{{.}}{{ with $.Site.Author.name }} ({{.}}){{end}}{{end}} + {{ .Permalink }} + {{ if default false $.Site.Params.feeds.contentAsDescription }}{{ .Content | html }}{{ else }}{{ .Summary | html }}{{ end }} + {{- partial "rss/enclosure" . }} + + {{ end }} + + diff --git a/layouts/partials/rss/enclosure.html b/layouts/partials/rss/enclosure.html new file mode 100644 index 000000000..c7df7332c --- /dev/null +++ b/layouts/partials/rss/enclosure.html @@ -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 }} + + {{- end -}} +{{- else -}} + {{- $images := .Resources.ByType "image" -}} + {{- $featured := $images.GetMatch "*feature*" -}} + {{- with $featured -}} + + {{- end -}} +{{- end -}} From f2e20f0fab361964ba674f437fc6d490ae05009b Mon Sep 17 00:00:00 2001 From: razonyang Date: Thu, 8 Sep 2022 01:34:39 +0800 Subject: [PATCH 2/4] chore(rss): beautify the description format via CDATA --- layouts/partials/rss.html | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/layouts/partials/rss.html b/layouts/partials/rss.html index 79e94235f..2a767943e 100644 --- a/layouts/partials/rss.html +++ b/layouts/partials/rss.html @@ -34,7 +34,13 @@ {{ .Date.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }} {{ with .Site.Author.email }}{{.}}{{ with $.Site.Author.name }} ({{.}}){{end}}{{end}} {{ .Permalink }} - {{ if default false $.Site.Params.feeds.contentAsDescription }}{{ .Content | html }}{{ else }}{{ .Summary | html }}{{ end }} + + {{- if default false $.Site.Params.feeds.contentAsDescription }} + {{- printf "" .Content | safeHTML }} + {{- else }} + {{- printf "" .Summary | safeHTML }} + {{- end }} + {{- partial "rss/enclosure" . }} {{ end }} From 4583d251330d125efd786ac175bc8dd88507f058 Mon Sep 17 00:00:00 2001 From: razonyang Date: Thu, 8 Sep 2022 19:39:50 +0800 Subject: [PATCH 3/4] chore(rss): use the extended tag `content:encoded` for content --- exampleSite/config/_default/params.toml | 2 +- layouts/partials/rss.html | 13 +++++-------- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/exampleSite/config/_default/params.toml b/exampleSite/config/_default/params.toml index 82ca22a19..9072ba62c 100644 --- a/exampleSite/config/_default/params.toml +++ b/exampleSite/config/_default/params.toml @@ -252,4 +252,4 @@ viewer = true # Image Viewer # expand = true # Expand docs navigations. [feeds] - # contentAsDescription = true + content = true diff --git a/layouts/partials/rss.html b/layouts/partials/rss.html index 2a767943e..0890f1d4d 100644 --- a/layouts/partials/rss.html +++ b/layouts/partials/rss.html @@ -13,7 +13,7 @@ {{- $pages = $pages | first $limit -}} {{- end -}} {{- printf "" | safeHTML }} - + {{ if eq .Title .Site.Title }}{{ .Site.Title }}{{ else }}{{ with .Title }}{{.}} on {{ end }}{{ .Site.Title }}{{ end }} {{ .Permalink }} @@ -34,13 +34,10 @@ {{ .Date.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }} {{ with .Site.Author.email }}{{.}}{{ with $.Site.Author.name }} ({{.}}){{end}}{{end}} {{ .Permalink }} - - {{- if default false $.Site.Params.feeds.contentAsDescription }} - {{- printf "" .Content | safeHTML }} - {{- else }} - {{- printf "" .Summary | safeHTML }} - {{- end }} - + {{- printf "" .Summary | safeHTML }} + {{- if default false $.Site.Params.feeds.content }} + {{- printf "" .Content | safeHTML }} + {{- end }} {{- partial "rss/enclosure" . }} {{ end }} From 0e5931b62c7dbd94414396f30e14bd7ca6d7be26 Mon Sep 17 00:00:00 2001 From: razonyang Date: Thu, 8 Sep 2022 19:53:01 +0800 Subject: [PATCH 4/4] docs: document the feeds parameter --- exampleSite/config/_default/params.toml | 2 +- exampleSite/content/docs/configuration/site-params/index.md | 2 ++ .../content/docs/configuration/site-params/index.zh-hans.md | 2 ++ .../content/docs/configuration/site-params/index.zh-hant.md | 2 ++ layouts/_default/rss.xml | 2 +- 5 files changed, 8 insertions(+), 2 deletions(-) diff --git a/exampleSite/config/_default/params.toml b/exampleSite/config/_default/params.toml index 9072ba62c..48e680f3d 100644 --- a/exampleSite/config/_default/params.toml +++ b/exampleSite/config/_default/params.toml @@ -252,4 +252,4 @@ viewer = true # Image Viewer # expand = true # Expand docs navigations. [feeds] - content = true + # content = true diff --git a/exampleSite/content/docs/configuration/site-params/index.md b/exampleSite/content/docs/configuration/site-params/index.md index 25648a471..289d035b2 100644 --- a/exampleSite/content/docs/configuration/site-params/index.md +++ b/exampleSite/content/docs/configuration/site-params/index.md @@ -149,6 +149,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. diff --git a/exampleSite/content/docs/configuration/site-params/index.zh-hans.md b/exampleSite/content/docs/configuration/site-params/index.zh-hans.md index b73e641a4..cda19969e 100644 --- a/exampleSite/content/docs/configuration/site-params/index.zh-hans.md +++ b/exampleSite/content/docs/configuration/site-params/index.zh-hans.md @@ -152,6 +152,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。 diff --git a/exampleSite/content/docs/configuration/site-params/index.zh-hant.md b/exampleSite/content/docs/configuration/site-params/index.zh-hant.md index 195467799..0d5843751 100644 --- a/exampleSite/content/docs/configuration/site-params/index.zh-hant.md +++ b/exampleSite/content/docs/configuration/site-params/index.zh-hant.md @@ -152,6 +152,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。 diff --git a/layouts/_default/rss.xml b/layouts/_default/rss.xml index 08b4bd9da..6d6d98368 100644 --- a/layouts/_default/rss.xml +++ b/layouts/_default/rss.xml @@ -1 +1 @@ -{{- partial "rss" . }} \ No newline at end of file +{{- partial "rss" . }}