Skip to content

Commit

Permalink
Feat: migrate JSON feed from FixIt theme (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lruihao committed Aug 24, 2024
1 parent 2c3ab0e commit 9493947
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 0 deletions.
Empty file removed assets/.gitkeep
Empty file.
38 changes: 38 additions & 0 deletions hugo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,41 @@
# -------------------------------------------------------------------------------------
# Media Types
# See: https://gohugo.io/templates/output-formats/#media-types
# -------------------------------------------------------------------------------------

[mediaTypes]
# Options to make output feed.json files
# Warning: The application/feed+json media type will cause an unknown error in resources.GetRemote!
# [mediaTypes."application/feed+json"]
# suffixes = ["json"]

# -------------------------------------------------------------------------------------
# Output Format Definitions
# See: https://gohugo.io/templates/output-formats/#output-format-definitions
# -------------------------------------------------------------------------------------

[outputFormats]
# Options to make output feed.json file
[outputFormats.jsonfeed]
baseName = "feed"
# Warning: The application/feed+json media type will cause an unknown error in resources.GetRemote!
mediaType = "application/json"
isPlainText = true
isHTML = false

# -------------------------------------------------------------------------------------
# Customizing Output Formats
# See: https://gohugo.io/templates/output-formats/#customizing-output-formats
# -------------------------------------------------------------------------------------

[outputs]
# <baseURL>/feed.json
home = ["html", "rss", "jsonfeed"]
# <baseURL>/posts/feed.json etc.
section = ["html", "rss", "jsonfeed"]
# <baseURL>/tags/foo/feed.json etc.
term = ["html", "rss", "jsonfeed"]

[module]
[module.hugoVersion]
extended = true
Expand Down
Empty file removed i18n/.gitkeep
Empty file.
3 changes: 3 additions & 0 deletions layouts/_default/home.jsonfeed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{- $options := dict "Site" .Site "Config" .Site.Params.feed "OutputFormats" .OutputFormats -}}
{{- $options = dict "Title" .Site.Title "Pages" (.Scratch.Get "mainSectionPages") | merge $options -}}
{{- partial "feed/json.html" $options -}}
83 changes: 83 additions & 0 deletions layouts/partials/feed/json.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{{- $pages := .Pages -}}
{{- /* Front matter: password */ -}}
{{- $pages = where $pages "Params.password" "eq" nil -}}
{{- /* Front matter: hiddenFromFeed */ -}}
{{- $pages = where $pages "Params.hiddenFromFeed" "ne" true -}}
{{- if ge .Config.limit 1 -}}
{{- $pages = $pages | first .Config.limit -}}
{{- end -}}
{{- $length := $pages.Len -}}

{
"version": "https://jsonfeed.org/version/1.1",
"title": "{{ .Title }}",
"description": "{{ .Site.Params.description | default .Site.Title }}",
"home_page_url": "{{ .Site.BaseURL }}",
{{ with .OutputFormats.Get "feed" -}}
"feed_url": "{{ .Permalink }}",
{{ end -}}
{{ with .Site.Language.LanguageCode -}}
"language": "{{ . }}",
{{ end -}}
"icon": "{{ `/apple-touch-icon.png` | absURL }}",
"favicon": "{{ `/favicon.ico` | absURL }}",
{{ with .Site.Params.author.name -}}
"authors": [
{
"name": "{{ . }}"{{ with $.Site.Params.author.link }},
"url": "{{ . }}"{{ end }}{{ with $.Site.Params.author.avatar }},
"avatar": "{{ . | absURL }}"{{ end }}
}
],
{{ end -}}
"items": [
{{- range $index, $page := $pages }}
{{- $params := .Params | merge .Site.Params.Page }}
{{- $fullText := ((.Params.feed | default dict) | merge $.Config ).fullText }}
{{- $author := partial "function/get-author-map.html" .Params.author }}
{{- $image := $params.featuredimagepreview | default $params.featuredimage }}
{{- with $image }}
{{- if not (hasPrefix . "/") }}
{{- $image = (printf "%s%s" $page.RelPermalink .) | safeURL }}
{{- end }}
{{- $image = $image | absURL }}
{{- end }}
{{- with .Resources.GetMatch "featured-image" }}
{{- $image = .Permalink }}
{{- end }}
{{- with .Resources.GetMatch "featured-image-preview" }}
{{- $image = .Permalink }}
{{- end }}
{
"title": {{ .Title | jsonify }},
"date_published": "{{ .Date.Format "2006-01-02T15:04:05Z07:00" }}",
"date_modified": "{{ .Lastmod.Format "2006-01-02T15:04:05Z07:00" }}",
"id": "{{ .Permalink }}",
"url": "{{ .Permalink }}",
{{ with $author.name -}}
"authors": [
{
"name": "{{ . }}"{{ with $author.link }},
"url": "{{ . }}"{{ end }}{{ with $author.avatar }},
"avatar": "{{ . | absURL }}"{{ end }}
}
],
{{ end -}}
{{ with .Params.tags -}}
"tags": {{ . | jsonify }},
{{ end -}}
{{ with $image -}}
"image": "{{ . }}",
{{ end -}}
{{ with .Summary -}}
"summary": {{ . | plainify | jsonify }},
{{ end -}}
{{- if $fullText -}}
"content_html": {{ .Content | jsonify }}
{{- else -}}
"content_html": {{ (.Summary | default .Description | default .Title) | jsonify }}
{{- end }}
}{{ if ne (add $index 1) $pages.Len }},{{ end -}}
{{- end }}
]
}
Empty file.
8 changes: 8 additions & 0 deletions layouts/section/section.jsonfeed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{{- $title := title (.Params.Title | default ((T .Section) | default .Section | dict "Some" | T "allSome")) -}}
{{- if .Site.Params.withSiteTitle -}}
{{- $title = printf "%s %s %s" $title .Site.Params.titleDelimiter .Site.Title -}}
{{- end -}}
{{- $config := (.Params.feed | default dict) | merge .Site.Params.section.feed | merge .Site.Params.feed -}}
{{- $options := dict "Site" .Site "Config" $config "OutputFormats" .OutputFormats -}}
{{- $options = dict "Title" $title "Pages" .Pages | merge $options -}}
{{- partial "feed/json.html" $options -}}
Empty file removed layouts/shortcodes/.gitkeep
Empty file.
8 changes: 8 additions & 0 deletions layouts/taxonomy/list.jsonfeed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{{- $title := printf "%s %s" .Title (T .Data.Singular | default .Data.Singular) -}}
{{- if .Site.Params.withSiteTitle -}}
{{- $title = printf "%s %s %s" $title .Site.Params.titleDelimiter .Site.Title -}}
{{- end -}}
{{- $config := (.Params.feed | default dict) | merge .Site.Params.list.feed | merge .Site.Params.feed -}}
{{- $options := dict "Site" .Site "Config" $config "OutputFormats" .OutputFormats -}}
{{- $options = dict "Title" $title "Pages" .Pages | merge $options -}}
{{- partial "feed/json.html" $options -}}

0 comments on commit 9493947

Please sign in to comment.