Skip to content

Commit

Permalink
Sitemap WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
astronomersiva committed Jul 31, 2018
1 parent cfc63c1 commit fe31a5c
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 30 deletions.
34 changes: 18 additions & 16 deletions lib/lego.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
const Site = require('./site');
const chalk = require('chalk');

const Site = require('./site');
const runTask = require('./utils/runTask');

module.exports = async function(args) {
const site = new Site();
try {
const site = new Site();

const build = [
'cleanBuild',
[
'copyCNAME',
'generatePostsFromMarkdown',
'generatePages',
'generatePagesForTags',
'copyMinorStaticAssets',
'copyMajorStaticAssets'
]
];
const build = [
'cleanBuild',
[
'copyCNAME',
'generatePostsFromMarkdown',
'generatePages',
'generatePagesForTags',
'copyMinorStaticAssets',
'copyMajorStaticAssets'
]
];

const server = [...build, 'startServer'];
const server = [...build, 'startServer'];

const [task] = args;
try {
const [task] = args;
if (['s', 'server'].includes(task)) {
await runTask(server, site);
} else {
await runTask(build, site);
}
} catch (error) {
console.log(chalk.red(`${error.message}\n`));
process.exit(1);
}
}
17 changes: 16 additions & 1 deletion lib/site.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const fs = require('fs-extra');
const path = require('path');
const fs = require('fs-extra');
const findUp = require('find-up');
const markdownIt = require('markdown-it');
const markdownItContainer = require('markdown-it-container');
const frontMatter = require('markdown-it-front-matter');
Expand Down Expand Up @@ -40,6 +41,14 @@ class Site {
this._posts = [];
this._tags = [];

const configFile = findUp.sync('lego.js');
if (!configFile) {
// ignore for now
// throw new Error('lego.js not found. Are you sure this is a lego project?');
}

this._config = require(configFile);

const posts = fs.readdirSync(POSTS);
const filteredPages = filterFileList(posts, 'md');

Expand Down Expand Up @@ -67,6 +76,12 @@ class Site {
// This causes pages to break. As a workaround, pass a new copy each time.
return [...this._tags];
}

// node-liquid is directly manipulating the params passed.
// This causes pages to break. As a workaround, pass a new copy each time.
getConfig() {
return JSON.parse(JSON.stringify(this._config));
}
}

module.exports = Site;
18 changes: 18 additions & 0 deletions lib/tasks/generateSiteMap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const fs = require('fs-extra');
const sitemap = require('sitemap');

const { BUILD } = require('../utils/constants');

module.exports = async function(site) {
try {
let siteConfig = site.getConfig();
let options = {
hostname: siteConfig.domain
};

const siteMapForSite = sitemap.createSitemap({ options });
fs.writeFileSync(`${BUILD}/sitemap.xml`, siteMapForSite.toString());
} catch (error) {
throw error;
}
}
92 changes: 79 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"autoprefixer": "^9.0.1",
"chalk": "^2.4.1",
"cssnano": "^4.0.4",
"find-up": "^3.0.0",
"fs-extra": "^7.0.0",
"glob": "^7.1.2",
"html-minifier": "^3.5.19",
Expand All @@ -40,6 +41,7 @@
"markdown-it-container": "^2.0.0",
"markdown-it-front-matter": "^0.1.2",
"postcss": "^7.0.1",
"sitemap": "^1.13.0",
"uglify-es": "^3.3.9"
}
}

0 comments on commit fe31a5c

Please sign in to comment.