Skip to content

Commit

Permalink
feat(post): add out-of-date content warning (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
keithmork authored and olOwOlo committed May 31, 2018
1 parent e1781a2 commit 9033266
Show file tree
Hide file tree
Showing 13 changed files with 118 additions and 6 deletions.
4 changes: 4 additions & 0 deletions archetypes/default.md
Expand Up @@ -26,6 +26,10 @@ mathjaxEnableAutoNumber: false
# You unlisted posts you might want not want the header or footer to show
hideHeaderAndFooter: false

# You can enable or disable out-of-date content warning for individual post.
# Comment this out to use the global config.
#enableOutdatedInfoWarning: false

flowchartDiagrams:
enable: false
options: ""
Expand Down
9 changes: 9 additions & 0 deletions exampleSite/config.toml
Expand Up @@ -98,6 +98,15 @@ copyright = "" # default: author.name ↓ # 默认为下面配
gitmentCSS = '<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gitment@0.0.3/style/default.min.css" crossorigin="anonymous">'
gitalkJS = '<script src="https://cdn.jsdelivr.net/npm/gitalk@1.2.2/dist/gitalk.min.js" integrity="sha256-DcjhdbufsHMHflFjZtKNFnPKOAL2ybOxGcPOR4MtnJg=" crossorigin="anonymous"></script>'
gitalkCSS = '<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gitalk@1.2.2/dist/gitalk.css" integrity="sha256-rJVe5uyYRXdLM+Kkoj7JtN+9qI0bZTxkYTaNWODpg7U=" crossorigin="anonymous">'
timeagoJS = '<script src="https://cdn.jsdelivr.net/npm/timeago.js@3.0.2/dist/timeago.min.js" integrity="sha256-jwCP0NAdCBloaIWTWHmW4i3snUNMHUNO+jr9rYd2iOI=" crossorigin="anonymous"></script>'
timeagoLocalesJS = '<script src="https://cdn.jsdelivr.net/npm/timeago.js@3.0.2/dist/timeago.locales.min.js" integrity="sha256-ZwofwC1Lf/faQCzN7nZtfijVV6hSwxjQMwXL4gn9qU8=" crossorigin="anonymous"></script>'

# Display a message at the beginning of an article to warn the readers that it's content may be outdated.
# 在文章开头显示提示信息,提醒读者文章内容可能过时。
[params.outdatedInfoWarning]
enable = false
hint = 30 # Display hint if the last modified time is more than these days ago. # 如果文章最后更新于这天数之前,显示提醒
warn = 180 # Display warning if the last modified time is more than these days ago. # 如果文章最后更新于这天数之前,显示警告

[params.gitment] # Gitment is a comment system based on GitHub issues. see https://github.com/imsun/gitment
owner = "" # Your GitHub ID
Expand Down
10 changes: 8 additions & 2 deletions i18n/en.yaml
Expand Up @@ -33,10 +33,10 @@ nextPage:

prevPost:
other: "Prev"

nextPost:
other: "Next"

toc:
other: "Contents"

Expand All @@ -60,6 +60,12 @@ readingTime:
one: "{{ .Count }} min read"
other: "{{ .Count }} mins read"

outdatedInfoWarningBefore:
other: "[NOTE] Updated "

outdatedInfoWarningAfter:
other: ". This article may have outdated content or subject matter."

# ===== content license =====
author:
other: "Author"
Expand Down
6 changes: 6 additions & 0 deletions i18n/es.yaml
Expand Up @@ -60,6 +60,12 @@ readingTime:
one: "{{ .Count }} min lectura"
other: "{{ .Count }} mins lectura"

outdatedInfoWarningBefore:
other: "[NOTE] Updated "

outdatedInfoWarningAfter:
other: ". This article may have outdated content or subject matter."

# ===== content license =====
author:
other: "Autor"
Expand Down
6 changes: 6 additions & 0 deletions i18n/fr.yaml
Expand Up @@ -60,6 +60,12 @@ readingTime:
one: "{{ .Count }} min de lecture"
other: "{{ .Count }} mins de lecture"

outdatedInfoWarningBefore:
other: "[NOTE] Updated "

outdatedInfoWarningAfter:
other: ". This article may have outdated content or subject matter."

# ===== content license =====
author:
other: "Auteur"
Expand Down
10 changes: 8 additions & 2 deletions i18n/zh-CN.yaml
Expand Up @@ -33,10 +33,10 @@ nextPage:

prevPost:
other: "上一篇"

nextPost:
other: "下一篇"

toc:
other: "文章目录"

Expand All @@ -60,6 +60,12 @@ readingTime:
one: "预计阅读 {{ .Count }} 分钟"
other: "预计阅读 {{ .Count }} 分钟"

outdatedInfoWarningBefore:
other: "【注意】最后更新于 "

outdatedInfoWarningAfter:
other: ",文中内容可能已过时,请谨慎使用。"

# ===== content license =====
author:
other: "文章作者"
Expand Down
28 changes: 28 additions & 0 deletions layouts/partials/post/outdated-info-warning.html
@@ -0,0 +1,28 @@
{{- if or .Params.enableOutdatedInfoWarning (and .Site.Params.outdatedInfoWarning.enable (ne .Params.enableOutdatedInfoWarning false)) }}
{{- $daysAgo := div (sub now.Unix .Lastmod.Unix) 86400 }}
{{- $hintThreshold := .Site.Params.outdatedInfoWarning.hint | default 30 }}
{{- $warnThreshold := .Site.Params.outdatedInfoWarning.warn | default 180 }}

{{- $updateTime := .Lastmod }}
{{- if .GitInfo }}
{{- if lt .GitInfo.AuthorDate.Unix .Lastmod.Unix }}
{{- $updateTime := .GitInfo.AuthorDate }}
{{- end }}
{{- end -}}

{{- if gt $daysAgo $hintThreshold }}
<div class="post-outdated">
{{- if gt $daysAgo $warnThreshold }}
<div class="warn">
{{- else }}
<div class="hint">
{{- end }}
<p>{{ T "outdatedInfoWarningBefore" -}}
<span class="timeago" datetime="{{ dateFormat "2006-01-02T15:04:05" $updateTime }}" title="{{ dateFormat "January 2, 2006" $updateTime }}">
{{- dateFormat "January 2, 2006" $updateTime -}}
</span>{{ T "outdatedInfoWarningAfter" -}}
</p>
</div>
</div>
{{- end -}}
{{- end -}}
16 changes: 16 additions & 0 deletions layouts/partials/scripts.html
Expand Up @@ -10,6 +10,22 @@
{{ if .Site.Params.fancybox }}<script type="text/javascript" src="{{ "lib/fancybox/jquery.fancybox-3.1.20.min.js" | relURL }}"></script>{{ end }}
{{- end -}}

<!-- timeago.JS -->
{{- if and (or .Params.enableOutdatedInfoWarning (and .Site.Params.outdatedInfoWarning.enable (ne .Params.enableOutdatedInfoWarning false))) (or .IsPage .IsHome) }}
{{- if .Site.Params.publicCDN.enable }}
{{ .Site.Params.publicCDN.timeagoJS | safeHTML }}
{{ .Site.Params.publicCDN.timeagoLocalesJS | safeHTML }}
{{- else }}
<script type="text/javascript" src="{{ "lib/timeago/timeago-3.0.2.min.js" | relURL }}"></script>
<script type="text/javascript" src="{{ "lib/timeago/timeago.locales-3.0.2.min.js" | relURL }}"></script>
{{- end }}
<script> // NOTE: timeago.js uses the language code format like "zh_CN" (underscore and case sensitive)
var languageCode = {{ .Site.LanguageCode }}.replace(/-/g, '_').replace(/_(.*)/, function ($0, $1) {return $0.replace($1, $1.toUpperCase());});
timeago().render(document.querySelectorAll('.timeago'), languageCode);
timeago.cancel(); // stop update
</script>
{{- end }}

<!-- flowchart -->
{{- if and (or .Params.flowchartDiagrams.enable (and .Site.Params.flowchartDiagrams.enable (ne .Params.flowchartDiagrams.enable false))) (or .IsPage .IsHome) -}}
<script>
Expand Down
3 changes: 3 additions & 0 deletions layouts/post/single.html
Expand Up @@ -27,6 +27,9 @@ <h1 class="post-title">{{ .Title }}</h1>
<!-- TOC -->
{{ partial "post/toc.html" . }}

<!-- Outdated Info Warning -->
{{ partial "post/outdated-info-warning.html" . }}

<!-- Content -->
<div class="post-content">
{{ .Content }}
Expand Down
5 changes: 3 additions & 2 deletions src/css/_partial/_post.scss
Expand Up @@ -9,7 +9,7 @@

.post {
padding: $post-padding;

& + .post {
border-top: $post-border;
}
Expand All @@ -20,4 +20,5 @@
@import '_post/copyright';
@import '_post/reward';
@import '_post/footer';
}
@import '_post/outdated';
}
25 changes: 25 additions & 0 deletions src/css/_partial/_post/_outdated.scss
@@ -0,0 +1,25 @@
.post-outdated {
.hint {
position: relative;
margin-top: 20px;
margin-bottom: 20px;
padding: 5px 10px;
border-left: 4px solid rgb(66, 172, 243);
background-color: rgb(239, 245, 255);
border-color: rgb(66, 172, 243);
}

.warn {
position: relative;
margin-top: 20px;
margin-bottom: 20px;
padding: 5px 10px;
border-left: 4px solid #f9cf63;
background-color: #ffffc0;
border-color: #f9cf63;
}
}




1 change: 1 addition & 0 deletions static/lib/timeago/timeago-3.0.2.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions static/lib/timeago/timeago.locales-3.0.2.min.js

Large diffs are not rendered by default.

0 comments on commit 9033266

Please sign in to comment.