greatly simplified json generation

This commit is contained in:
Gabriele Musco 2021-04-13 22:33:49 +02:00
parent 6f18465f88
commit 4239ea5347
No known key found for this signature in database
GPG Key ID: 8539FD3454380B83
2 changed files with 26 additions and 43 deletions

View File

@ -1,26 +1,17 @@
{{ define "main" }}
{
"pages": [
{{ range $i, $e := .Site.RegularPages }}
{{ if $i }},{{ end }}
{
"title": {{ .Title | jsonify }},
"date": {{ .Date.Format "2006-01-02" | jsonify }},
"tags": {{ if .Params.tags }}
{{ .Params.tags | jsonify }}
{{ else }}
[]
{{ end }},
"summary": {{ .Summary | jsonify }},
"text": {{ lower .Plain | jsonify }},
"link": "{{ .Permalink }}"
{{ if .Site.Params.imageInArticlePreview }}
, "image": {{ if .Params.Image }}
{{ .Params.Image | jsonify }}{{ else }}""
{{ end }}
{{ end }}
}
{{ end }}
]
{{ $mscratch := newScratch }}
{{ $mscratch.Add "articles" slice }}
{{ range .Site.RegularPages }}
{{ $mscratch.Add "articles" (dict
"title" .Title
"date" (.Date.Format "2006-01-02")
"tags" (or .Params.tags slice)
"summary" .Summary
"text" (lower .Plain)
"link" .Permalink
"image" (or .Params.Image "")) }}
{{ end }}
"pages": {{ $mscratch.Get "articles" | jsonify }}
}
{{ end }}

View File

@ -6,26 +6,18 @@
"next": {{ if .Paginator.HasNext }}
{{ .Paginator.Next.URL | absURL | jsonify }}
{{ else }}""{{ end }},
"articles": [
{{ range $i, $e := (.Paginate $postsList).Pages }}
{{ if $i }}, {{ end }}{
"ignore": {{ not (in $postsDir .Section) | jsonify }},
"title": {{ .Title | jsonify }},
"date": {{ .Date.Format "2006-01-02" | jsonify }},
"tags": {{ if .Params.tags }}
{{ .Params.tags | jsonify }}
{{ else }}
[]
{{ end }},
"summary": {{ .Summary | jsonify }},
"link": "{{ .Permalink }}"
{{ if .Site.Params.imageInArticlePreview }}
, "image": {{ if .Params.Image }}
{{ .Params.Image | jsonify }}{{ else }}""
{{ end }}
{{ end }}
}
{{ end }}
]
{{ $mscratch := newScratch }}
{{ $mscratch.Add "articles" slice }}
{{ range (.Paginate $postsList).Pages }}
{{ $mscratch.Add "articles" (dict
"ignore" (not (in $postsDir .Section))
"title" .Title
"date" (.Date.Format "2006-01-02")
"tags" (or .Params.tags slice)
"summary" .Summary
"link" .Permalink
"image" (or .Params.Image "")) }}
{{ end }}
"articles": {{ $mscratch.Get "articles" | jsonify }}
}
{{ end }}