From 58f744a22380c42c25ee4059bb4b2bcfd255fd53 Mon Sep 17 00:00:00 2001 From: Serghei Iakovlev Date: Tue, 17 May 2022 21:02:53 +0200 Subject: Add json feed --- exampleSite/config.toml | 13 +++++++++++-- layouts/_default/home.atom.xml | 3 ++- layouts/_default/home.feed.xml | 11 ++++++----- layouts/_default/home.json.json | 26 ++++++++++++++++++++++++++ theme.toml | 25 ++++++++++++------------- 5 files changed, 57 insertions(+), 21 deletions(-) create mode 100644 layouts/_default/home.json.json diff --git a/exampleSite/config.toml b/exampleSite/config.toml index 3e57fe9..5a3ae85 100644 --- a/exampleSite/config.toml +++ b/exampleSite/config.toml @@ -16,9 +16,8 @@ disableKinds = ['RSS'] [outputs] # TODO: - # home = [ "HTML", "Atom", "JSON", "humanstxt" ] # section = [ "HTML", "Atom" ] - home = [ 'HTML', 'Atom', 'FEED', 'humanstxt' ] + home = [ 'HTML', 'Atom', 'FEED', 'JSON', 'humanstxt' ] # Atom feed. # For more see: https://validator.w3.org/feed/docs/atom.html#whatIsAtom @@ -51,6 +50,16 @@ disableKinds = ['RSS'] noUgly = true permalinkable = false +# For more see: https://www.jsonfeed.org/ +[outputFormats.JSON] + name = 'Json' + mediaType = 'application/json' + baseName = 'feeds/feed' + rel = 'alternate' + isPlainText = true + isHTML = false + noUgly = false + # For more see: https://gohugo.io/templates/output-formats/ [outputFormats.humanstxt] mediaType = 'text/plain' diff --git a/layouts/_default/home.atom.xml b/layouts/_default/home.atom.xml index 5368ab7..45af5ca 100644 --- a/layouts/_default/home.atom.xml +++ b/layouts/_default/home.atom.xml @@ -4,9 +4,10 @@ xml:lang="{{ site.LanguageCode }}"> {{ printf `<![CDATA[%s]]>` site.Title | safeHTML }} {{ with site.Params.description }}{{ printf `` . | safeHTML }}{{ end }} - + {{ with site.Home.OutputFormats.Get "ATOM" }}{{ end }} {{ with site.Home.OutputFormats.Get "FEED"}}{{end}} + {{ with site.Home.OutputFormats.Get "JSON"}}{{end}} {{ $logo := resources.Get (site.Params.assets.logo | default "/img/open-graph-logo.png" ) }}{{ $logo = $logo.Resize "96x96" }}{{ $logo.Permalink | absURL }} {{ $logo.Permalink | absURL }} {{ with site.Author.name }} diff --git a/layouts/_default/home.feed.xml b/layouts/_default/home.feed.xml index 3522efc..912070d 100644 --- a/layouts/_default/home.feed.xml +++ b/layouts/_default/home.feed.xml @@ -9,20 +9,21 @@ xmlns:media="http://search.yahoo.com/mrss/" xml:lang="{{ site.LanguageCode }}"> - {{ printf `<![CDATA[%s RSS]]>` site.Title | safeHTML }} + {{ printf `<![CDATA[%s RSS 2.0]]>` site.Title | safeHTML }} - {{ "/" | absURL }} + {{ site.BaseURL | absURL }} {{ $logo := resources.Get (site.Params.assets.logo | default "/img/open-graph-logo.png" ) }}{{ $logo = $logo.Resize "96x96" }} {{ $logo.Permalink | absURL }} - {{ printf `<![CDATA[%s RSS]]>` site.Title | safeHTML }} - {{ "/" | absURL }} + {{ printf `<![CDATA[%s RSS 2.0]]>` site.Title | safeHTML }} + {{ site.BaseURL | absURL }} {{ $logo.Width }} {{ $logo.Height }} - + {{ with site.Home.OutputFormats.Get "FEED"}}{{end}} {{ with site.Home.OutputFormats.Get "ATOM"}}{{end}} + {{ with site.Home.OutputFormats.Get "JSON"}}{{end}} {{ with site.Params.description }}{{ printf `` . | safeHTML }}{{ end }} Hugo {{ hugo.Version }} {{ with site.Author.name }}{{ printf `` . | safeHTML }}{{ end }} diff --git a/layouts/_default/home.json.json b/layouts/_default/home.json.json new file mode 100644 index 0000000..77f5f4d --- /dev/null +++ b/layouts/_default/home.json.json @@ -0,0 +1,26 @@ +{ + "version": "https://jsonfeed.org/version/1.1", + "title": {{ (printf "%s JSON Feed" site.Title) | jsonify }}, + "home_page_url": {{ site.BaseURL | absURL | jsonify }}, + {{ with .OutputFormats.Get "JSON" }}"feed_url": {{.Permalink | absURL | jsonify }},{{ end }} + {{ with site.Params.description}}"description": {{site.Params.description | jsonify}},{{end}} + {{ $logo := resources.Get (site.Params.assets.logo | default "/img/open-graph-logo.png" ) }}{{ $logo = $logo.Resize "96x96" }} + "icon": {{ $logo.Permalink | jsonify }}, + "favicon": {{ $logo.Permalink | jsonify }}, + {{ with site.Author.name }}"author": + { + "name": {{ . | jsonify }} + },{{ end }} + {{ with site.LanguageCode }}"language" : "{{ . }}",{{ end }}{{ $feedsize := site.Params.feedSize | default 25 }} + "items": [{{ $r := first $feedsize (where site.RegularPages.ByDate.Reverse "Type" "in" site.Params.mainSections) }} {{ range $i, $ := $r }}{{ if $i }},{{ end }} + { + "id": {{ $uuid := sha1 (.Permalink | absURL)}}"{{substr $uuid 0 8}}-{{substr $uuid 8 4}}-5{{substr $uuid 13 3}}-{{substr $uuid 16 1}}9{{substr $uuid 17 2}}-{{substr $uuid 21 12}}", + "url": {{ .Permalink | jsonify }}, + "title": {{ .Title | plainify | chomp | jsonify }}, + "summary": {{ .Summary | plainify | chomp | jsonify }}, + "date_published": {{ .PublishDate.UTC.Format site.Params.dateFormatFeed | jsonify }}, + "date_modified" : {{ .Lastmod.UTC.Format site.Params.dateFormatFeed | jsonify }} + } + {{- end}} + ] +} diff --git a/theme.toml b/theme.toml index 590551e..508cbf6 100644 --- a/theme.toml +++ b/theme.toml @@ -11,32 +11,31 @@ demosite = "https://sergeyklay.github.io/gohugo-theme-ed/" tags = [ "blog", - "responsive", - "simple", "clean", + "customizable", + "ed", "light", "minimal", "minimalist", - "customizable", - "ed" + "responsive", + "simple" ] # The theme's features: features = [ "blog", - "shortcodes", - "responsive", - "posts", - "opengraph", - "favicon", - # "archive", - # "search", "clean", - "simple", + "favicon", "feed", "minimal", "minimalist", - "mobile" + "mobile", + "opengraph", + "posts", + "responsive", + "rss", + "shortcodes", + "simple" ] min_version = "0.95.0" -- cgit v1.2.3