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

Add serve --one-page option to live preview a single page #475

Merged
merged 2 commits into from
Dec 23, 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
1 change: 1 addition & 0 deletions docs/userGuide/developingASite.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ MarkBind will generate the site in a folder named `_site` in the current directo
| `-f`, `--force-reload` | Force a full reload of all site files when a file is changed. |
| `-p`, `--port <port>` | The port used for serving your website. |
| `-s`, `--site-config <file>` | Specify the site config file (default: site.json) |
| `--one-page <file>` | Render and serve only a single page from your website. |
Xenonym marked this conversation as resolved.
Show resolved Hide resolved
| --no-open | Don't open browser automatically. |

Note: Live reload is only supported for the following file types:
Expand Down
1 change: 1 addition & 0 deletions docs/userGuide/userQuickStart.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ Live reload is enabled to regenerate the site for changes, so you could see the
| `-f`, `--force-reload` | Force a full reload of all site files when a file is changed. |
| `-p`, `--port <port>` | The port used for serving your website. |
| `-s`, `--site-config <file>` | Specify the site config file (default: site.json) |
| `--one-page <file>` | Render and serve only a single page from your website. |
| --no-open | Don't open browser automatically. |

### Build the static site
Expand Down
11 changes: 9 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,20 @@ program
.option('-f, --force-reload', 'force a full reload of all site files when a file is changed')
.option('-p, --port <port>', 'port for server to listen on (Default is 8080)')
.option('-s, --site-config <file>', 'specify the site config file (default: site.json)')
.option('--one-page <file>', 'render and serve only a single page in the site')
.option('--no-open', 'do not automatically open the site in browser')
.action((root, options) => {
const rootFolder = path.resolve(root || process.cwd());
const logsFolder = path.join(rootFolder, '_markbind/logs');
const outputFolder = path.join(rootFolder, '_site');

const site = new Site(rootFolder, outputFolder, options.forceReload, options.siteConfig);
if (options.onePage) {
// replace slashes for paths on Windows
// eslint-disable-next-line no-param-reassign
options.onePage = fsUtil.ensurePosix(options.onePage);
}

const site = new Site(rootFolder, outputFolder, options.onePage, options.forceReload, options.siteConfig);

const addHandler = (filePath) => {
logger.info(`[${new Date().toLocaleTimeString()}] Reload for file add: ${filePath}`);
Expand Down Expand Up @@ -176,7 +183,7 @@ program

// server config
const serverConfig = {
open: options.open,
open: options.open && options.onePage ? `/${options.onePage.replace(/\.(md|mbd)$/, '.html')}` : false,
logLevel: 0,
root: outputFolder,
port: options.port || 8080,
Expand Down
32 changes: 24 additions & 8 deletions src/Site.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ const GENERATE_SITE_LOGGING_KEY = 'Generate Site';
const MARKBIND_WEBSITE_URL = 'https://markbind.github.io/markbind/';
const MARKBIND_LINK_HTML = `<a href='${MARKBIND_WEBSITE_URL}'>MarkBind ${CLI_VERSION}</a>`;

function Site(rootPath, outputPath, forceReload = false, siteConfigPath = SITE_CONFIG_NAME) {
function Site(rootPath, outputPath, onePagePath, forceReload = false, siteConfigPath = SITE_CONFIG_NAME) {
const configPath = findUp.sync(siteConfigPath, { cwd: rootPath });
this.rootPath = configPath ? path.dirname(configPath) : rootPath;
this.outputPath = outputPath;
Expand All @@ -123,6 +123,7 @@ function Site(rootPath, outputPath, forceReload = false, siteConfigPath = SITE_C
this.addressablePages = [];
this.baseUrlMap = {};
this.forceReload = forceReload;
this.onePagePath = onePagePath;
this.siteConfig = {};
this.siteConfigPath = siteConfigPath;
this.userDefinedVariablesMap = {};
Expand Down Expand Up @@ -586,13 +587,28 @@ Site.prototype.generatePages = function () {
}

this._setTimestampVariable();
this.pages = addressablePages.map(page => this.createPage({
faviconUrl,
pageSrc: page.src,
title: page.title,
layout: page.layout || LAYOUT_DEFAULT_NAME,
searchable: page.searchable !== 'no',
}));
if (this.onePagePath) {
const page = addressablePages.find(p => p.src === this.onePagePath);
if (!page) {
return Promise.reject(new Error(`${this.onePagePath} does not exist`));
}
this.pages.push(this.createPage({
faviconUrl,
pageSrc: page.src,
title: page.title,
layout: page.layout || LAYOUT_DEFAULT_NAME,
searchable: page.searchable !== 'no',
}));
} else {
this.pages = addressablePages.map(page => this.createPage({
faviconUrl,
pageSrc: page.src,
title: page.title,
layout: page.layout || LAYOUT_DEFAULT_NAME,
searchable: page.searchable !== 'no',
}));
}

const progressBar = new ProgressBar(`[:bar] :current / ${this.pages.length} pages built`,
{ total: this.pages.length });
progressBar.render();
Expand Down
8 changes: 8 additions & 0 deletions src/util/fsUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ const path = require('path');
const sourceFileExtNames = ['.html', '.md', '.mbd', '.mbdf'];

module.exports = {
ensurePosix: (filePath) => {
if (path.sep !== '/') {
return filePath.split(path.sep).join('/');
}

return filePath;
},

isSourceFile(filePath) {
return sourceFileExtNames.includes(path.extname(filePath));
},
Expand Down