UNPKG

@mapbox/batfish

Version:

The React-powered static-site generator you didn't know you wanted

85 lines (77 loc) 2.83 kB
// 'use strict'; const _ = require('lodash'); const fs = require('fs'); const path = require('path'); const pify = require('pify'); const pTry = require('p-try'); const globby = require('globby'); const micromatch = require('micromatch'); const grayMatter = require('gray-matter'); const joinUrlParts = require('./join-url-parts'); const constants = require('./constants'); // Get data about pages. Reads the pagesDirectory, figures out URL paths, parses // front matter. // // Return Promise resolves with an object whose keys are page paths and values // are page data. See type definition for BatfishPageData. function getPagesData( batfishConfig ) { return pTry(() => { const base = batfishConfig.siteBasePath; const pagesGlob = [ path.join(batfishConfig.pagesDirectory, `**/*.${constants.PAGE_EXT_GLOB}`) ]; // Convert a page's file path to its URL path. const pageFilePathToUrlPath = (filePath ) => { const relativePath = path.relative( batfishConfig.pagesDirectory, filePath ); if (/^index\.(js|md)$/.test(relativePath)) { if (base === '/') return base; return base + '/'; } if (/\/index\.(js|md)$/.test(relativePath)) { return joinUrlParts(base, path.dirname(relativePath), ''); } return joinUrlParts(base, relativePath.replace(/\.(js|md)$/, ''), ''); }; let pagesData = {}; const registerPage = (filePath ) => { return pify(fs.readFile)(filePath, 'utf8').then((content ) => { const isMarkdown = path.extname(filePath) === '.md'; const grayMatterOptions = isMarkdown ? { delims: ['---', '---'] } : { delims: ['/*---', '---*/'] }; const parsedFrontMatter = grayMatter(content, grayMatterOptions); const published = parsedFrontMatter.data.published !== false; if (!published && batfishConfig.production) return; const pagePath = pageFilePathToUrlPath(filePath); const pageData = { filePath, path: pagePath, frontMatter: parsedFrontMatter.data }; pagesData[pagePath] = pageData; }); }; return globby(pagesGlob) .then(pageFilePaths => { // Filter out any unprocessedPageFiles if (batfishConfig.unprocessedPageFiles) { const unprocessed = micromatch( pageFilePaths, batfishConfig.unprocessedPageFiles ); return _.difference(pageFilePaths, unprocessed); } else { return pageFilePaths; } }) .then(pageFilePaths => Promise.all(pageFilePaths.map(registerPage))) .then(() => pagesData); }); } module.exports = getPagesData;