Skip to content

Commit

Permalink
Add related content section to single page (#77)
Browse files Browse the repository at this point in the history
Set `relatedPosts` to `true` in the config to enable this.
  • Loading branch information
Track3 authored Jun 21, 2019
1 parent e333628 commit 6cee5ad
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 2 deletions.
19 changes: 19 additions & 0 deletions assets/scss/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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 (min-width: 800px) {
Expand Down
2 changes: 2 additions & 0 deletions exampleSite/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
justifyContent = false # Set "text-align: justify" to `.content`.

relatedPosts = false # Add a related content section to all single posts page

# Add custom css
# customCSS = ["css/foo.css", "css/bar.css"]

Expand Down
3 changes: 3 additions & 0 deletions i18n/en.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ other = "Archives"

# posts/single.html
#
[seeAlso]
other = "See Also"

[wordCount]
other = "{{ .WordCount }} Words"

Expand Down
3 changes: 3 additions & 0 deletions i18n/zh-hans.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ other = "归档"

# posts/single.html
#
[seeAlso]
other = "相关推荐"

[wordCount]
other = "{{ .WordCount }} 字"

Expand Down
11 changes: 11 additions & 0 deletions layouts/partials/related-posts.html
Original file line number Diff line number Diff line change
@@ -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 -}}
3 changes: 3 additions & 0 deletions layouts/posts/single.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ <h1>{{ .Title }}</h1>
<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 }}
</div>
{{- if .Site.Params.relatedPosts }}
{{- partial "related-posts.html" . -}}
{{- end }}
<hr class="post-end">
<footer class="post-info">
{{- with .Params.tags }}
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -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="}}

0 comments on commit 6cee5ad

Please sign in to comment.