Files
artcraft/frontend/scripts/generate-news-json.mjs
Brandon Thomas 0004726a36 ArtCraft Desktop (fresh start)
This is a fresh start for the `artcraft` repo.

The old monorepo, which included our web API gateway, job services, SRE
tools, various frontends, and more has moved to `artcraft-services`

You can find the full git history here:

https://github.com/storytold/artcraft-services

This change was motivated by the fact that the entire git history was
hundreds of gigabytes in size, which is unwieldy for anyone not in the
core team that doesn't care about support services outside of the core
artcraft app.

We wanted to keep this repo in its well-known place since we already
have users and activity here.

Please update your remotes to either `artcraft-services` or make a fresh
clone of this repo.

History can be found at the artcraft-services repo.
2026-09-11 14:19:51 -04:00

72 lines
2.2 KiB
JavaScript

import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const NEWS_DIR = path.resolve(__dirname, '../libs/markdown-content/src/lib/content/news');
const OUTPUT_FILE = path.resolve(__dirname, '../apps/artcraft-website/public/news.json');
const parseFrontmatter = (raw) => {
const text = raw.replace(/\r\n/g, "\n");
if (!text.startsWith("---\n")) {
return { frontmatter: {}, body: text };
}
const end = text.indexOf("\n---\n", 4);
if (end === -1) return { frontmatter: {}, body: text };
const header = text.slice(4, end);
const body = text.slice(end + 5);
const fm = {};
for (const line of header.split("\n")) {
const idx = line.indexOf(":");
if (idx === -1) continue;
const key = line.slice(0, idx).trim();
const value = line
.slice(idx + 1)
.trim()
.replace(/^"|^'|"$|'$/g, "");
if (key) fm[key] = value;
}
return { frontmatter: fm, body };
};
const generateNewsJson = () => {
if (!fs.existsSync(NEWS_DIR)) {
console.error(`News directory not found: ${NEWS_DIR}`);
process.exit(1);
}
const files = fs.readdirSync(NEWS_DIR).filter(f => f.endsWith('.md'));
const posts = files.map(filename => {
const filePath = path.join(NEWS_DIR, filename);
const raw = fs.readFileSync(filePath, 'utf-8');
const { frontmatter, body } = parseFrontmatter(raw);
const slug = filename.replace(/\.md$/, "");
return {
slug,
title: frontmatter.title || slug,
description: frontmatter.abstract || "",
date: frontmatter.date || "",
thumbnail: frontmatter.thumbnail,
body,
};
}).sort((a, b) => {
if (a.date && b.date)
return new Date(b.date).getTime() - new Date(a.date).getTime();
return 0;
});
const outputDir = path.dirname(OUTPUT_FILE);
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
fs.writeFileSync(OUTPUT_FILE, JSON.stringify(posts, null, 2));
console.log(`Generated news.json at ${OUTPUT_FILE} with ${posts.length} posts.`);
};
generateNewsJson();