forked from Protonull/IceniaGov
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fix-dates.ts
executable file
·56 lines (48 loc) · 1.87 KB
/
fix-dates.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
#!/usr/bin/env bun
import { Glob } from "bun";
import { promises as fs } from "node:fs";
import * as path from "node:path";
import parseGreyMatter from "gray-matter";
import {Dates, Strings} from "@helpers";
const SOURCE_NEWS_FOLDER = "./src/content/news/";
const SITE_NEWS_FOLDER = "/news/";
const NEWS_REGEX = /^\d+-\d{2}-\d{2}-(.*?)$/;
const redirects = new Map<string, string>();
const glob = new Glob("*.md");
for await (const filepath of glob.scan({ cwd: SOURCE_NEWS_FOLDER, onlyFiles: true })) {
const filename = path.parse(filepath).name;
let slugSuffix = "senate-election"; {
// Convert "2024-02-01-senate-election" to "senate-election"
const match = NEWS_REGEX.exec(filename)
if (match === null) {
console.warn(`Could not match [${filename}] with the news-item regex!`);
continue;
}
slugSuffix = match[1];
}
let datePrefix = "0000-00-00"; {
const parsed = parseGreyMatter(await Bun.file(SOURCE_NEWS_FOLDER + filepath).text());
const date = Dates.parseDate(parsed.data.date);
if (date === null) {
console.warn(`Encountered weird date [${parsed.data.date}] within [${filename}]!`);
continue;
}
datePrefix = Strings.formatDate(date, "yyyy-mm-dd");
}
const expectedFilename = datePrefix + "-" + slugSuffix;
if (expectedFilename === filename) {
continue;
}
console.warn(`Found incorrectly dated slug! Expected [${expectedFilename}], actual [${filename}]`);
await fs.rename(
SOURCE_NEWS_FOLDER + filepath,
SOURCE_NEWS_FOLDER + expectedFilename + ".md"
);
redirects.set(
SITE_NEWS_FOLDER + filename,
SITE_NEWS_FOLDER + expectedFilename
);
}
if (redirects.size > 0) {
console.log("Please add the following entries to Astro's redirects:", Object.fromEntries(redirects.entries()));
}