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

@@ -5,9 +5,9 @@ import {
writeTextAsFile,
} from "./toolchain-helpers.js";
import { toHtml } from "./markdown-parser.js";
import { MenuManifest } from "./zblog-toolchain.js";
import { IPluginBuilder, MenuManifest, Plugin } from "./typings.js";
type MarkdownLoaderOptions = {
type MarkdownLoaderManifestOptions = {
notesRootPath: string;
noteFileName: string;
assetsDirectoryName: string;
@@ -26,7 +26,7 @@ const createMarkdownLoaderManifest = ({
notesRootPath,
assetsDirectoryName,
noteFileName,
}: MarkdownLoaderOptions): MarkdownLoaderManifest => {
}: MarkdownLoaderManifestOptions): MarkdownLoaderManifest => {
const directoryNames = fs.readdirSync(notesRootPath);
return directoryNames.map((noteDirectory) => {
const assetDirectoryPath = `${notesRootPath}/${noteDirectory}/${assetsDirectoryName}`;
@@ -79,34 +79,44 @@ const writeMarkdownAsHtmlToOutputDirectory = (
});
};
export const loadMarkdown = (
options: {
rootPath: string;
assetsDirectoryName: string;
noteFileName: string;
noteHtmlTemplatePath: string;
outputDirectory: string;
markdownHtmlReplacementTag: string;
},
menuManifest: MenuManifest
) => {
const markdownManifest = createMarkdownLoaderManifest({
notesRootPath: options.rootPath,
assetsDirectoryName: options.assetsDirectoryName,
noteFileName: options.noteFileName,
});
menuManifest.push(
...markdownManifest.map((m) => ({
name: m.name,
link: `/${m.directoryName}.html`,
}))
);
const noteHtmlTemplate = readFileAsText(options.noteHtmlTemplatePath);
writeMarkdownAsHtmlToOutputDirectory(
markdownManifest,
noteHtmlTemplate,
options.markdownHtmlReplacementTag,
options.outputDirectory
);
copyAssetsToOutputDirectory(markdownManifest, options.outputDirectory);
type MarkdownLoaderOptions = {
rootPath: string;
assetsDirectoryName: string;
noteFileName: string;
noteHtmlTemplatePath: string;
markdownHtmlReplacementTag: string;
};
export class MarkdownLoader implements IPluginBuilder {
options: MarkdownLoaderOptions;
constructor(options: MarkdownLoaderOptions) {
this.options = options;
}
build(): Plugin {
return (builderContext) => {
const markdownManifest = createMarkdownLoaderManifest({
notesRootPath: this.options.rootPath,
assetsDirectoryName: this.options.assetsDirectoryName,
noteFileName: this.options.noteFileName,
});
builderContext.menuManifest.push(
...markdownManifest.map((m) => ({
name: m.name,
link: `/${m.directoryName}.html`,
}))
);
const noteHtmlTemplate = readFileAsText(
this.options.noteHtmlTemplatePath
);
writeMarkdownAsHtmlToOutputDirectory(
markdownManifest,
noteHtmlTemplate,
this.options.markdownHtmlReplacementTag,
builderContext.outputDirectory
);
copyAssetsToOutputDirectory(
markdownManifest,
builderContext.outputDirectory
);
};
}
}