From 021ddb3a13694c7a4097829bcdd08e4b97f13a5b Mon Sep 17 00:00:00 2001 From: jonahtanjz Date: Sat, 13 Mar 2021 19:37:46 +0800 Subject: [PATCH 01/12] Reload site.json on change --- packages/cli/index.js | 3 +++ packages/core/src/Site/index.js | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/packages/cli/index.js b/packages/cli/index.js index aac678a853..83b82b88df 100755 --- a/packages/cli/index.js +++ b/packages/cli/index.js @@ -152,6 +152,9 @@ program const changeHandler = (filePath) => { logger.info(`[${new Date().toLocaleTimeString()}] Reload for file change: ${filePath}`); Promise.resolve('').then(() => { + if (filePath.replace(/^.*[\\\/]/, '') === "site.json") { + return site.reloadSiteConfig(); + } if (site.isDependencyOfPage(filePath)) { return site.rebuildAffectedSourceFiles(filePath); } diff --git a/packages/core/src/Site/index.js b/packages/core/src/Site/index.js index 19e6c06942..8f3ddae061 100644 --- a/packages/core/src/Site/index.js +++ b/packages/core/src/Site/index.js @@ -816,6 +816,11 @@ class Site { } } + async reloadSiteConfig() { + await this.readSiteConfig(); + await this._rebuildSourceFiles(); + } + /** * Checks if a specified file path is a dependency of a page * @param {string} filePath file path to check From 488f369b99928947a48af3c3228b5388452aae71 Mon Sep 17 00:00:00 2001 From: jonahtanjz Date: Sun, 14 Mar 2021 00:16:56 +0800 Subject: [PATCH 02/12] Check for updated pages --- packages/cli/index.js | 5 +++-- packages/core/src/Site/index.js | 7 ++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/cli/index.js b/packages/cli/index.js index 83b82b88df..d20b0e6698 100755 --- a/packages/cli/index.js +++ b/packages/cli/index.js @@ -18,6 +18,7 @@ const { INDEX_MARKDOWN_FILE, INDEX_MARKBIND_FILE, LAZY_LOADING_SITE_FILE_NAME, + SITE_CONFIG_NAME, } = require('@markbind/core/src/Site/constants'); const cliUtil = require('./src/util/cliUtil'); @@ -152,8 +153,8 @@ program const changeHandler = (filePath) => { logger.info(`[${new Date().toLocaleTimeString()}] Reload for file change: ${filePath}`); Promise.resolve('').then(() => { - if (filePath.replace(/^.*[\\\/]/, '') === "site.json") { - return site.reloadSiteConfig(); + if (filePath.replace(/^.*[\\\/]/, '') === SITE_CONFIG_NAME) { + return site.reloadSiteConfig(filePath); } if (site.isDependencyOfPage(filePath)) { return site.rebuildAffectedSourceFiles(filePath); diff --git a/packages/core/src/Site/index.js b/packages/core/src/Site/index.js index 8f3ddae061..50a5cc87e0 100644 --- a/packages/core/src/Site/index.js +++ b/packages/core/src/Site/index.js @@ -34,6 +34,7 @@ _.difference = require('lodash/difference'); _.flatMap = require('lodash/flatMap'); _.has = require('lodash/has'); _.isBoolean = require('lodash/isBoolean'); +_.isEqual = require('lodash/isEqual'); _.isUndefined = require('lodash/isUndefined'); _.noop = require('lodash/noop'); _.omitBy = require('lodash/omitBy'); @@ -817,8 +818,12 @@ class Site { } async reloadSiteConfig() { + const oldSiteConfig = this.siteConfig; await this.readSiteConfig(); - await this._rebuildSourceFiles(); + if (!_.isEqual(oldSiteConfig.pages, this.siteConfig.pages)) { + await this.rebuildSourceFiles(); + await this.writeSiteData(); + } } /** From 7c087159a3f3e456f90f332c1fb7302f7d20eaa2 Mon Sep 17 00:00:00 2001 From: jonahtanjz Date: Sun, 14 Mar 2021 00:20:51 +0800 Subject: [PATCH 03/12] Lint fix --- packages/cli/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cli/index.js b/packages/cli/index.js index d20b0e6698..a2b2d322fe 100755 --- a/packages/cli/index.js +++ b/packages/cli/index.js @@ -153,7 +153,7 @@ program const changeHandler = (filePath) => { logger.info(`[${new Date().toLocaleTimeString()}] Reload for file change: ${filePath}`); Promise.resolve('').then(() => { - if (filePath.replace(/^.*[\\\/]/, '') === SITE_CONFIG_NAME) { + if (filePath.replace(/^.*[\\/]/, '') === SITE_CONFIG_NAME) { return site.reloadSiteConfig(filePath); } if (site.isDependencyOfPage(filePath)) { From d9dfe496fe27cc390ed4047b7cd988a184aab18b Mon Sep 17 00:00:00 2001 From: jonahtanjz Date: Mon, 15 Mar 2021 20:15:22 +0800 Subject: [PATCH 04/12] Support single page rebuild --- packages/cli/index.js | 2 +- packages/core/src/Site/index.js | 71 +++++++++++++++++++++++++++++++-- 2 files changed, 69 insertions(+), 4 deletions(-) diff --git a/packages/cli/index.js b/packages/cli/index.js index a2b2d322fe..9013375830 100755 --- a/packages/cli/index.js +++ b/packages/cli/index.js @@ -154,7 +154,7 @@ program logger.info(`[${new Date().toLocaleTimeString()}] Reload for file change: ${filePath}`); Promise.resolve('').then(() => { if (filePath.replace(/^.*[\\/]/, '') === SITE_CONFIG_NAME) { - return site.reloadSiteConfig(filePath); + return site.reloadSiteConfig(); } if (site.isDependencyOfPage(filePath)) { return site.rebuildAffectedSourceFiles(filePath); diff --git a/packages/core/src/Site/index.js b/packages/core/src/Site/index.js index 50a5cc87e0..0185cd6708 100644 --- a/packages/core/src/Site/index.js +++ b/packages/core/src/Site/index.js @@ -31,9 +31,11 @@ const { const _ = {}; _.difference = require('lodash/difference'); +_.differenceWith = require('lodash/differenceWith'); _.flatMap = require('lodash/flatMap'); _.has = require('lodash/has'); _.isBoolean = require('lodash/isBoolean'); +_.isEmpty = require('lodash/isEmpty'); _.isEqual = require('lodash/isEqual'); _.isUndefined = require('lodash/isUndefined'); _.noop = require('lodash/noop'); @@ -819,11 +821,75 @@ class Site { async reloadSiteConfig() { const oldSiteConfig = this.siteConfig; + const oldPagesSrc = oldSiteConfig.pages.slice().map(page => page.src); await this.readSiteConfig(); - if (!_.isEqual(oldSiteConfig.pages, this.siteConfig.pages)) { + // Get pages with edited attributes but with the same src + const editedPages = _.differenceWith(this.siteConfig.pages, oldSiteConfig.pages, function (newPage, oldPage) { + if (!_.isEqual(newPage, oldPage)) { + return !oldPagesSrc.includes(newPage.src); + } else { + return true; + } + }); + const addedPages = _.differenceWith(this.siteConfig.pages, oldSiteConfig.pages, this.isNewPage); + const removedPages = _.differenceWith(oldSiteConfig.pages, this.siteConfig.pages, this.isNewPage); + if (!_.isEmpty(addedPages) || !_.isEmpty(removedPages)) { await this.rebuildSourceFiles(); - await this.writeSiteData(); + } else { + this.updateAddressablePages(); + const editedGlob = editedPages.filter(page => page.glob); + if (editedGlob) { + this.updateGlobPages(editedGlob); + } + this.updatePages(editedPages); + const siteConfigDirectory = path.dirname(path.join(this.rootPath, this.siteConfigPath)); + this.rebuildAffectedSourceFiles(editedPages.map(page => path.join(siteConfigDirectory, page.src))); } + await this.writeSiteData(); + } + + /** + * Updates the pageConfig attributes of the pages that have been edited + */ + updatePages(pagesToUpdate) { + pagesToUpdate.forEach(pageToUpdate => { + const pageAttributes = Object.keys(pageToUpdate); + this.pages.forEach(page => { + if (page.pageConfig.src === pageToUpdate.src) { + pageAttributes.forEach(pageAttribute => { + if (!pageToUpdate.pageConfig[globAttribute]) { + return; + } + page.pageConfig[pageAttribute] = pageToUpdate[pageAttribute]; + }); + } + }); + }); + } + + /** + * Updates the pageConfig attributes of the pages with the new glob attributes + */ + updateGlobPages(globToUpdate) { + const pagesToUpdate = this.pages.filter(page => { + return !this.addressablePages.some(addressablePage => addressablePage.src === page.pageConfig.src); + }); + const globAttributes = Object.keys(globToUpdate) + pagesToUpdate.forEach(pageToUpdate => { + globAttributes.forEach(globAttribute => { + if (!pageToUpdate.pageConfig[globAttribute]) { + return; + } + pageToUpdate.pageConfig[globAttribute] = globToUpdate[globAttribute]; + }); + }); + } + + /** + * Helper function for reloadSiteConfig() + */ + isNewPage(newPage, oldPage) { + return _.isEqual(newPage, oldPage) || newPage.src === oldPage.src; } /** @@ -948,7 +1014,6 @@ class Site { this._setTimestampVariable(); this.mapAddressablePagesToPages(addressablePages, faviconUrl); - return this.generatePagesThrottled(this.pages); } From 47af7a9d0874e9e70b50fdfd4b1d43d985e4be60 Mon Sep 17 00:00:00 2001 From: jonahtanjz Date: Mon, 15 Mar 2021 21:44:11 +0800 Subject: [PATCH 05/12] Create new pages for edited pages --- packages/core/src/Site/index.js | 79 +++++++++++++-------------------- 1 file changed, 30 insertions(+), 49 deletions(-) diff --git a/packages/core/src/Site/index.js b/packages/core/src/Site/index.js index 0185cd6708..147de9c154 100644 --- a/packages/core/src/Site/index.js +++ b/packages/core/src/Site/index.js @@ -820,78 +820,59 @@ class Site { } async reloadSiteConfig() { - const oldSiteConfig = this.siteConfig; - const oldPagesSrc = oldSiteConfig.pages.slice().map(page => page.src); + const oldAddressablePages = this.addressablePages.slice(); + const oldPagesSrc = oldAddressablePages.map(page => page.src); await this.readSiteConfig(); - // Get pages with edited attributes but with the same src - const editedPages = _.differenceWith(this.siteConfig.pages, oldSiteConfig.pages, function (newPage, oldPage) { + this.updateAddressablePages(); + // Get pages with edited attributes but with the same src + const editedPages = _.differenceWith(this.addressablePages, oldAddressablePages, (newPage, oldPage) => { if (!_.isEqual(newPage, oldPage)) { - return !oldPagesSrc.includes(newPage.src); - } else { - return true; + if (newPage.glob) { + return !oldPagesSrc.includes(newPage.glob); + } + return !oldPagesSrc.includes(newPage.src); } + return true; }); - const addedPages = _.differenceWith(this.siteConfig.pages, oldSiteConfig.pages, this.isNewPage); - const removedPages = _.differenceWith(oldSiteConfig.pages, this.siteConfig.pages, this.isNewPage); + + // Comparator for the _differenceWith comparison below + const isNewPage = (newPage, oldPage) => _.isEqual(newPage, oldPage) || newPage.src === oldPage.src; + + const addedPages = _.differenceWith(this.addressablePages, oldAddressablePages, isNewPage); + const removedPages = _.differenceWith(oldAddressablePages, this.addressablePages, isNewPage); if (!_.isEmpty(addedPages) || !_.isEmpty(removedPages)) { await this.rebuildSourceFiles(); + await this.writeSiteData(); } else { - this.updateAddressablePages(); - const editedGlob = editedPages.filter(page => page.glob); - if (editedGlob) { - this.updateGlobPages(editedGlob); - } this.updatePages(editedPages); const siteConfigDirectory = path.dirname(path.join(this.rootPath, this.siteConfigPath)); this.rebuildAffectedSourceFiles(editedPages.map(page => path.join(siteConfigDirectory, page.src))); } - await this.writeSiteData(); } /** - * Updates the pageConfig attributes of the pages that have been edited + * Updates the pageConfig of the pages that have been edited */ updatePages(pagesToUpdate) { - pagesToUpdate.forEach(pageToUpdate => { - const pageAttributes = Object.keys(pageToUpdate); - this.pages.forEach(page => { + pagesToUpdate.forEach((pageToUpdate) => { + this.pages.forEach((page, index) => { if (page.pageConfig.src === pageToUpdate.src) { - pageAttributes.forEach(pageAttribute => { - if (!pageToUpdate.pageConfig[globAttribute]) { - return; - } - page.pageConfig[pageAttribute] = pageToUpdate[pageAttribute]; + const newPage = this.createPage({ + faviconUrl: this.getFavIconUrl(), + pageSrc: pageToUpdate.src, + title: pageToUpdate.title, + layout: pageToUpdate.layout, + frontmatter: pageToUpdate.frontmatter, + searchable: pageToUpdate.searchable !== 'no', + externalScripts: pageToUpdate.externalScripts, }); + newPage.resetState(); + this.pages[index] = newPage; } }); }); } - /** - * Updates the pageConfig attributes of the pages with the new glob attributes - */ - updateGlobPages(globToUpdate) { - const pagesToUpdate = this.pages.filter(page => { - return !this.addressablePages.some(addressablePage => addressablePage.src === page.pageConfig.src); - }); - const globAttributes = Object.keys(globToUpdate) - pagesToUpdate.forEach(pageToUpdate => { - globAttributes.forEach(globAttribute => { - if (!pageToUpdate.pageConfig[globAttribute]) { - return; - } - pageToUpdate.pageConfig[globAttribute] = globToUpdate[globAttribute]; - }); - }); - } - - /** - * Helper function for reloadSiteConfig() - */ - isNewPage(newPage, oldPage) { - return _.isEqual(newPage, oldPage) || newPage.src === oldPage.src; - } - /** * Checks if a specified file path is a dependency of a page * @param {string} filePath file path to check From 342e056d28c7ed24837d10f153310703c725dda1 Mon Sep 17 00:00:00 2001 From: jonahtanjz Date: Mon, 15 Mar 2021 21:45:49 +0800 Subject: [PATCH 06/12] Update comment --- packages/core/src/Site/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/Site/index.js b/packages/core/src/Site/index.js index 147de9c154..f3d022edcf 100644 --- a/packages/core/src/Site/index.js +++ b/packages/core/src/Site/index.js @@ -851,7 +851,7 @@ class Site { } /** - * Updates the pageConfig of the pages that have been edited + * Creates new pages and replaces the original pages with the updated version */ updatePages(pagesToUpdate) { pagesToUpdate.forEach((pageToUpdate) => { From 02d9b45cf3530fd9822b63fc14fd0e345da8e727 Mon Sep 17 00:00:00 2001 From: jonahtanjz Date: Mon, 15 Mar 2021 21:59:34 +0800 Subject: [PATCH 07/12] Delete removed pages --- packages/core/src/Site/index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/core/src/Site/index.js b/packages/core/src/Site/index.js index f3d022edcf..f952038398 100644 --- a/packages/core/src/Site/index.js +++ b/packages/core/src/Site/index.js @@ -839,8 +839,10 @@ class Site { const isNewPage = (newPage, oldPage) => _.isEqual(newPage, oldPage) || newPage.src === oldPage.src; const addedPages = _.differenceWith(this.addressablePages, oldAddressablePages, isNewPage); - const removedPages = _.differenceWith(oldAddressablePages, this.addressablePages, isNewPage); + const removedPages = _.differenceWith(oldAddressablePages, this.addressablePages, isNewPage) + .map(filePath => Site.setExtension(filePath.src, '.html')); if (!_.isEmpty(addedPages) || !_.isEmpty(removedPages)) { + await this.removeAsset(removedPages); await this.rebuildSourceFiles(); await this.writeSiteData(); } else { From ddb7d5c852042d83b50c9678cbab9f59bae2f191 Mon Sep 17 00:00:00 2001 From: jonahtanjz Date: Mon, 15 Mar 2021 22:22:53 +0800 Subject: [PATCH 08/12] Remove unused glob --- packages/core/src/Site/index.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/packages/core/src/Site/index.js b/packages/core/src/Site/index.js index f952038398..fb37a0bdf2 100644 --- a/packages/core/src/Site/index.js +++ b/packages/core/src/Site/index.js @@ -827,9 +827,6 @@ class Site { // Get pages with edited attributes but with the same src const editedPages = _.differenceWith(this.addressablePages, oldAddressablePages, (newPage, oldPage) => { if (!_.isEqual(newPage, oldPage)) { - if (newPage.glob) { - return !oldPagesSrc.includes(newPage.glob); - } return !oldPagesSrc.includes(newPage.src); } return true; From 2f5e3c32f51a21d061c3c594b8a28eb13bdce83f Mon Sep 17 00:00:00 2001 From: jonahtanjz Date: Tue, 16 Mar 2021 14:32:10 +0800 Subject: [PATCH 09/12] Update after review --- packages/cli/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cli/index.js b/packages/cli/index.js index 9013375830..2145f5a19e 100755 --- a/packages/cli/index.js +++ b/packages/cli/index.js @@ -153,7 +153,7 @@ program const changeHandler = (filePath) => { logger.info(`[${new Date().toLocaleTimeString()}] Reload for file change: ${filePath}`); Promise.resolve('').then(() => { - if (filePath.replace(/^.*[\\/]/, '') === SITE_CONFIG_NAME) { + if (path.basename(filePath) === SITE_CONFIG_NAME) { return site.reloadSiteConfig(); } if (site.isDependencyOfPage(filePath)) { From dc0f82990b5d671359e2c5fa2986fb91487700d8 Mon Sep 17 00:00:00 2001 From: jonahtanjz Date: Thu, 18 Mar 2021 23:15:55 +0800 Subject: [PATCH 10/12] Update after review --- packages/core/src/Site/index.js | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/packages/core/src/Site/index.js b/packages/core/src/Site/index.js index fb37a0bdf2..19a77d10d9 100644 --- a/packages/core/src/Site/index.js +++ b/packages/core/src/Site/index.js @@ -823,7 +823,7 @@ class Site { const oldAddressablePages = this.addressablePages.slice(); const oldPagesSrc = oldAddressablePages.map(page => page.src); await this.readSiteConfig(); - this.updateAddressablePages(); + this.collectAddressablePages(); // Get pages with edited attributes but with the same src const editedPages = _.differenceWith(this.addressablePages, oldAddressablePages, (newPage, oldPage) => { if (!_.isEqual(newPage, oldPage)) { @@ -840,12 +840,12 @@ class Site { .map(filePath => Site.setExtension(filePath.src, '.html')); if (!_.isEmpty(addedPages) || !_.isEmpty(removedPages)) { await this.removeAsset(removedPages); - await this.rebuildSourceFiles(); + await this._rebuildSourceFiles(); await this.writeSiteData(); } else { this.updatePages(editedPages); const siteConfigDirectory = path.dirname(path.join(this.rootPath, this.siteConfigPath)); - this.rebuildAffectedSourceFiles(editedPages.map(page => path.join(siteConfigDirectory, page.src))); + this.regenerateAffectedPages(editedPages.map(page => path.join(siteConfigDirectory, page.src))); } } @@ -856,15 +856,7 @@ class Site { pagesToUpdate.forEach((pageToUpdate) => { this.pages.forEach((page, index) => { if (page.pageConfig.src === pageToUpdate.src) { - const newPage = this.createPage({ - faviconUrl: this.getFavIconUrl(), - pageSrc: pageToUpdate.src, - title: pageToUpdate.title, - layout: pageToUpdate.layout, - frontmatter: pageToUpdate.frontmatter, - searchable: pageToUpdate.searchable !== 'no', - externalScripts: pageToUpdate.externalScripts, - }); + const newPage = this.createNewPage(pageToUpdate, this.getFavIconUrl()); newPage.resetState(); this.pages[index] = newPage; } @@ -922,7 +914,16 @@ class Site { * @param {String} faviconUrl */ mapAddressablePagesToPages(addressablePages, faviconUrl) { - this.pages = addressablePages.map(page => this.createPage({ + this.pages = addressablePages.map(page => this.createNewPage(page, faviconUrl)); + } + + /** + * Creates and returns a new page with the given page config details and favicon url + * @param {*} page config + * @param {*} faviconUrl of the page + */ + createNewPage(page, faviconUrl) { + return this.createPage({ faviconUrl, pageSrc: page.src, title: page.title, @@ -930,7 +931,7 @@ class Site { frontmatter: page.frontmatter, searchable: page.searchable !== 'no', externalScripts: page.externalScripts, - })); + }); } /** From 940d68c72c0829ad6aea3275d0c9ff0d755c5726 Mon Sep 17 00:00:00 2001 From: jonahtanjz Date: Thu, 18 Mar 2021 23:25:32 +0800 Subject: [PATCH 11/12] Update comment --- packages/core/src/Site/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/core/src/Site/index.js b/packages/core/src/Site/index.js index 19a77d10d9..9957ed0ec7 100644 --- a/packages/core/src/Site/index.js +++ b/packages/core/src/Site/index.js @@ -918,9 +918,9 @@ class Site { } /** - * Creates and returns a new page with the given page config details and favicon url - * @param {*} page config - * @param {*} faviconUrl of the page + * Creates and returns a new Page with the given page config details and favicon url + * @param {Page} page config + * @param {String} faviconUrl of the page */ createNewPage(page, faviconUrl) { return this.createPage({ From a3f186fcfbaf64ec494ec65d0546d5b2a97e9398 Mon Sep 17 00:00:00 2001 From: jonahtanjz Date: Sat, 20 Mar 2021 16:57:09 +0800 Subject: [PATCH 12/12] Update after review --- packages/core/src/Site/index.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/packages/core/src/Site/index.js b/packages/core/src/Site/index.js index 9957ed0ec7..27647e5282 100644 --- a/packages/core/src/Site/index.js +++ b/packages/core/src/Site/index.js @@ -824,13 +824,6 @@ class Site { const oldPagesSrc = oldAddressablePages.map(page => page.src); await this.readSiteConfig(); this.collectAddressablePages(); - // Get pages with edited attributes but with the same src - const editedPages = _.differenceWith(this.addressablePages, oldAddressablePages, (newPage, oldPage) => { - if (!_.isEqual(newPage, oldPage)) { - return !oldPagesSrc.includes(newPage.src); - } - return true; - }); // Comparator for the _differenceWith comparison below const isNewPage = (newPage, oldPage) => _.isEqual(newPage, oldPage) || newPage.src === oldPage.src; @@ -838,11 +831,19 @@ class Site { const addedPages = _.differenceWith(this.addressablePages, oldAddressablePages, isNewPage); const removedPages = _.differenceWith(oldAddressablePages, this.addressablePages, isNewPage) .map(filePath => Site.setExtension(filePath.src, '.html')); + if (!_.isEmpty(addedPages) || !_.isEmpty(removedPages)) { await this.removeAsset(removedPages); await this._rebuildSourceFiles(); await this.writeSiteData(); } else { + // Get pages with edited attributes but with the same src + const editedPages = _.differenceWith(this.addressablePages, oldAddressablePages, (newPage, oldPage) => { + if (!_.isEqual(newPage, oldPage)) { + return !oldPagesSrc.includes(newPage.src); + } + return true; + }); this.updatePages(editedPages); const siteConfigDirectory = path.dirname(path.join(this.rootPath, this.siteConfigPath)); this.regenerateAffectedPages(editedPages.map(page => path.join(siteConfigDirectory, page.src))); @@ -995,6 +996,7 @@ class Site { this._setTimestampVariable(); this.mapAddressablePagesToPages(addressablePages, faviconUrl); + return this.generatePagesThrottled(this.pages); }