-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.ts
140 lines (113 loc) · 3.26 KB
/
helpers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { env } from 'process';
import { Date, Article } from './types';
import ordinal from 'ordinal';
export const IS_PRODUCTION = env.NODE_ENV === 'production';
export const IS_PRINT = env.IS_PRINT === 'print';
export function ifSet<T>(x: T, y: string | ((value: NonNullable<T>) => string)): string {
if (x) {
if (typeof y === 'function') {
return y(x);
}
return y;
}
return '';
}
export function asAttr(name: string, value: string | undefined): string {
return ifSet(value, ` ${name}="${value}"`);
}
const numberFormatter = new Intl.NumberFormat('en');
export function formatNumberString(it: string | number): string {
if (typeof it === 'number') {
return numberFormatter.format(it);
}
return it;
}
const months =
["January"
, "February"
, "March"
, "April"
, "May"
, "June"
, "July"
, "August"
, "September"
, "October"
, "November"
, "December"
];
const days =
[
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
];
export function renderExplicitDate(date: Date, omitIfJustYear: boolean): string | null {
if (typeof date === 'number') {
date = { year: date };
}
if ('month' in date) {
const month = months[date.month - 1];
if ('day' in date) {
if (date.OS) {
const actualDate =
(date.month < 3 || date.month === 3 && date.day < 25)
? new globalThis.Date(date.year + 1, date.month - 1, date.day)
: new globalThis.Date(date.year, date.month - 1, date.day);
const day = (((actualDate.getDay() - 10) % 7) + 7) % 7;
return `${days[day]}, ${ordinal(date.day)} ${month} ${date.year} [<abbr title="old-style">OS</abbr>]`;
}
const actualDate = new globalThis.Date(date.year, date.month - 1, date.day);
const day = actualDate.getDay();
return `${days[day]}, ${ordinal(date.day)} ${month} ${date.year}`;
}
return `${month} ${date.year}`;
}
if ('season' in date) {
return `${date.season} ${date.year}`;
}
if (omitIfJustYear) {
return null;
}
return null;
}
export function isolate(value: string): string {
return `⁨${value}⁩`;
}
export function purify(str: string): string {
return str.replaceAll('&', '&')
.replaceAll('<', '<')
.replaceAll('>', '>')
.replaceAll('\'', ''')
.replaceAll('"', '"');
}
export function renderArticle(article: Article) {
if (!IS_PRODUCTION || !article.draft) {
return `<li><a href="${article.url}"${asAttr("lang", article.titleLang)}>${article.title}</a>`
+ ifSet(article.draft, ' <span class="draft">Draft</span>')
+ (article.children ? renderArticleList(article.children) : '')
+ `</li>`;
}
// render draft article names if they have children
if (article.children?.length) {
return `<li><span${asAttr("lang", article.titleLang)}>${article.title}</span>`
+ (article.children ? renderArticleList(article.children) : '')
+ `</li>`;
}
return '';
}
export function renderArticleList(articles: readonly Article[]) {
if (articles.length === 0) {
return '';
}
let result = '<ul class="article-list">';
for (const article of articles) {
result += renderArticle(article);
}
result += "</ul>";
return result;
}