Add a relative time formatter (#7)

This commit is contained in:
Carter McBride 2024-06-19 15:31:57 -06:00 committed by GitHub
parent 048e3fcde7
commit 39c6402253
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 8 additions and 3 deletions

BIN
bun.lockb

Binary file not shown.

View file

@ -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>

View file

@ -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"

View file

@ -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 => {