Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow loading yaml files as data #12

Merged
merged 1 commit into from
Sep 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ This was a great learning experience to be honest.
│   ├── 404.html
│   ├── about.html // each of these will be put under a separate folder in build
│   └── index.html
├── data
│   ├── authors.yml
│   └── speakers.yaml // will be available as data.authors and data.speakers
├── posts
│   ├── post.md
│   ├── another-post.md
Expand Down
3 changes: 3 additions & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
│ ├── 404.html
│ ├── about.html // each of these will be put under a separate folder in build
│ └── index.html
├── data
│   ├── authors.yml
│   └── speakers.yaml // will be available as data.authors and data.speakers
├── posts
│ ├── post.md
│ ├── another-post.md
Expand Down
3 changes: 3 additions & 0 deletions docs/index/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
│ ├── 404.html
│ ├── about.html // each of these will be put under a separate folder in build
│ └── index.html
├── data
│   ├── authors.yml
│   └── speakers.yaml // will be available as data.authors and data.speakers
├── posts
│ ├── post.md
│ ├── another-post.md
Expand Down
25 changes: 18 additions & 7 deletions lib/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ const {Signale} = require('signale');
const cleanBuild = require('./utils/cleanBuild');
const filterFileList = require('./utils/filterFileList');
const generateHtmlFromMd = require('./utils/generateHtmlFromMd');
const loadData = require('./utils/loadData');

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

class Site {
constructor() {
this._posts = [];
this._data = [];
this._tags = [];
this._assets = {};
this.logger = new Signale({ interactive: true });
Expand All @@ -19,13 +22,16 @@ class Site {
this.logger = {
success: consoleLog,
await: consoleLog,
watch: consoleLog
watch: consoleLog,
fatal: consoleLog
}
}

cleanBuild(this);
this.logger.await('Preparing site');

this.loadData();

// let configFile = findUp.sync('lego.js');
// if (!configFile) {
// throw new Error('lego.js not found. Are you sure this is a lego project?');
Expand Down Expand Up @@ -61,6 +67,10 @@ class Site {
this._tags = [...tags];
}

loadData() {
this._data = loadData(this.logger);
}

async handlePostChange(event, post) {
let postName = path.parse(post).name;
if (event === 'unlink') {
Expand Down Expand Up @@ -91,24 +101,25 @@ class Site {
}
}

// node-liquid is directly manipulating the params passed.
// This causes pages to break. As a workaround, pass a new copy each time.

getPosts() {
// node-liquid is directly manipulating the params passed.
// This causes pages to break. As a workaround, pass a new copy each time.
return [...this._posts];
}

getTags() {
// node-liquid is directly manipulating the params passed.
// 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 {...this._config};
}

getData() {
return {...this._data};
}

updateAsset(asset, revisedAsset) {
this._assets[asset] = revisedAsset;
}
Expand Down
5 changes: 3 additions & 2 deletions lib/tasks/generatePages.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = async function(site) {
site.logger.await(message);

let posts = site.getPosts();
let data = site.getData();
let pages = fs.readdirSync(PAGES);
let filteredPages = filterFileList(pages, 'html')
.filter(page => page !== '404.html');
Expand All @@ -20,7 +21,7 @@ module.exports = async function(site) {
let htmlPath = `${BUILD}/${path.parse(page).name}`;
fs.mkdirp(htmlPath);

let renderedHtml = await generateHtmlFromLiquid(pageContents, { posts });
let renderedHtml = await generateHtmlFromLiquid(pageContents, { posts, data });
fs.writeFileSync(`${htmlPath}/index.html`, renderedHtml);
}

Expand All @@ -29,7 +30,7 @@ module.exports = async function(site) {

// generate 404 page
let pageContents = fs.readFileSync(`${PAGES}/404.html`).toString();
let renderedHtml = await generateHtmlFromLiquid(pageContents, { posts });
let renderedHtml = await generateHtmlFromLiquid(pageContents, { posts, data });
fs.writeFileSync(`${BUILD}/404.html`, renderedHtml);

site.logger.success(message);
Expand Down
3 changes: 2 additions & 1 deletion lib/tasks/generatePagesForTags.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ module.exports = async function(site) {
let baseTemplate = fs.readFileSync(`${LAYOUTS}/tag.html`).toString();
let tags = [...new Set(site.getTags())];
let posts = site.getPosts();
let data = site.getData();

for (const tag of tags) {
let htmlPath = `${BUILD}/tag/${tag}`;
fs.mkdirp(htmlPath);

let renderedHtml = await generateHtmlFromLiquid(baseTemplate, { posts, tag });
let renderedHtml = await generateHtmlFromLiquid(baseTemplate, { posts, tag, data });
fs.writeFileSync(`${htmlPath}/index.html`, renderedHtml);

site.logger.success(message);
Expand Down
3 changes: 2 additions & 1 deletion lib/tasks/generatePostsFromMarkdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module.exports = async function(site) {

let baseTemplate = fs.readFileSync(`${LAYOUTS}/post.html`).toString();
let posts = site.getPosts();
let data = site.getData();
for (const post of posts) {
let htmlPath = `${BUILD}/${post.path}`;
fs.mkdirp(htmlPath);
Expand All @@ -18,7 +19,7 @@ module.exports = async function(site) {
? fs.readFileSync(`${LAYOUTS}/${post.layout}`).toString()
: baseTemplate;

let renderedHtml = await generateHtmlFromLiquid(template, { post });
let renderedHtml = await generateHtmlFromLiquid(template, { post, data });
fs.writeFileSync(`${htmlPath}/index.html`, renderedHtml);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/startServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = function() {
root: BUILD,
open: true,
wait: 500,
logLevel: 1
logLevel: 0
};

liveServer.start(params);
Expand Down
1 change: 1 addition & 0 deletions lib/utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module.exports = {
POSTS: 'posts',
STATIC: 'static',
LAYOUTS: 'layouts',
DATA: 'data',
MEDIA: [
'webp',
'png',
Expand Down
4 changes: 3 additions & 1 deletion lib/utils/filterFileList.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ const extensions = {
js: '.js',
css: '.css',
md: '.md',
html: '.html'
html: '.html',
yml: '.yml',
yaml: '.yaml'
};

Object.assign(extensions, media);
Expand Down
28 changes: 28 additions & 0 deletions lib/utils/loadData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module.exports = function(logger) {
const fs = require('fs-extra');
const path = require('path');
const chalk = require('chalk');
const YAML = require('yaml').default;

const filterFileList = require('./filterFileList');
const { DATA } = require('./constants');

if (!fs.existsSync(DATA)) {
return {};
}

let files = fs.readdirSync(DATA);
let ymlFiles = filterFileList(files, ['yml', 'yaml']);
let data = {};

ymlFiles.map((yml) => {
let fileContents = fs.readFileSync(`${DATA}/${yml}`).toString();
try {
data[path.parse(yml).name] = YAML.parse(fileContents);
} catch (error) {
logger.fatal(`YAML error at ${yml} \n\n${chalk.gray(error.source.toString())}`);
}
});

return data;
}
49 changes: 34 additions & 15 deletions lib/utils/watch.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
module.exports = function(site, runTask) {
const fs = require('fs-extra');
const chokidar = require('chokidar');
const { STATIC } = require('./constants');
const {
STATIC,
PAGES,
LAYOUTS,
DATA,
POSTS
} = require('./constants');

site.logger.watch('Watching for changes');

let postsWatcher = chokidar.watch(['posts/*md', 'posts/*html', 'pages/*html'], {
let watchOptions = {
ignored: /(^|[/\\])\../,
ignoreInitial: true
});
};

let postsWatcher = chokidar.watch([`${POSTS}/*md`, `${POSTS}/*html`, `${PAGES}/*html`, `${DATA}/*yml`], watchOptions);

postsWatcher.on('all', async(event, path) => {
await site.handlePostChange(event, path);
Expand All @@ -19,10 +28,26 @@ module.exports = function(site, runTask) {
], site);
});

let layoutsWatcher = chokidar.watch('layouts/*html', {
ignored: /(^|[/\\])\../,
ignoreInitial: true
});
let dataWatcher = chokidar.watch(`${DATA}/*yml`, `${DATA}/*yaml`);
dataWatcher.on('all', (event, path) => {
if (event === 'add') {
let size = fs.statSync(path).size;
if (size === 0) {
return;
}
}

site.loadData();

// Need to further refine this to only rebuild changed files
runTask([
'generatePostsFromMarkdown',
'generatePages',
'generatePagesForTags'
], site);
})

let layoutsWatcher = chokidar.watch(`${LAYOUTS}/*html`, watchOptions);

layoutsWatcher.on('all', () => {
// Need to further refine this to only rebuild changed files
Expand All @@ -33,20 +58,14 @@ module.exports = function(site, runTask) {
], site);
});

let pagesWatcher = chokidar.watch('pages/*html', {
ignored: /(^|[/\\])\../,
ignoreInitial: true
});
let pagesWatcher = chokidar.watch(`${PAGES}/*html`, watchOptions);

pagesWatcher.on('all', () => {
// Need to further refine this to only rebuild changed files
runTask('generatePages', site);
});

let majorAssetsWatcher = chokidar.watch([`${STATIC}/css/**/*css`, `${STATIC}/js/**/*js`], {
ignored: /(^|[/\\])\../,
ignoreInitial: true
});
let majorAssetsWatcher = chokidar.watch([`${STATIC}/css/**/*css`, `${STATIC}/js/**/*js`], watchOptions);

majorAssetsWatcher.on('all', async(event, path) => {
// Need to further refine this to only rebuild changed files
Expand Down
12 changes: 6 additions & 6 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@astronomersiva/lego",
"version": "0.0.7",
"version": "0.0.8",
"description": "A custom built static site generator that will one day power [sivasubramanyam.me](https://sivasubramanyam.me) 🏋️‍",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -52,6 +52,6 @@
"signale": "^1.2.1",
"sitemap": "^1.13.0",
"uglify-es": "^3.3.9",
"uninstall": "0.0.0"
"yaml": "^1.0.0-rc.8"
}
}