From c3e9abbb34d4a09c3815857392368e4fabf5d0bf Mon Sep 17 00:00:00 2001 From: Robert Kaussow Date: Mon, 24 Aug 2020 09:03:56 +0200 Subject: [PATCH] add template for json feeds --- CHANGELOG.md | 2 +- exampleSite/config.yml | 15 ++++++- exampleSite/content/posts/how-to-use.md | 1 + layouts/_default/list.json.json | 57 +++++++++++++++++++++++++ 4 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 layouts/_default/list.json.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 21ec78a..8b4ac9a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,2 +1,2 @@ - ENHANCEMENT - - switch back to Hugo internal twitter-card template + - add template for Json Feeds diff --git a/exampleSite/config.yml b/exampleSite/config.yml index 071e5ca..08ce4f9 100644 --- a/exampleSite/config.yml +++ b/exampleSite/config.yml @@ -30,7 +30,18 @@ outputFormats: name: "Atom" mediaType: "application/atom+xml" # generated file: . = atom.xml - baseName: "atom" + baseName: "feed" + isPlainText: false + rel: "alternate" + isHTML: false + noUgly: true + permalinkable: false + Json: + # https://www.jsonfeed.org/2020/08/07/json-feed-version.html + name: "Json" + mediaType: "application/json" + # generated file: . = feed.json + baseName: "feed" isPlainText: false rel: "alternate" isHTML: false @@ -41,6 +52,7 @@ outputs: home: - HTML - ATOM + - JSON page: - HTML section: @@ -50,6 +62,7 @@ outputs: term: - HTML - ATOM + - JSON enableRobotsTXT: true diff --git a/exampleSite/content/posts/how-to-use.md b/exampleSite/content/posts/how-to-use.md index d906248..72bb213 100644 --- a/exampleSite/content/posts/how-to-use.md +++ b/exampleSite/content/posts/how-to-use.md @@ -31,6 +31,7 @@ This post works as a documentation for the features of the theme. - Simple sticky posts - Content tags - Multiple authors +- Supports Atom and Json feeds ## Configuration diff --git a/layouts/_default/list.json.json b/layouts/_default/list.json.json new file mode 100644 index 0000000..af7bd50 --- /dev/null +++ b/layouts/_default/list.json.json @@ -0,0 +1,57 @@ +{{- $title := .Site.Title -}} +{{- with .Title -}} + {{- if (not (eq . $title)) -}} + {{- $title = printf `%s on %s` . $title -}} + {{- end -}} +{{- end -}} +{ + "version": "https://jsonfeed.org/version/1.1", + "title": {{ $title | jsonify }}, + {{- with .Site.Params.subtitle }} + "description": {{ . | jsonify }}, + {{- end }} + "home_page_url" : "{{ .Site.BaseURL }}", + {{- with .OutputFormats.Get "json" }} + "feed_url" : "{{ .Permalink }}", + {{- end }} + {{- with .Site.Params.GeekblogAuthor -}} + {{ with index $.Site.Data.authors . }} + "authors" : [ + { + "name" : "{{ .name }}"{{ with .url }}, + "url": "{{ . }}"{{ end }}{{ with .avatar }}, + "avatar": "{{ . }}"{{ end }} + } + ], + {{- end }} + {{- end }} + "items" : [ + {{- $pages := where .RegularPages "Type" "in" .Site.Params.mainSections -}} + {{- if (eq .Kind "home") -}} + {{- $pages = where .Site.RegularPages "Type" "in" .Site.Params.mainSections -}} + {{- end -}} + {{- $len := (len $pages) }} + {{- range $i, $page := $pages }} + { + "title" : {{ .Title | jsonify }}, + "date_published" : {{ (.Date.Format "2006-01-02T15:04:05Z07:00") | jsonify }}, + "date_modified" : {{ (.Lastmod.Format "2006-01-02T15:04:05Z07:00") | jsonify }}, + "id" : "{{ .Permalink }}", + "url" : "{{ .Permalink }}", + {{- with .Params.authors -}} + "authors" : [ + {{- $authors := (sort .) }} + {{- $authors_len := (len $authors) }} + {{- range $i, $element := $authors -}} + {{- $author := index $.Site.Data.authors $element }} + { + "name" : "{{ $author.name }}" + }{{ if ne (add $i 1) $authors_len }},{{ end }} + {{- end }} + ], + {{- end }} + "content_html" : {{ .Content | jsonify }} + }{{ if ne (add $i 1) $len }},{{ end }} + {{- end }} + ] +}