const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
// Ensure marked and highlight.js are installed
try {
require.resolve('marked');
require.resolve('highlight.js');
} catch (e) {
console.log("Installing marked and highlight.js...");
execSync('npm install marked highlight.js', { stdio: 'inherit' });
}
const { marked } = require('marked');
const hljs = require('highlight.js');
marked.setOptions({
highlight: function(code, lang) {
const language = hljs.getLanguage(lang) ? lang : 'plaintext';
return hljs.highlight(code, { language }).value;
},
langPrefix: 'hljs language-',
gfm: true,
breaks: true
});
const docsDir = __dirname;
const indexFile = path.join(docsDir, 'index.html');
// Read the viewer template
const template = `
{{TITLE}} - Siro Docs
{{CONTENT}}
`;
function traverseDir(dir) {
const files = fs.readdirSync(dir);
files.forEach(file => {
const fullPath = path.join(dir, file);
if (fs.statSync(fullPath).isDirectory()) {
traverseDir(fullPath);
} else if (fullPath.endsWith('.md') && !fullPath.includes('node_modules')) {
console.log(`Processing: ${fullPath}`);
const markdown = fs.readFileSync(fullPath, 'utf8');
const htmlContent = marked.parse(markdown);
const title = path.basename(fullPath);
const finalHtml = template
.replace(/{{TITLE}}/g, title)
.replace('{{CONTENT}}', htmlContent);
const htmlPath = fullPath.replace(/\.md$/, '.html');
fs.writeFileSync(htmlPath, finalHtml);
console.log(`Generated: ${htmlPath}`);
}
});
}
// 1. Generate HTML files for all MD files
const dirsToProcess = ['01_overview', '02_journeys_and_tutorials', '03_pricing', '04_features', '05_transit_mawasalati', '06_investors', '07_marketing', '08_security', '10_food_orders'];
dirsToProcess.forEach(dir => {
const fullPath = path.join(docsDir, dir);
if (fs.existsSync(fullPath)) {
traverseDir(fullPath);
}
});
// 2. Update index.html to point to .html files instead of viewer.html?doc=...
let indexHtml = fs.readFileSync(indexFile, 'utf8');
indexHtml = indexHtml.replace(/href="viewer\.html\?doc=([^"]+)\.md"/g, 'href="$1.html"');
fs.writeFileSync(indexFile, indexHtml);
console.log('Updated index.html links to point to static .html files.');
// 3. Remove viewer.html since it's no longer needed
const viewerHtml = path.join(docsDir, 'viewer.html');
if (fs.existsSync(viewerHtml)) {
fs.unlinkSync(viewerHtml);
console.log('Deleted viewer.html');
}
console.log('Build complete!');