Skip to content

Commit

Permalink
Make clean URL generation optional (default true)
Browse files Browse the repository at this point in the history
  • Loading branch information
dpogue committed Nov 23, 2018
1 parent 7e0478d commit 58b25be
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
18 changes: 12 additions & 6 deletions lib/modules/build.js
Expand Up @@ -14,7 +14,7 @@ const build = (options = {}) => {
log.info('Building site...');
const startTime = process.hrtime();

const { srcPath, outputPath, site } = parseOptions(options);
const { srcPath, outputPath, cleanUrls, site } = parseOptions(options);

// clear destination folder
fse.emptyDirSync(outputPath);
Expand All @@ -27,7 +27,9 @@ const build = (options = {}) => {
// read pages
const files = glob.sync('**/*.@(md|ejs|html)', { cwd: `${srcPath}/pages` });

files.forEach(file => _buildPage(file, { srcPath, outputPath, site }));
files.forEach(file =>
_buildPage(file, { srcPath, outputPath, cleanUrls, site })
);

// display build time
const timeDiff = process.hrtime(startTime);
Expand All @@ -48,12 +50,12 @@ const _loadLayout = (layout, { srcPath }) => {
/**
* Build a single page
*/
const _buildPage = (file, { srcPath, outputPath, site }) => {
const _buildPage = (file, { srcPath, outputPath, cleanUrls, site }) => {
const fileData = path.parse(file);
let destPath = path.join(outputPath, fileData.dir);

// create extra dir if filename is not index
if (fileData.name !== 'index') {
// create extra dir if generating clean URLs and filename is not index
if (cleanUrls && fileData.name !== 'index') {
destPath = path.join(destPath, fileData.name);
}

Expand Down Expand Up @@ -102,7 +104,11 @@ const _buildPage = (file, { srcPath, outputPath, site }) => {
);

// save the html file
fse.writeFileSync(`${destPath}/index.html`, completePage);
if (cleanUrls) {
fse.writeFileSync(`${destPath}/index.html`, completePage);
} else {
fse.writeFileSync(`${destPath}/${fileData.name}.html`, completePage);
}
};

module.exports = build;
10 changes: 7 additions & 3 deletions lib/utils/parser.js
@@ -1,17 +1,21 @@
const buildDefaults = { srcPath: './src', outputPath: './public' };
const buildDefaults = {
srcPath: './src',
outputPath: './public',
cleanUrls: true
};

/**
* Parse options, setting the defaults on missing values
*/
const parseOptions = options => {
const { srcPath, outputPath } = Object.assign(
const { srcPath, outputPath, cleanUrls } = Object.assign(
{},
buildDefaults,
options.build
);
const site = options.site || {};

return { srcPath, outputPath, site };
return { srcPath, outputPath, cleanUrls, site };
};

module.exports = {
Expand Down

0 comments on commit 58b25be

Please sign in to comment.