Skip to content

Commit 913186b

Browse files
committed
fix: omit month if date specified is only year
1 parent d3c7456 commit 913186b

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

index.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ import Handlebars from 'handlebars';
33
import { minify } from 'html-minifier';
44
import { marked } from 'marked';
55

6+
const DATE_LOCALE = 'en-US';
7+
/** @type {Intl.DateTimeFormatOptions} */
8+
const LONG_DATE_FORMAT = { month: 'short', year: 'numeric' };
9+
/** @type {Intl.DateTimeFormatOptions} */
10+
const SHORT_DATE_FORMAT = { year: 'numeric' };
11+
612
/**
713
* Custom renderer for marked, namely to disable unwanted features. We only want
814
* to allow basic inline elements, like links, bold, or inline-code.
@@ -49,27 +55,25 @@ const minifyOptions = {
4955
sortClassName: true,
5056
};
5157

52-
Handlebars.registerHelper('date', (body) => {
58+
Handlebars.registerHelper('date', /** @param {string} body */ (body) => {
5359
if (!body) {
5460
return 'Present'
5561
}
5662

5763
const date = new Date(body);
58-
5964
const datetime = date.toISOString();
60-
const localeString = date.toLocaleDateString('en-US', {
61-
month: 'short',
62-
year: 'numeric'
63-
});
65+
const localeString = body.split('-').length !== 1
66+
? date.toLocaleDateString(DATE_LOCALE, LONG_DATE_FORMAT)
67+
: date.toLocaleDateString(DATE_LOCALE, SHORT_DATE_FORMAT);
6468

6569
return `<time datetime="${datetime}">${localeString}</time>`;
6670
});
6771

68-
Handlebars.registerHelper('markdown', (body) => {
72+
Handlebars.registerHelper('markdown', /** @param {string} body */ (body) => {
6973
return marked.parse(body);
7074
});
7175

72-
Handlebars.registerHelper('link', (body) => {
76+
Handlebars.registerHelper('link', /** @param {string} body */ (body) => {
7377
const parsed = new URL(body);
7478
const host = (parsed.host.startsWith('www.')) ? parsed.host.substring(4) : parsed.host;
7579
return `<a href="${body}">${host}</a>`;

0 commit comments

Comments
 (0)