const fs = require('fs'); const marked = require('marked'); // files to read and process const files = [ 'README.md', 'BUILDING.md', 'CONFIGURATION.md', 'FUNCTIONS.md', 'BUILTINS.md', 'EXTERNAL.md' ]; // template var template = fs.readFileSync('./index.tpl', 'utf8'); // links var links = [ ]; // Override function const renderer = { heading (text, level) { const escapedText = text.toLowerCase().replace(/[^\w]+/g, '-'); links.push({ level: level, link: '#' + escapedText, text: text }); return ` ${text} `; } }; marked.use({ renderer }); // create the content var content = ''; files.forEach((elem) => { const input = fs.readFileSync('../../docs/' + elem, 'utf8'); content += marked.parse(input); }); // replace the content of the template template = template.replace('%CONTENT%', () => content); // create the links for the sidebar var level = 0; var sidebar = ''; links.forEach((elem) => { if (elem.level > level) { level++; sidebar += '\n'; } sidebar += `
  • ${elem.text}
  • \n`; }); // replace the content of the sidebar template = template.replace('%SIDEBAR%', () => sidebar); // write the output fs.writeFileSync('index.html', template, 'utf8');