最后活跃于 10 months ago

Takes the outbox.json Pixelfed export data and downloads the files and creates markdown files. Great for use with something like 11ty or Astro.

修订 70bd840f7e91a40201dc7131f78d9814a87ac95b

index.js 原始文件
1import { stringify } from "yaml";
2import fs from "node:fs";
3import https from "node:https";
4import sanitize from "sanitize-html";
5
6function omit(obj, props) {
7 obj = { ...obj };
8 props.forEach((prop) => delete obj[prop]);
9 return obj;
10}
11
12function readJsonFile(file = "./outbox.json") {
13 const data = fs.readFileSync(file);
14 return JSON.parse(data);
15}
16
17function processOutbox(outbox) {
18 return outbox.map((item) => {
19 const body = sanitize(item.content, {
20 allowedTags: [],
21 allowedAttributes: {},
22 });
23 return {
24 id: item.id.split("/").at(-1),
25 date: item.published,
26 body,
27 tags: body.match(/(?<=\#)\w*/g),
28 attachments: item.attachment.map((el) => ({
29 altText: el.summary,
30 type: el.mediaType,
31 url: el.url,
32 })),
33 };
34 });
35}
36
37function writeFiles(data, outputDir = "./out") {
38 if (!fs.existsSync(outputDir)) {
39 fs.mkdirSync(outputDir);
40 }
41
42 for (const item of data) {
43 const id = item.id;
44 item.attachments = item.attachments.map((attachment, idx) => {
45 const extension = attachment.url.split(".").at(-1);
46 const fileName = `${id}${idx > 0 ? `-${idx}` : ""}.${extension}`;
47 const fileStream = fs.createWriteStream(`${outputDir}/${fileName}`);
48 https.get(attachment.url, (res) => res.pipe(fileStream));
49 attachment.url = `./${fileName}`;
50 return attachment;
51 });
52 const yaml = stringify(omit(item, ["id"]));
53 const file = `---\n${yaml}---\n\n${item.body}\n`;
54 fs.writeFileSync(`${outputDir}/${id}.md`, file);
55 }
56}
57
58function main() {
59 const outbox = readJsonFile();
60 const processedOutbox = processOutbox(outbox);
61 writeFiles(processedOutbox);
62}
63
64main();
65
package.json 原始文件
This file can't be rendered. 查看完整文件。