Skip to content

Commit

Permalink
Fix watcher for nested posts
Browse files Browse the repository at this point in the history
  • Loading branch information
astronomersiva committed Dec 23, 2018
1 parent 638b3b1 commit c827139
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
16 changes: 12 additions & 4 deletions lib/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ class Site {
for (const post of filteredPages) {
let pageContents = fs.readFileSync(`${POSTS}/${post}`).toString();
let { html, meta } = generateHtmlFromMd(pageContents);
let parsedPath = path.parse(post);
let urlFromPath = this._config.skipDirsInPostUrls ? parsedPath.name : path.join(parsedPath.dir, parsedPath.name);
let postPath = meta.url ? meta.url : urlFromPath;
let postPath = this.getPathForPost(post, meta);
let postData = Object.assign({
path: postPath,
html
Expand Down Expand Up @@ -85,6 +83,15 @@ class Site {
this._data = loadData(this.logger);
}

getPathForPost(post, meta) {
let config = this.getConfig();
let parsedPath = path.parse(post);
let urlFromPath = config.skipDirsInPostUrls ? parsedPath.name : path.join(parsedPath.dir, parsedPath.name);
let postPath = meta.url ? meta.url : urlFromPath;

return postPath;
}

async handlePostChange(event, post) {
let postName = path.parse(post).name;
if (event === 'unlink') {
Expand All @@ -97,8 +104,9 @@ class Site {
if (['add', 'change'].includes(event)) {
let pageContents = fs.readFileSync(post).toString();
let { html, meta } = generateHtmlFromMd(pageContents);
let postPath = this.getPathForPost(post, meta);
let postData = Object.assign({
path: postName,
path: postPath,
html
}, meta);

Expand Down
2 changes: 1 addition & 1 deletion lib/utils/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = function(site, runTask) {
ignoreInitial: true
};

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

postsWatcher.on('all', async(event, path) => {
await site.handlePostChange(event, path);
Expand Down

0 comments on commit c827139

Please sign in to comment.