131 lines
3.4 KiB
JavaScript
131 lines
3.4 KiB
JavaScript
import Handlebars from "handlebars";
|
|
import * as fs from "fs";
|
|
import * as sass from "sass";
|
|
import * as YAML from "yaml";
|
|
import { unified } from "unified";
|
|
import remarkParse from "remark-parse";
|
|
import remarkGfm from "remark-gfm";
|
|
import remarkToc from "remark-toc";
|
|
import remarkRehype from "remark-rehype";
|
|
import rehypeSlug from "rehype-slug";
|
|
import rehypeStringify from "rehype-stringify";
|
|
|
|
deletePreviousBuild();
|
|
loadPartials();
|
|
buildPages();
|
|
buildMarkdownPages();
|
|
copyAssets();
|
|
buildStyles();
|
|
|
|
function deletePreviousBuild() {
|
|
if (fs.existsSync("./build")) fs.rmSync("./build", { recursive: true });
|
|
fs.mkdirSync("./build");
|
|
}
|
|
|
|
function loadPartials() {
|
|
fs.readdirSync("./partials").forEach((file) => {
|
|
if (!file.endsWith(".hbs")) return;
|
|
const template = fs.readFileSync(`./partials/${file}`, "utf8");
|
|
const templateName = file.substring(0, file.length - 4);
|
|
Handlebars.registerPartial(templateName, template);
|
|
});
|
|
}
|
|
|
|
function getData() {
|
|
const data = YAML.parse(fs.readFileSync("./data.yaml", "utf8"));
|
|
|
|
return {
|
|
data,
|
|
year: new Date().getFullYear(),
|
|
};
|
|
}
|
|
|
|
function buildPages() {
|
|
if (!fs.existsSync("./build")) fs.mkdirSync("./build");
|
|
|
|
fs.readdirSync("./pages").forEach((file) => {
|
|
if (!file.endsWith(".hbs")) return;
|
|
if (file.startsWith("_")) return;
|
|
const template = Handlebars.compile(
|
|
fs.readFileSync(`./pages/${file}`, "utf8")
|
|
);
|
|
|
|
const result = template(getData());
|
|
|
|
fs.writeFileSync(
|
|
`./build/${file.substring(0, file.length - 4)}.html`,
|
|
result
|
|
);
|
|
});
|
|
}
|
|
|
|
function buildMarkdownPages() {
|
|
if (!fs.existsSync("./build")) fs.mkdirSync("./build");
|
|
|
|
fs.readdirSync("./pages").forEach(async (file) => {
|
|
if (!file.endsWith(".md")) return;
|
|
if (file.startsWith("_")) return;
|
|
const template = Handlebars.compile(
|
|
fs.readFileSync("./pages/_markdown.hbs", "utf8")
|
|
);
|
|
const processor = unified()
|
|
.use(remarkParse)
|
|
.use(remarkGfm)
|
|
.use(remarkToc, {
|
|
heading: "(table[ -]of[ -])?contents?|toc|inhalts?(verzeichnis)?",
|
|
})
|
|
.use(remarkRehype)
|
|
.use(rehypeSlug)
|
|
.use(rehypeStringify);
|
|
|
|
const markdown = (
|
|
await processor.process(
|
|
handleDynamicMarkdown(fs.readFileSync(`./pages/${file}`, "utf8"))
|
|
)
|
|
).toString();
|
|
|
|
const result = template({
|
|
...getData(),
|
|
markdown,
|
|
});
|
|
fs.writeFileSync(
|
|
`./build/${file.substring(0, file.length - 3)}.html`,
|
|
result
|
|
);
|
|
});
|
|
}
|
|
|
|
function handleDynamicMarkdown(md) {
|
|
const replacers = {
|
|
SCOPE: () => {
|
|
return getData()
|
|
.data.domains.map((d) => `- [${d}](https://${d})`)
|
|
.join("\n");
|
|
},
|
|
};
|
|
|
|
Object.keys(replacers).forEach((key) => {
|
|
md = md.replace(new RegExp(`\\[\\[${key}\\]\\]`, "gm"), replacers[key]());
|
|
});
|
|
|
|
return md;
|
|
}
|
|
|
|
function copyAssets() {
|
|
fs.cpSync("./assets", "./build/assets", { recursive: true });
|
|
}
|
|
|
|
function buildStyles() {
|
|
if (!fs.existsSync("./build")) fs.mkdirSync("./build");
|
|
if (!fs.existsSync("./build/styles")) fs.mkdirSync("./build/styles");
|
|
if (!fs.existsSync("./sass")) return;
|
|
fs.readdirSync("./sass").forEach((file) => {
|
|
if (!(file.endsWith(".scss") || file.endsWith(".sass"))) return;
|
|
if (file.startsWith("_")) return;
|
|
const result = sass.compile(`./sass/${file}`, {
|
|
style: "compressed",
|
|
});
|
|
fs.writeFileSync("./build/styles/styles.css", result.css);
|
|
});
|
|
}
|