// Name: Combine Markdown
// Description: Combines markdown files with supporting resources.
// Author: tayiorbeii
import { readFile, writeFile } from 'node:fs/promises';
const selectedFile = await path({
message: "Select a markdown file (e.g., -part01.md)",
onlyFiles: true,
});
if (!selectedFile) {
console.error("No file selected.");
process.exit(1);
}
const baseName = path.basename(selectedFile, ".md");
const dir = path.dirname(selectedFile);
const partFile = `${baseName}.md`;
const supportingFile = `${baseName}-supporting-prompts.md`;
const contrarianFile = `${baseName}-contrarian.md`;
const partContent = await readFile(path.join(dir, partFile), "utf-8");
let supportingContent = "";
// Check if the supporting file exists
if(await pathExists(path.join(dir, supportingFile))){
const fileContent = await readFile(path.join(dir, supportingFile), "utf-8");
const aiResponseIndex = fileContent.indexOf("# AI Response");
// If "# AI Response" is found, extract the content after it; otherwise, use the entire content.
supportingContent =
aiResponseIndex !== -1
? fileContent.substring(aiResponseIndex + "# AI Response".length).trim()
: fileContent;
}
let contrarianContent = "";
// Check if the contrarian file exists
if(await pathExists(path.join(dir, contrarianFile))){
contrarianContent = await readFile(path.join(dir, contrarianFile), "utf-8");}
const combinedContent = `${partContent}
# Supporting Resources
${supportingContent}
# Contrarian Resources
${contrarianContent}
`;
const outputFileName = `${baseName}-combined.md`;
const outputPath = path.join(dir, outputFileName);
await writeFile(outputPath, combinedContent);
await edit(outputPath);