| 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 |
melanie / Pixelfed Export Downloader to Markdown
Last active 3 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.
| 1 | { |
| 2 | "name": "pixelfed-export-downloader", |
| 3 | "version": "1.0.0", |
| 4 | "main": "index.js", |
| 5 | "scripts": { |
| 6 | "start": "node index.js", |
| 7 | "test": "echo \"Error: no test specified\" && exit 1" |
| 8 | }, |
| 9 | "keywords": ["pixelfed"], |
| 10 | "author": "melanie kat", |
| 11 | "license": "ISC", |
| 12 | "type": "module", |
| 13 | "description": "", |
| 14 | "dependencies": { |
| 15 | "sanitize-html": "^2.14.0", |
| 16 | "yaml": "^2.7.0" |
| 17 | } |
| 18 | } |
| 19 |