-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patharticle.js
121 lines (111 loc) · 3.3 KB
/
article.js
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
import {
escapeCharacters,
unescapeReferences,
} from "./lib/character-reference.js";
import config from "../config.js";
import fs from "node:fs/promises";
import { getDateDetails } from "./lib/get-date-details.js";
import path from "node:path";
import { runCommand } from "./lib/run-command.js";
import { selectDraft } from "./lib/select-draft.js";
const checkIDFormat = (id) => {
if (!id) {
throw new Error("A draft must have an ID.");
}
if (id && !/[0-9a-z][-.0-9a-z]*[0-9a-z]/u.test(id)) {
throw new Error(
"This draft ID is not valid. ID must start and end with “0-9” or “a-z”, and must not contain other than “-.a-z0-9”.",
);
}
};
const checkIDConflict = async (id) => {
const file = path.join(config.dir.dest, "blog", `${id}.html`);
const exists = await fs
.access(file, fs.constants.F_OK)
.then(() => true)
.catch(() => false);
if (exists) {
const message = `“${id}” is already used.`;
throw new Error(message);
}
};
const checkTitleType = (title) => {
if (typeof title !== "string") {
throw new Error(
"This draft does not have a valid title. A draft title must be a string.",
);
}
};
const isSameTitle = (title, element) => title === element.title;
const checkTitleConflict = (title, articles) => {
if (articles.findIndex(isSameTitle.bind(null, title)) !== -1) {
const message = `There has been a entry named “${title}”.`;
throw new Error(message);
}
};
const buildArticle = (selected) => {
const { body, id, title } = selected;
const [description] = unescapeReferences(body.replace(/<.*?>/gu, ""))
.trim()
.split("\n");
const link = path.posix.join("/", "blog", `${id}.html`);
const published = Date.now();
const dt = getDateDetails(published);
const shortDescription = `${description.split("。")[0]}。`;
const type = "article";
return {
body,
description: `${description}`,
link,
published,
...dt,
shortDescription,
title,
type,
};
};
const file = path.join(config.dir.data, "articles.json");
const [selected, articles] = await Promise.all([
selectDraft(),
fs.readFile(file).then(JSON.parse),
]);
const body = selected.body.replace(
/(?<=\b(href|src|srcset)=")\.\/dist\//gu,
"/",
);
await Promise.all([
checkIDFormat(selected.id),
checkIDConflict(selected.id),
checkTitleType(selected.title),
checkTitleConflict(selected.title, articles),
]);
const article = buildArticle({
...selected,
body,
});
const articleFile = path.join(config.dir.data, article.link);
await fs.mkdir(path.dirname(articleFile), { recursive: true });
const escapedTitle = escapeCharacters(article.title);
const formatted = JSON.stringify([article, ...articles], null, 2);
await fs.mkdir(path.dirname(file), { recursive: true });
await Promise.all([
fs.writeFile(file, `${formatted}\n`),
fs.writeFile(
articleFile,
`<h1>${escapedTitle}</h1>
${body}
`,
),
]);
await runCommand("git", ["add", "--", file, articleFile]);
const th = articles.length + 1;
await runCommand("git", [
"commit",
`--message=Contribute ${article.link} (${th})`,
]);
const twitter = new URL("https://x.com/intent/tweet");
twitter.searchParams.append(
"text",
`${article.shortDescription} / ${article.title} ${config.scheme}://${config.domain}${article.link}`,
);
await runCommand("open", [twitter.href]);