const settings = require('./settings') const fs = require('fs') module.exports = generateSiteMap() /** Generate full site map * * @returns {Object} full site map */ function generateSiteMap(){ return scanDirectory(settings.siteTitle, __dirname + settings.searchDir) } /** Generates a object containing a list of all pages * * @param {String} dir directory source of the pages * @param {Array} files list of the file in the directory * @returns {Object} siteMap list of pages */ function scanPages(dir, files){ var pages = {} for(i=files.length-1; i >= 0; i--){ var file, path, urlName, extention, fileName file = files[i] // full file name path = `${dir}/${file}` // full path to file fileName = file.split('.') extention = fileName.pop() // extention fileName = fileName.join('.') // filename without extention urlName = fileName.toLowerCase().replace(/\ /g, '-') // url friendly file name // skip files called index if(fileName == 'index'){ continue } // if directory if(fs.lstatSync(path).isDirectory()){ pages[urlName] = scanDirectory(file, path) continue } // is file pages[urlName] = { title: fileName, type: extention, file: path } } return pages } /** * * @param {String} dirname directory name * @param {String} path Path to the directory */ function scanDirectory(dirname, path){ var index var files = fs.readdirSync(path); if((index = files.indexOf('index.md')) != -1){ var fileName, extention fileName = files[index].split('.') extention = fileName.pop() fileName = fileName.join('.') index = { title: fileName, type: extention, file: `${path}/${files[index]}` } }else{ index = false } return { title: String (dirname), type: "directory", file: String (path), index: (index) ? index : { title: String (dirname), type: 'index', file: String (path) }, subPages: scanPages(path, files) } }