forked from TayzenDev/smallblog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblog.ts
363 lines (328 loc) · 8.91 KB
/
blog.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import * as path from "@std/path";
import * as fm from "@std/front-matter";
import * as fs from "@std/fs";
import { render } from "@tayzendev/gfm";
import RSS from "rss";
import { Sitemap } from "./sitemap.ts";
import MiniSearch from "minisearch";
import loadLanguages from "npm:[email protected]/components/index.js";
import "npm:prismjs-gleam@1/gleam.js"
loadLanguages(); // should load all language highlightings
function getMarkdown(fileName: string, postsFolder: string) {
const postPath = path.join(postsFolder, fileName);
let content: string;
try {
content = Deno.readTextFileSync(postPath);
} catch (error) {
if (!(error instanceof Deno.errors.NotFound)) {
throw error;
}
return "";
}
return content;
}
export function isPostsFolderEmpty(folder: string) {
try {
for (const _ of fs.expandGlobSync(path.join(folder, "[!_]*.md"))) {
return false;
}
return true;
} catch (e) {
console.error("Error while opening folder:", folder, ":", e);
return true;
}
}
export function getArticles(
postsFolder: string,
routeBase?: string,
defaultAuthors?: string[],
): Article[] {
let postsNames: string[] = [];
try {
for (const entry of fs.expandGlobSync(path.join(postsFolder, "[!_]*.md"))) {
postsNames = postsNames.concat(entry.name);
}
} catch (e) {
console.error("Error while opening folder:", postsFolder, ":", e);
return [];
}
const articles: Article[] = postsNames
.map((name) => {
return new Article(
postsFolder,
name.slice(0, -3),
getMarkdown(name, postsFolder),
routeBase,
defaultAuthors,
);
})
.filter((article) => article.content !== "");
return articles.sort((a, b) => {
if (a?.metadata?.date && b?.metadata?.date) {
return b?.metadata?.date?.getTime() - a?.metadata?.date?.getTime();
}
return -1;
});
}
export function filterArticlesFTS(
articles: Article[],
query: string,
): Article[] {
if (!query) {
return articles;
}
const tagQuery = query.startsWith("tag::");
if (tagQuery) {
const tag = query.slice("tags::".length - 1);
return filterByTag(articles, tag);
}
const miniSearch = new MiniSearch({
fields: ["title", "content", "tags"], // fields to index for full-text search
storeFields: [], // fields to return with search results
idField: "url",
});
const transformedArticles = articles.map((article) => {
return {
...article,
tags: article.metadata.tags?.join(" "),
};
});
miniSearch.addAll(transformedArticles);
const searchResult = miniSearch.search(query, { prefix: true });
const result = searchResult.flatMap((articleResult) => {
return articles.find((article) => article.url === articleResult.id) || [];
});
return result || [];
}
function filterByTag(articles: Article[], tag: string): Article[] {
return articles.filter((article) => {
return article.metadata.tags?.includes(tag);
});
}
export function getRSS(baseUrl: string, articles: Article[]) {
const feed = new RSS({
title: "Tayzen's blog",
description: "A blog about nerdy stuffs.",
site_url: baseUrl,
feed_url: new URL("rss.xml", baseUrl).href,
language: "en",
categories: ["Technology", "Programming", "Self-hosting"],
});
for (const article of articles) {
feed.item({
title: article.title,
description: article.html,
url: new URL(article.url, baseUrl).href,
date: article.metadata.date,
categories: article.metadata.tags,
author: article.metadata.authors?.join(", "),
});
}
return feed.xml();
}
export function getSitemap(baseUrl: string, articles: Article[]) {
const sitemap: Sitemap = new Sitemap(baseUrl);
for (const article of articles) {
sitemap.add(article.url, {
changefreq: "weekly",
priority: 1,
});
}
sitemap.add("/", { changefreq: "daily", priority: 0.8 });
return sitemap.sitemap;
}
export function getArticle(
name: string,
postsFolder: string,
routeBase?: string,
defaultAuthors?: string[],
): Article {
const article = new Article(
routeBase || postsFolder,
name,
getMarkdown(name + ".md", postsFolder),
routeBase,
defaultAuthors,
);
return article;
}
function estimateTimeReadingMinutes(
markdownSimplified: string,
): number | string {
return Math.round(markdownSimplified.split(" ").length / 250) || "< 1";
}
type MetadataProps = {
title?: string;
description?: string;
author?: string | string[];
authors?: string | string[];
tag?: string | string[];
tags?: string | string[];
date?: Date;
modificationDate?: Date;
redirect?: string;
preview?: string;
section?: string;
order?: number;
};
export class Metadata {
title?: string;
description?: string;
authors?: string[];
tags?: string[];
date?: Date;
modificationDate?: Date;
redirect?: string;
preview?: string;
section?: string;
order?: number;
constructor(
filePath: string,
{
title,
description,
authors,
author,
tags,
tag,
date,
modificationDate,
redirect,
preview,
section,
order,
}: MetadataProps,
defaultAuthors?: string[],
) {
let fileStats;
try {
fileStats = Deno.statSync(filePath);
} catch {
console.error(`File ${filePath} not found`);
}
this.title = title;
this.description = description;
const actualAuthors = authors || author || defaultAuthors;
if (typeof actualAuthors === "string") {
this.authors = [actualAuthors];
} else {
this.authors = actualAuthors;
}
const actualTags = tags || tag;
if (typeof actualTags === "string") {
this.tags = [actualTags];
} else {
this.tags = actualTags;
}
this.date = date || fileStats?.birthtime || undefined;
this.modificationDate = modificationDate || fileStats?.mtime || undefined;
this.redirect = redirect;
this.preview = preview;
this.section = section;
this.order = order;
}
}
function convertNameToLabel(name: string) {
const nameWithoutStartingUnderscore = name.startsWith("_") ? name.slice(1) : name;
const nameWithSpace = nameWithoutStartingUnderscore.split("_").join(" ");
return nameWithSpace.charAt(0).toUpperCase() + nameWithSpace.slice(1);
}
type ClearedMarkdown = {
body: string;
title?: string;
};
function removingTitleFromMD(markdown: string): ClearedMarkdown {
const titleRegex = /^# +\S+[\S ]*/m;
const titleMatched = markdown.match(titleRegex);
if (!titleMatched) {
return { body: markdown };
}
const titlePosition = markdown.indexOf(titleMatched[0]);
if (titlePosition === undefined || titlePosition < 0) {
return { body: markdown };
}
return { title: titleMatched[0].substring(2), body: markdown.substring(titlePosition + titleMatched[0].length) };
}
type ParsedMarkdown = {
metadata: Metadata;
body: string;
title?: string;
};
function parseMd(
markdownData: string,
filePath: string,
defaultAuthors?: string[],
): ParsedMarkdown {
if (fm.test(markdownData)) {
const data = fm.extractYaml(markdownData);
const { title, body } = removingTitleFromMD(data.body);
return {
metadata: new Metadata(
filePath,
data.attrs as MetadataProps,
defaultAuthors,
),
body: body.trim(),
title,
};
}
const metadata = new Metadata(filePath, {}, defaultAuthors)
const { title, body } = removingTitleFromMD(markdownData);
return {
metadata ,
body: body.trim(),
title
};
}
function customRender(text: string, noLinks: boolean = false) {
return render(text, {
disableHtmlSanitization: true,
allowIframes: true,
allowMath: true,
liteYTEmbed: true,
noLinks,
});
}
export class Article {
name: string;
title: string;
content: string;
preview: string;
html: string;
metadata: Metadata;
url: string;
timeToReadMinutes: number | string;
constructor(
postsFolder: string,
name: string,
content: string,
routeBase: string = path.basename(postsFolder),
defaultAuthors?: string[],
title?: string,
html?: string,
metadata?: Metadata,
timeToReadMinutes?: number | string,
) {
const { metadata: parsedMetadata, body: cleanedContent, title: h1Title } = parseMd(
content,
path.join(postsFolder, name + ".md"),
defaultAuthors,
);
this.metadata = metadata || parsedMetadata;
this.name = name;
this.content = content;
this.title = title || this.metadata.title || h1Title || convertNameToLabel(name);
this.preview = this.metadata.preview
? customRender(this.metadata.preview, true)
: customRender(
cleanedContent.length > 300
? cleanedContent.slice(0, 300) + "..."
: cleanedContent,
true,
);
this.html = html || customRender(cleanedContent);
this.url = path.join("/", routeBase, this.name);
this.timeToReadMinutes =
timeToReadMinutes || estimateTimeReadingMinutes(cleanedContent);
}
}