c4dslbuilder
Version:
A CLI tool designed to compile a folder structure of markdowns and mermaid files into a site, pdf, single file markdown or a collection of markdowns with links - inspired by c4builder
39 lines (38 loc) • 1.64 kB
JavaScript
import path from 'path';
import chalk from 'chalk';
import { SafeFiles } from './safe-files.js';
import { CliLogger } from './cli-logger.js';
import { OutputType, ProcessorBase } from './processor-base.js';
import { MermaidProcessor } from './mermaid-processor.js';
export class MarkdownProcessor extends ProcessorBase {
safeFiles;
logger;
mermaid;
constructor(safeFiles = new SafeFiles(), logger = new CliLogger(MarkdownProcessor.name), mermaid = new MermaidProcessor()) {
super(safeFiles, logger, mermaid);
this.safeFiles = safeFiles;
this.logger = logger;
this.mermaid = mermaid;
}
async generateMarkdownFromTree(tree, buildConfig) {
let MD = this.generateDocumentHeader(tree, buildConfig);
MD += await this.buildDocumentBody(tree, buildConfig);
const outPath = path.join(buildConfig.distFolder, `${buildConfig.projectName}.md`);
try {
await this.safeFiles.writeFile(outPath, MD);
this.logger.info(`Wrote ${buildConfig.projectName}.md to ${outPath}`);
}
catch (err) {
this.logger.error(`Failed to write ${buildConfig.projectName}.md`, err);
}
}
async prepareMarkdown(buildConfig) {
if (!(await this.prepareOutputFolder(OutputType.md, buildConfig))) {
this.logger.warn('Output folder preparation failed.');
return;
}
const tree = await this.generateSourceTree(buildConfig);
await this.generateMarkdownFromTree(tree, buildConfig);
this.logger.log(chalk.green(`\nMarkdown documentation generated successfully!`));
}
}