Add a relative time formatter (#7)
This commit is contained in:
parent
048e3fcde7
commit
39c6402253
4 changed files with 8 additions and 3 deletions
BIN
bun.lockb
BIN
bun.lockb
Binary file not shown.
|
|
@ -27,7 +27,7 @@
|
|||
<li class="{% if item.isRecent %}has-recent{% endif %}">
|
||||
<div><a class="article-title" href="{{ item.link }}" target="_blank" rel="noopener norefferer nofollow">{{
|
||||
item.title }}</a>
|
||||
<div class="article-timestamp">{{ item.timestamp | formatDate }}, {{ item.timestamp | formatTime }}</div>
|
||||
<div class="article-timestamp">{{ item.timestamp | relative }}</div>
|
||||
</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@
|
|||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"chalk": "^5.2.0",
|
||||
"javascript-time-ago": "^2.5.10",
|
||||
"node-fetch": "^3.3.1",
|
||||
"nunjucks": "^3.2.4",
|
||||
"rss-parser": "^3.13.0"
|
||||
|
|
|
|||
|
|
@ -8,13 +8,17 @@ import nunjucks from "nunjucks";
|
|||
const env: nunjucks.Environment = nunjucks.configure({ autoescape: true });
|
||||
import { readFile } from "node:fs/promises";
|
||||
import type { Feeds, JSONValue } from "./@types/bubo";
|
||||
import TimeAgo from "javascript-time-ago";
|
||||
import en from "javascript-time-ago/locale/en";
|
||||
|
||||
TimeAgo.addDefaultLocale(en);
|
||||
const timeFormatter = new TimeAgo("en-US");
|
||||
/**
|
||||
* Global filters for my Nunjucks templates
|
||||
*/
|
||||
env.addFilter("formatDate", (dateString): string => {
|
||||
env.addFilter("relative", (dateString): string => {
|
||||
const date: Date = new Date(Number.parseInt(dateString));
|
||||
return !Number.isNaN(date.getTime()) ? date.toLocaleDateString() : dateString;
|
||||
return !Number.isNaN(date.getTime()) ? timeFormatter.format(date) : dateString;
|
||||
});
|
||||
|
||||
env.addFilter("formatTime", (dateString): string => {
|
||||
|
|
|
|||
Loading…
Reference in a new issue