-
Notifications
You must be signed in to change notification settings - Fork 62
/
get-template-data.ts
198 lines (172 loc) · 6.56 KB
/
get-template-data.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import { htmlToText } from "../utils/html-to-text";
import { cliVersion } from "../utils/version";
import type { EnrichedArticle, EnrichedSource } from "./enrich";
import type { Config } from "./get-config";
import { FEED_FILENAME } from "./render-atom";
import { getDateFromIsoString, getIsoTimeWithOffset } from "./time";
export interface GetTemplateDataOutput {
/** Array of dates, each containing the content published on the date */
dates: TemplateDates[];
/** Array of source, each containing the content published from the source */
sources: TemplateSource[];
/** Array of articles, most recent first */
articles: TemplateArticle[];
/** npm package version of the builder */
cliVersion: string;
/** URL for the generated atom feed */
feedHref: string;
/** URL for the osmosfeed project */
projectUrl: string;
/** ISO timestamp for the build */
siteBuildTimestamp: string;
/** URL to the GitHub Action run. Available only when using GitHub Action */
githubRunUrl: string | null;
siteTitle: string;
}
interface TemplateArticle extends EnrichedArticle {
source: EnrichedSource;
/**
* @deprecated This date should only be used by the server internally.
* Use `isoUtcPublishTime` for high-fidelity timestamp or `isoOffsetPublishDate` for grouping by dates
*/
isoPublishDate: string;
/**
* The publish date in user's specified timezone (UTC by default)
*/
isoOffsetPublishDate: string;
/**
* The publish time in UTC timezone
*/
isoUtcPublishTime: string;
readingTimeInMin: number;
}
interface TemplateSource extends TemplateSourceBase {
dates: {
/**
* @deprecated This date should only be used by the server internally.
* Use `isoUtcPublishTime` for high-fidelity timestamp or `isoOffsetPublishDate` for grouping by dates
*/
isoPublishDate: string;
isoOffsetPublishDate: string;
isoUtcPublishTime: string;
articles: TemplateArticle[];
}[];
}
interface TemplateDates {
/**
* @deprecated This date should only be used by the server internally.
* Use `isoUtcPublishTime` for true timestamp or `isoOffsetPublishDate` for grouping by dates
*/
isoPublishDate: string;
isoOffsetPublishDate: string;
isoUtcPublishTime: string;
articles: TemplateArticle[];
sources: TemplateSourceBase[];
}
interface TemplateSourceBase extends EnrichedSource {
isoOffsetPublishDate: string;
isoUtcPublishTime: string;
articles: TemplateArticle[];
}
export interface GetTemplateDataInput {
enrichedSources: EnrichedSource[];
config: Config;
}
export function getTemplateData(input: GetTemplateDataInput): GetTemplateDataOutput {
const { githubServerUrl, githubRepository, githubRunId } = input.config;
const githubRunUrl =
githubServerUrl && githubRepository && githubRunId
? `${input.config.githubServerUrl}/${input.config.githubRepository}/actions/runs/${input.config.githubRunId}`
: null;
return {
get dates() {
return organizeByDates(input);
},
get sources() {
return organizeBySources(input);
},
get articles() {
return organizeByArticles(input);
},
cliVersion,
githubRunUrl,
siteTitle: input.config.siteTitle,
siteBuildTimestamp: new Date().toISOString(),
projectUrl: `https://github.com/osmoscraft/osmosfeed`,
feedHref: FEED_FILENAME,
};
}
function getTimestamps(isoUtcTimestamp: string, timezoneOffset: number) {
const isoOffsetTime = getIsoTimeWithOffset(isoUtcTimestamp, timezoneOffset);
return {
isoUtcPublishTime: isoUtcTimestamp,
isoOffsetPublishDate: getDateFromIsoString(isoOffsetTime),
};
}
function organizeByArticles(input: GetTemplateDataInput): TemplateArticle[] {
const articles: TemplateArticle[] = input.enrichedSources
.flatMap((enrichedSource) =>
enrichedSource.articles.map((article) => ({
...article,
source: enrichedSource,
isoPublishDate: getDateFromIsoString(article.publishedOn),
...getTimestamps(article.publishedOn, input.config.timezoneOffset),
title: ensureDisplayString(htmlToText(article.title), "Untitled"),
description: ensureDisplayString(htmlToText(article.description), "No content preview"),
readingTimeInMin: Math.round((article.wordCount ?? 0) / 300),
}))
)
.sort((a, b) => b.publishedOn.localeCompare(a.publishedOn)); // by time, most recent first
return articles;
}
function organizeBySources(input: GetTemplateDataInput): TemplateSource[] {
const articles = organizeByArticles(input);
const articlesBySource = groupBy(articles, (article) => article.source);
const sortedArticlesBySource = [...articlesBySource.entries()].map(([source, articles]) => ({
...source,
...getTimestamps(articles[0].isoUtcPublishTime, input.config.timezoneOffset),
articles: articles.sort((a, b) => b.publishedOn.localeCompare(a.publishedOn)), // by date, most recent first
dates: [...groupBy(articles, (article) => article.isoOffsetPublishDate)]
.sort((a, b) => b[0].localeCompare(a[0])) // by date, most recent first
.map(([date, articles]) => ({
isoPublishDate: date,
...getTimestamps(articles[0].isoUtcPublishTime, input.config.timezoneOffset),
articles,
})),
}));
return sortedArticlesBySource;
}
function organizeByDates(input: GetTemplateDataInput): TemplateDates[] {
const articles = organizeByArticles(input);
const articlesByDate = groupBy(articles, (article) => article.isoOffsetPublishDate);
const sortedArticlesByDate = [...articlesByDate.entries()]
.sort((a, b) => b[0].localeCompare(a[0])) // by date, most recent first
.map(([date, articles]) => ({
isoPublishDate: date,
...getTimestamps(articles[0].isoUtcPublishTime, input.config.timezoneOffset),
articles,
sources: [...groupBy(articles, (articles) => articles.source).entries()]
.sort((a, b) => b[1][0].isoUtcPublishTime.localeCompare(a[1][0].isoUtcPublishTime)) // by date, most recent first
.map(([source, articles]) => ({
...source,
...getTimestamps(articles[0].isoUtcPublishTime, input.config.timezoneOffset),
articles,
})),
}));
return sortedArticlesByDate;
}
function groupBy<T, K>(array: T[], selector: (item: T) => K) {
const result = new Map<K, T[]>();
return array.reduce((latest, item) => {
const feature = selector(item);
if (!latest.has(feature)) {
latest.set(feature, [item]);
} else {
latest.get(feature)!.push(item);
}
return latest;
}, result);
}
function ensureDisplayString(input: string | null | undefined, fallback: string) {
return input?.length ? input : fallback;
}