Skip to content

Implement live preview for pages in site.json#1514

Merged
ang-zeyu merged 12 commits into
MarkBind:masterfrom
jonahtanjz:live-preview-pages
Mar 21, 2021
Merged

Implement live preview for pages in site.json#1514
ang-zeyu merged 12 commits into
MarkBind:masterfrom
jonahtanjz:live-preview-pages

Conversation

@jonahtanjz

@jonahtanjz jonahtanjz commented Mar 13, 2021

Copy link
Copy Markdown
Contributor

What is the purpose of this pull request?

  • Documentation update
  • Bug fix
  • Feature addition or enhancement
  • Code maintenance
  • Others, please explain:

Overview of changes:
Addresses part of #48.

This PR intends to support live preview for the pages property in site.json. When site.json is edited, reloadSiteConfig function will read in the new values and check for changes in pages. If there is added/removed pages, all pages will be rebuilt. This implementation is similar to setting the glob to track all .md and .mbd files in pages ("glob": ["**/index.md", "**/*.+(md|mbd)"]). If new pages are added/removed, all pages will be rebuilt in the addHandler.

If the attributes of a page are edited, then only that specific page/pages dependent on this will be rebuilt. For example, if the "title" of a page at contents/index.md is changed, then only contents/index.html will be regenerated. The rest of the pages will not be rebuilt.

Anything you'd like to highlight / discuss:
Current implementation will rebuild all pages when there is a change in pages. Not sure if I should only 'watch' for added/removed pages and then only update the relevant pages that will be affected.

EDIT: After looking around at the other handlers (addHandler and removeHandler), it seems all pages should be rebuilt if the added/removed file is a page/dependency of page.

Testing instructions:

  1. Serve a MarkBind site using markbind serve
  2. Open and edit the pages property in site.json
  3. Live preview should be updated to reflect he latest changes

Proposed commit message: (wrap lines at 72 characters)
Implement live preview for pages in site.json

When pages are added or removed from site.json, the respective
generated pages are not built or removed from the live preview.
Similarly, edited pages attributes are not reflected as well. A server
reboot is necessary to display the new changes.

Let's support live preview for pages in site.json so that the user
does not have to restart the server each time the pages property
is edited.


Checklist: ☑️

  • Updated the documentation for feature additions and enhancements
  • Added tests for bug fixes or features
  • Linked all related issues
  • No blatantly unrelated changes
  • Pinged someone for a review!

@jonahtanjz jonahtanjz changed the title [WIP] Implement live preview for pages in sites.json [WIP] Implement live preview for pages in site.json Mar 14, 2021
@jonahtanjz jonahtanjz changed the title [WIP] Implement live preview for pages in site.json Implement live preview for pages in site.json Mar 15, 2021
@jonahtanjz jonahtanjz marked this pull request as ready for review March 15, 2021 14:18
Comment thread packages/cli/index.js Outdated

@ang-zeyu ang-zeyu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @jonahtanjz

Some nits:


EDIT: After looking around at the other handlers (addHandler and removeHandler), it seems all pages should be rebuilt if the added/removed file is a page/dependency of page.

This has been existing behaviour for a long while already but can probably be made into an issue 🙂

Comment thread packages/core/src/Site/index.js Outdated
Comment thread packages/core/src/Site/index.js Outdated
pagesToUpdate.forEach((pageToUpdate) => {
this.pages.forEach((page, index) => {
if (page.pageConfig.src === pageToUpdate.src) {
const newPage = this.createPage({

@ang-zeyu ang-zeyu Mar 17, 2021

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about something like a mapAddressablePageToPage function to reduce duplication with mapAddressablePagesToPages (or if could generalise the latter maybe even better)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I tried to combine them but it seems like their logic is quite different. As a workaround, I've extracted out the createPage part into a separate function so that both mapAddressablePagesToPages and this can call the new function which resolves the repetition issue. Is this ok?

Comment thread packages/core/src/Site/index.js Outdated
Comment thread packages/core/src/Site/index.js Outdated
} else {
this.updatePages(editedPages);
const siteConfigDirectory = path.dirname(path.join(this.rootPath, this.siteConfigPath));
this.rebuildAffectedSourceFiles(editedPages.map(page => path.join(siteConfigDirectory, page.src)));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm... would it be correct to instead use regenerateAffectedPages (in _rebuildAffectedSourceFiles) directly? (or something even lower level)

The filePaths parameter of rebuildAffectedSourceFiles indicates changed source files, but over here its the page configuration that changed, not the file. So the extra fluff (e.g. layout building) in _rebuildAffectedSourceFiles shouldn't be needed, and even with regenerateAffectedPages there's still quite a bit of fluff (e.g. change in a page's properties wouldn't affect other pages even if said page's source file is a dependency of those other pages).

Only exactly the pages which's properties have changed in site.json should be rebuilt. (and even then if using markbind s -o only the page being viewed should be rebuilt)

If this needs a substantial addition here that's not worth it, we can do it after enhancing the rebuild process for page addition / removal (above comment) + #1513 though, then streamlining the overall flow, which should make this easier; also since this is a feature addition.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm... would it be correct to instead use regenerateAffectedPages (in _rebuildAffectedSourceFiles) directly? (or something even lower level)

The filePaths parameter of rebuildAffectedSourceFiles indicates changed source files, but over here its the page configuration that changed, not the file. So the extra fluff (e.g. layout building) in _rebuildAffectedSourceFiles shouldn't be needed, and even with regenerateAffectedPages there's still quite a bit of fluff (e.g. change in a page's properties wouldn't affect other pages even if said page's source file is a dependency of those other pages).

Yup agree that changing page properties wouldn't affect other pages and they should not be rebuilt. I will look into this and perhaps open another PR to work on it as it seems like it will take some time and will require quite a bit of change. For now I've switched to using regenerateAffectedPages as suggested.

@ang-zeyu ang-zeyu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Last 2 nits, generally looks good to go 👍

Please hold on while I merge #1513 in first; There shouldn't be any conflicts but would be good to test this on top of that.

Comment thread packages/core/src/Site/index.js
Comment thread packages/core/src/Site/index.js Outdated
@jonahtanjz

Copy link
Copy Markdown
Contributor Author

@ang-zeyu I have updated the PR accordingly.

Please hold on while I merge #1513 in first; There shouldn't be any conflicts but would be good to test this on top of that.

Alright sounds good :)

@ang-zeyu ang-zeyu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm 👍

@ang-zeyu ang-zeyu added this to the v3.0 milestone Mar 21, 2021
@ang-zeyu ang-zeyu merged commit 8c2195e into MarkBind:master Mar 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants