Add related content section to single page (#77)

Set `relatedPosts` to `true` in the config to enable this.
This commit is contained in:
Track3 2019-06-21 13:15:53 +08:00 committed by GitHub
parent e333628a26
commit 6cee5adc69
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 43 additions and 2 deletions

View file

@ -687,6 +687,25 @@ hr.post-end {
} }
} }
.related-posts {
padding: .8em;
margin-top: 1.5em;
font-size: .8rem;
border: 3px dashed rgba(255, 255, 255, .2);
border-radius: 5px;
h2{
margin: 0;
line-height: normal;
}
ul {
margin-top: .5em;
margin-bottom: 0;
}
}
// Media Queries // Media Queries
// //
@media (min-width: 800px) { @media (min-width: 800px) {

View file

@ -46,6 +46,8 @@ enableEmoji = true # Shorthand emojis in content files - https://gohugo.io/func
# Toggling this option needs to rebuild SCSS, requires Hugo extended version # Toggling this option needs to rebuild SCSS, requires Hugo extended version
justifyContent = false # Set "text-align: justify" to `.content`. justifyContent = false # Set "text-align: justify" to `.content`.
relatedPosts = false # Add a related content section to all single posts page
# Add custom css # Add custom css
# customCSS = ["css/foo.css", "css/bar.css"] # customCSS = ["css/foo.css", "css/bar.css"]

View file

@ -14,6 +14,9 @@ other = "Archives"
# posts/single.html # posts/single.html
# #
[seeAlso]
other = "See Also"
[wordCount] [wordCount]
other = "{{ .WordCount }} Words" other = "{{ .WordCount }} Words"

View file

@ -14,6 +14,9 @@ other = "归档"
# posts/single.html # posts/single.html
# #
[seeAlso]
other = "相关推荐"
[wordCount] [wordCount]
other = "{{ .WordCount }} 字" other = "{{ .WordCount }} 字"

View file

@ -0,0 +1,11 @@
{{- $related := .Site.RegularPages.Related . | first 5 }}
{{ with $related }}
<div class="related-posts thin">
<h2>{{ i18n "seeAlso" }}</h2>
<ul>
{{ range . }}
<li><a href="{{ .RelPermalink }}">{{ .Title }}</a></li>
{{ end }}
</ul>
</div>
{{ end -}}

View file

@ -25,6 +25,9 @@
<div class="content"> <div class="content">
{{ .Content | replaceRE "(<h[1-6] id=\"([^\"]+)\".+)(</h[1-6]+>)" `${1}<a href="#${2}" class="anchor" aria-hidden="true"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M15 7h3a5 5 0 0 1 5 5 5 5 0 0 1-5 5h-3m-6 0H6a5 5 0 0 1-5-5 5 5 0 0 1 5-5h3"></path><line x1="8" y1="12" x2="16" y2="12"></line></svg></a>${3}` | safeHTML }} {{ .Content | replaceRE "(<h[1-6] id=\"([^\"]+)\".+)(</h[1-6]+>)" `${1}<a href="#${2}" class="anchor" aria-hidden="true"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M15 7h3a5 5 0 0 1 5 5 5 5 0 0 1-5 5h-3m-6 0H6a5 5 0 0 1-5-5 5 5 0 0 1 5-5h3"></path><line x1="8" y1="12" x2="16" y2="12"></line></svg></a>${3}` | safeHTML }}
</div> </div>
{{- if .Site.Params.relatedPosts }}
{{- partial "related-posts.html" . -}}
{{- end }}
<hr class="post-end"> <hr class="post-end">
<footer class="post-info"> <footer class="post-info">
{{- with .Params.tags }} {{- with .Params.tags }}

File diff suppressed because one or more lines are too long

View file

@ -1 +1 @@
{"Target":"css/style.min.d979d8f818e47a9821dee69a30ab80ccf745d990f992a4d3f8cb529d03642480.css","MediaType":"text/css","Data":{"Integrity":"sha256-2XnY+Bjkepgh3uaaMKuAzPdF2ZD5kqTT+MtSnQNkJIA="}} {"Target":"css/style.min.183d31ccbed0fd59293d435028691de8635f709926a69d5df7c744cc5ebf42a3.css","MediaType":"text/css","Data":{"Integrity":"sha256-GD0xzL7Q/VkpPUNQKGkd6GNfcJkmpp1d98dEzF6/QqM="}}