98 lines
2.4 KiB
JavaScript
98 lines
2.4 KiB
JavaScript
import * as fs from "fs";
|
|
import { toHtml } from "./markdown.js";
|
|
|
|
const paths = {
|
|
output: ".dist",
|
|
notes: {
|
|
root: "notes",
|
|
assets: "assets",
|
|
note: "note.md",
|
|
},
|
|
templates: {
|
|
root: "templates",
|
|
note: "note.html",
|
|
index: "index.html",
|
|
notFound: "404.html",
|
|
},
|
|
};
|
|
|
|
if (fs.readdirSync(".").includes(paths.output)) {
|
|
fs.rmSync(paths.output, { recursive: true, force: true });
|
|
console.log("Starting rebuild");
|
|
} else {
|
|
console.log("Starting build");
|
|
}
|
|
fs.mkdirSync(paths.output);
|
|
|
|
console.log("Indexing content..");
|
|
const directoryNames = fs.readdirSync(paths.notes.root);
|
|
const manifest = directoryNames.map((noteDirectory) => ({
|
|
directoryName: noteDirectory,
|
|
name: noteDirectory,
|
|
markdown: fs.readFileSync(
|
|
`${paths.notes.root}/${noteDirectory}/${paths.notes.note}`,
|
|
{ encoding: "utf-8" }
|
|
),
|
|
assetDirectoryPath: `${paths.notes.root}/${noteDirectory}/${paths.notes.assets}`,
|
|
publicAssetDirectoryPath: `/${noteDirectory}_`,
|
|
}));
|
|
|
|
console.log("Parsing content..");
|
|
manifest.forEach((m) => {
|
|
const notePath = `${paths.templates.root}/${paths.templates.note}`;
|
|
let htmlTemplate = fs.readFileSync(notePath, {
|
|
encoding: "utf-8",
|
|
});
|
|
htmlTemplate = htmlTemplate.replace(
|
|
"{{markdown}}",
|
|
toHtml(m.markdown, m.publicAssetDirectoryPath)
|
|
);
|
|
fs.writeFileSync(`${paths.output}/${m.directoryName}.html`, htmlTemplate, {
|
|
encoding: "utf-8",
|
|
flag: "ax",
|
|
});
|
|
|
|
if (!fs.existsSync(m.assetDirectoryPath)) return;
|
|
const assetsList = fs.readdirSync(m.assetDirectoryPath);
|
|
assetsList.forEach((assetName) =>
|
|
fs.cpSync(
|
|
`${m.assetDirectoryPath}/${assetName}`,
|
|
`${paths.output}/${m.name}_${assetName}`
|
|
)
|
|
);
|
|
});
|
|
|
|
console.log("Moving static templates..");
|
|
[paths.templates.notFound].forEach((filename) => {
|
|
fs.copyFileSync(
|
|
`${paths.templates.root}/${filename}`,
|
|
`${paths.output}/${filename}`
|
|
);
|
|
});
|
|
|
|
console.log("Building startpage..");
|
|
let htmlTemplate = fs.readFileSync(
|
|
`${paths.templates.root}/${paths.templates.index}`,
|
|
{
|
|
encoding: "utf-8",
|
|
}
|
|
);
|
|
|
|
const links = manifest
|
|
.map((m) => `<a href='/${m.directoryName}.html'>${m.name}</a>`)
|
|
.reverse();
|
|
|
|
const unorderedListItems = links.map((l) => `<li>${l}</li>`).join("\r\n");
|
|
const html = `
|
|
<ul>
|
|
${unorderedListItems}
|
|
<ul>
|
|
`;
|
|
htmlTemplate = htmlTemplate.replace("{{content}}", html);
|
|
fs.writeFileSync(`${paths.output}/index.html`, htmlTemplate, {
|
|
encoding: "utf-8",
|
|
flag: "ax",
|
|
});
|
|
|
|
console.log("Done");
|