67 lines
2 KiB
JavaScript
67 lines
2 KiB
JavaScript
const fs = require("node:fs/promises");
|
|
|
|
let meta;
|
|
|
|
const chooseFortune = () => {
|
|
const authors = Object.keys(meta.fortune);
|
|
const chosenAuthor = authors[Math.floor(Math.random()*authors.length)];
|
|
const fortunes = meta.fortune[chosenAuthor];
|
|
const chosenFortune = fortunes[Math.floor(Math.random()*fortunes.length)];
|
|
return {
|
|
text: chosenFortune,
|
|
name: chosenAuthor
|
|
};
|
|
};
|
|
|
|
const getPosts = async () => {
|
|
const posts = [];
|
|
for (let i = 0; i < meta.targets.length; i++) {
|
|
const t = meta.targets[i];
|
|
const data = JSON.parse(await fs.readFile(`./~${t}/manifest.rolsf.json`));
|
|
if (!data.document || data.document !== "xyz.rolsf.manifest.own.1") {
|
|
throw new Error(`unsupported own manifest for target: '${t}'`);
|
|
}
|
|
data.posts.forEach(post => {
|
|
post.user = t;
|
|
posts.push(post);
|
|
});
|
|
}
|
|
posts.sort((a, b) => b.unix_time - a.unix_time);
|
|
return posts;
|
|
};
|
|
|
|
const Post = ({ href="#", user, date, title }) =>
|
|
`<a href="${href}" class="post-link">
|
|
<span class="user">${user}</span>
|
|
<span class="date">${date}</span>
|
|
<span class="title">${title}</span>
|
|
</a>`;
|
|
|
|
const Fortune = ({text, name}) =>
|
|
`<div class="fortune"><span id="fortune-text">“${text}”</span><span id="fortune-name"> -- ${name}</span></div>`;
|
|
|
|
const MainPage = async () =>
|
|
`<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>${meta.brand}</title>
|
|
<link rel="stylesheet" href="style.css">
|
|
</head>
|
|
<body>
|
|
<main>
|
|
<span class="h1">${meta.brand}</span>
|
|
${Fortune(chooseFortune())}
|
|
${(await getPosts()).map(Post).join("")}
|
|
</main>
|
|
</body>
|
|
</html>`;
|
|
|
|
|
|
const main = async () => {
|
|
meta = JSON.parse((await fs.readFile("./meta.json")));
|
|
await fs.writeFile("./dist/index.html", await MainPage());
|
|
};
|
|
|
|
main();
|