Broke out all functionality into plugins

This commit is contained in:
2024-01-29 21:39:31 +01:00
parent 31c253c99b
commit fb79c2d10e
10 changed files with 171 additions and 128 deletions

View File

@@ -1,96 +1,32 @@
import * as fs from "fs";
import { destructivelyRecreateDirectory } from "./toolchain-helpers.js";
import { loadMarkdown } from "./markdown-loader.js";
import { MenuManifest, Plugin, IPluginBuilder } from "./typings.js";
export type Paths = {
outputDirectory: string;
notes: {
rootPath: string;
assetsDirectoryName: string;
noteFileName: string;
markdownHtmlVariableReplacementTag: string;
};
templates: {
rootPath: string;
noteTemplateName: string;
indexTemplateName: string;
notFoundName: string;
};
};
export type MenuManifest = { name: string; link: string }[];
export type PluginBuilder = () => Plugin;
export type Plugin = (builderContext: {
menuManifest: MenuManifest;
}) => void | Promise<void>;
export class ToolchainBuilder {
private paths: Paths;
private plugins: Plugin[] = [];
private menuManifest: MenuManifest = [];
constructor(paths: Paths) {
this.paths = paths;
}
addPlugins(plugins: IPluginBuilder[]): ToolchainBuilder {
this.plugins = plugins.map((p) => p.build());
return this;
}
async build() {
destructivelyRecreateDirectory(this.paths.outputDirectory);
loadMarkdown(
{
rootPath: this.paths.notes.rootPath,
assetsDirectoryName: this.paths.notes.assetsDirectoryName,
noteFileName: this.paths.notes.noteFileName,
markdownHtmlReplacementTag:
this.paths.notes.markdownHtmlVariableReplacementTag,
noteHtmlTemplatePath: `${this.paths.templates.rootPath}/${this.paths.templates.noteTemplateName}`,
for (const plugin of this.plugins) {
await plugin({
menuManifest: this.menuManifest,
outputDirectory: this.paths.outputDirectory,
},
this.menuManifest
);
const plugins: Plugin[] = [
() => {},
async () => {
return new Promise<void>((resolve) => {
resolve();
});
},
];
for (const plugin of plugins) {
await plugin({ menuManifest: this.menuManifest });
});
}
// static html loader
[this.paths.templates.notFoundName].forEach((filename) => {
fs.copyFileSync(
`${this.paths.templates.rootPath}/${filename}`,
`${this.paths.outputDirectory}/${filename}`
);
});
console.log("Building startpage..");
let htmlTemplate = fs.readFileSync(
`${this.paths.templates.rootPath}/${this.paths.templates.indexTemplateName}`,
{
encoding: "utf-8",
}
);
const links = this.menuManifest
.map((m) => `<a href='${m.link}'>${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(`${this.paths.outputDirectory}/index.html`, htmlTemplate, {
encoding: "utf-8",
flag: "ax",
});
console.log("Done");
}
}