Continuation on breaking out functionality
This commit is contained in:
112
packages/@zblog/toolchain/src/markdown-loader.ts
Normal file
112
packages/@zblog/toolchain/src/markdown-loader.ts
Normal file
@@ -0,0 +1,112 @@
|
||||
import fs from "fs";
|
||||
import {
|
||||
copyFile,
|
||||
readFileAsText,
|
||||
writeTextAsFile,
|
||||
} from "./toolchain-helpers.js";
|
||||
import { toHtml } from "./markdown-parser.js";
|
||||
import { MenuManifest } from "./zblog-toolchain.js";
|
||||
|
||||
type MarkdownLoaderOptions = {
|
||||
notesRootPath: string;
|
||||
noteFileName: string;
|
||||
assetsDirectoryName: string;
|
||||
};
|
||||
|
||||
type MarkdownLoaderManifest = {
|
||||
directoryName: string;
|
||||
name: string;
|
||||
markdown: string;
|
||||
assetDirectoryPath: string;
|
||||
publicAssetDirectoryPath: string;
|
||||
assetFiles: { path: string; name: string }[];
|
||||
}[];
|
||||
|
||||
const createMarkdownLoaderManifest = ({
|
||||
notesRootPath,
|
||||
assetsDirectoryName,
|
||||
noteFileName,
|
||||
}: MarkdownLoaderOptions): MarkdownLoaderManifest => {
|
||||
const directoryNames = fs.readdirSync(notesRootPath);
|
||||
return directoryNames.map((noteDirectory) => {
|
||||
const assetDirectoryPath = `${notesRootPath}/${noteDirectory}/${assetsDirectoryName}`;
|
||||
return {
|
||||
directoryName: noteDirectory,
|
||||
name: noteDirectory,
|
||||
markdown: fs.readFileSync(
|
||||
`${notesRootPath}/${noteDirectory}/${noteFileName}`,
|
||||
{ encoding: "utf-8" }
|
||||
),
|
||||
assetDirectoryPath,
|
||||
publicAssetDirectoryPath: `/${noteDirectory}_`,
|
||||
assetFiles: fs.existsSync(assetDirectoryPath)
|
||||
? fs.readdirSync(assetDirectoryPath).map((name) => ({
|
||||
path: `${assetDirectoryPath}/${name}`,
|
||||
name,
|
||||
}))
|
||||
: [],
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
const copyAssetsToOutputDirectory = (
|
||||
manifest: MarkdownLoaderManifest,
|
||||
outputDirectory: string
|
||||
) => {
|
||||
manifest.forEach((m) =>
|
||||
m.assetFiles.forEach((asset) =>
|
||||
copyFile(asset.path, `${outputDirectory}/${m.name}_${asset.name}`)
|
||||
)
|
||||
);
|
||||
};
|
||||
|
||||
const writeMarkdownAsHtmlToOutputDirectory = (
|
||||
markdownManifest: MarkdownLoaderManifest,
|
||||
noteHtmlTemplate: string,
|
||||
markdownHtmlReplacementTag: string,
|
||||
outputDirectory: string
|
||||
) => {
|
||||
markdownManifest.forEach((m) => {
|
||||
const markdownAsHtml = toHtml(m.markdown, m.publicAssetDirectoryPath);
|
||||
const noteHtmlDocument = noteHtmlTemplate.replace(
|
||||
markdownHtmlReplacementTag,
|
||||
markdownAsHtml
|
||||
);
|
||||
writeTextAsFile(
|
||||
`${outputDirectory}/${m.directoryName}.html`,
|
||||
noteHtmlDocument
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
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);
|
||||
};
|
||||
Reference in New Issue
Block a user