Skip to content

Commit

Permalink
✨ WIP: add featured image support in feed
Browse files Browse the repository at this point in the history
  • Loading branch information
Lruihao committed Aug 15, 2024
1 parent 4017cc9 commit 3e8f4b0
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 67 deletions.
6 changes: 6 additions & 0 deletions layouts/_default/summary.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
<article class="single summary" itemscope itemtype="http://schema.org/Article">
{{- /* Featured image */ -}}
{{- $image := $params.featuredimagepreview | default $params.featuredimage -}}
{{- with $image }}
{{- if not (hasPrefix . "/") }}
{{- $image = (printf "%s%s" $.RelPermalink .) | safeURL }}
{{- end }}
{{- $image = $image | relURL }}
{{- end }}
{{- with .Resources.GetMatch "featured-image" -}}
{{- $image = .RelPermalink -}}
{{- end -}}
Expand Down
34 changes: 24 additions & 10 deletions layouts/partials/feed/json.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
{{- $pages = $pages | first $limit -}}
{{- end -}}
{{- $length := $pages.Len -}}
{{- /* 支持 image 属性 */ -}}

{
"version": "https://jsonfeed.org/version/1.1",
Expand All @@ -33,9 +32,23 @@
],
{{ end -}}
"items": [
{{ range $index, $element := $pages -}}
{{- $fullText := (.Param "feed").fullText -}}
{{- $author := partial "function/get-author-map.html" .Params.author -}}
{{- range $index, $page := $pages }}
{{- $params := .Params | merge .Site.Params.Page }}
{{- $fullText := (.Param "feed").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" }}",
Expand All @@ -54,17 +67,18 @@
{{ with .Params.tags -}}
"tags": {{ . | jsonify }},
{{ end -}}
{{ with $image -}}
"image": "{{ . }}",
{{ end -}}
{{ with .Summary -}}
"summary": {{ . | plainify | jsonify }},
{{ if not $fullText -}}
"content_html": {{ . | plainify | jsonify }}
{{- end }}
{{- end -}}
{{ 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 }}
}{{ if ne (add $index 1) $pages.Len }},{{ end -}}
{{- end }}
]
}
24 changes: 20 additions & 4 deletions layouts/partials/feed/rss.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
{{- $pages = $pages | first $limit -}}
{{- end -}}
{{- $length := $pages.Len -}}
{{- /* TODO 封面图、文章内图片链接、分类、标签等处理 */ -}}
{{- /* TODO 文章内图片链接、分类、标签等处理 */ -}}

{{- printf "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>" | safeHTML }}
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
Expand All @@ -25,10 +25,23 @@
{{- with .OutputFormats.Get "rss" }}
{{ printf "<atom:link href=%q rel=\"self\" type=%q />" .Permalink .MediaType | safeHTML }}
{{- end }}
{{- range $pages }}
{{- range $page := $pages }}
{{- $params := .Params | merge .Site.Params.Page }}
{{- $fullText := (.Param "feed").fullText }}
{{- $author := partial "function/get-author-map.html" .Params.author -}}
{{- $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 }}
<item>
<title>{{ .Title }}</title>
<link>{{ .Permalink }}</link>
Expand All @@ -40,11 +53,14 @@
{{- "<![CDATA[" | safeHTML }}
{{- $content := dict "Content" .Content "Ruby" $params.ruby "Fraction" $params.fraction "Fontawesome" $params.fontawesome | partial "function/content.html" }}
{{- /* {{- $content | replaceRE `<figure[^>]*>.*</figure>` "" | replaceRE `<img[^>]*( /)?>` "" | safeHTML -}} */ -}}
{{- with $image }}
{{- $content = add (printf "<img src=%q alt=%q referrerpolicy=%q>" . "featured image" "no-referrer") $content | safeHTML }}
{{- end }}
{{- $content | transform.XMLEscape | safeHTML }}
{{- "]]>" | safeHTML -}}
</description>
{{- else }}
<description>{{ .Summary | transform.XMLEscape | safeHTML }}</description>
<description>{{ (.Summary | default .Description | default .Title) | transform.XMLEscape | safeHTML }}</description>
{{- end }}
</item>
{{- end }}
Expand Down
2 changes: 1 addition & 1 deletion layouts/partials/init/index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{- .Scratch.Set "version" "v0.3.10-3581ee1c" -}}
{{- .Scratch.Set "version" "v0.3.10-5a715ca9" -}}
{{- .Scratch.Set "this" dict -}}

{{- partial "init/detection-env.html" . -}}
Expand Down
52 changes: 0 additions & 52 deletions layouts/partials/rss/item.html

This file was deleted.

0 comments on commit 3e8f4b0

Please sign in to comment.