index.js
· 1.7 KiB · JavaScript
Eredeti
import { stringify } from "yaml";
import fs from "node:fs";
import https from "node:https";
import sanitize from "sanitize-html";
function omit(obj, props) {
obj = { ...obj };
props.forEach((prop) => delete obj[prop]);
return obj;
}
function readJsonFile(file = "./outbox.json") {
const data = fs.readFileSync(file);
return JSON.parse(data);
}
function processOutbox(outbox) {
return outbox.map((item) => {
const body = sanitize(item.content, {
allowedTags: [],
allowedAttributes: {},
});
return {
id: item.id.split("/").at(-1),
date: item.published,
body,
tags: body.match(/(?<=\#)\w*/g),
attachments: item.attachment.map((el) => ({
altText: el.summary,
type: el.mediaType,
url: el.url,
})),
};
});
}
function writeFiles(data, outputDir = "./out") {
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir);
}
for (const item of data) {
const id = item.id;
item.attachments = item.attachments.map((attachment, idx) => {
const extension = attachment.url.split(".").at(-1);
const fileName = `${id}${idx > 0 ? `-${idx}` : ""}.${extension}`;
const fileStream = fs.createWriteStream(`${outputDir}/${fileName}`);
https.get(attachment.url, (res) => res.pipe(fileStream));
attachment.url = `./${fileName}`;
return attachment;
});
const yaml = stringify(omit(item, ["id"]));
const file = `---\n${yaml}---\n\n${item.body}\n`;
fs.writeFileSync(`${outputDir}/${id}.md`, file);
}
}
function main() {
const outbox = readJsonFile();
const processedOutbox = processOutbox(outbox);
writeFiles(processedOutbox);
}
main();
| 1 | import { stringify } from "yaml"; |
| 2 | import fs from "node:fs"; |
| 3 | import https from "node:https"; |
| 4 | import sanitize from "sanitize-html"; |
| 5 | |
| 6 | function omit(obj, props) { |
| 7 | obj = { ...obj }; |
| 8 | props.forEach((prop) => delete obj[prop]); |
| 9 | return obj; |
| 10 | } |
| 11 | |
| 12 | function readJsonFile(file = "./outbox.json") { |
| 13 | const data = fs.readFileSync(file); |
| 14 | return JSON.parse(data); |
| 15 | } |
| 16 | |
| 17 | function 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 | |
| 37 | function 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 | |
| 58 | function main() { |
| 59 | const outbox = readJsonFile(); |
| 60 | const processedOutbox = processOutbox(outbox); |
| 61 | writeFiles(processedOutbox); |
| 62 | } |
| 63 | |
| 64 | main(); |
| 65 |
package.json
· 389 B · Binary
Eredeti
This file can't be rendered. Tekintsd meg a fájl egész tartalmát.