Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit 0fb6df4

Browse files
authored
Merge pull request #34 from rachx/support-variables
Support user-defined variables #143
2 parents 8c0a84e + 4d96fbd commit 0fb6df4

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

lib/Page.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ function Page(pageConfig) {
2323
this.baseUrl = pageConfig.baseUrl;
2424
this.asset = pageConfig.asset;
2525
this.baseUrlMap = pageConfig.baseUrlMap;
26+
this.userDefinedVariablesMap = pageConfig.userDefinedVariablesMap;
2627
this.includedFiles = {};
2728
}
2829

@@ -80,6 +81,7 @@ Page.prototype.generate = function (builtFiles) {
8081
return new Promise((resolve, reject) => {
8182
markbinder.includeFile(this.sourcePath, {
8283
baseUrlMap: this.baseUrlMap,
84+
userDefinedVariablesMap: this.userDefinedVariablesMap,
8385
rootPath: this.rootPath,
8486
})
8587
.then(result => markbinder.resolveBaseUrl(result, {
@@ -157,6 +159,7 @@ Page.prototype.resolveDependency = function (dependency, builtFiles) {
157159
}
158160
return markbinder.includeFile(file, {
159161
baseUrlMap: this.baseUrlMap,
162+
userDefinedVariablesMap: this.userDefinedVariablesMap,
160163
rootPath: this.rootPath,
161164
})
162165
.then(result => markbinder.resolveBaseUrl(result, {

lib/Site.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const cheerio = require('cheerio');
12
const path = require('path');
23
const ignore = require('ignore');
34
const ejs = require('ejs');
@@ -17,6 +18,7 @@ const BOILERPLATE_FOLDER_NAME = '_boilerplates';
1718
const SITE_ASSET_FOLDER_NAME = 'asset';
1819
const TEMPLATE_ROOT_FOLDER_NAME = 'template';
1920
const TEMPLATE_SITE_ASSET_FOLDER_NAME = 'markbind';
21+
const USER_VARIABLES_PATH = '_markbind/variables.md';
2022

2123
const SITE_CONFIG_DEFAULT = {
2224
baseUrl: '',
@@ -38,6 +40,11 @@ const SITE_CONFIG_DEFAULT = {
3840

3941
const INDEX_MARKDOWN_DEFAULT = '# Hello world\nWelcome to your page generated with MarkBind.\n';
4042

43+
const USER_VARIABLES_DEFAULT = '<span id="example">\n'
44+
+ 'To inject this HTML segment in your markbind files, use {{ example }} where you want to place it.\n'
45+
+ 'More generally, surround the segment\'s id with double curly braces.\n'
46+
+ '</span>';
47+
4148
function Site(rootPath, outputPath) {
4249
this.rootPath = rootPath;
4350
this.outputPath = outputPath;
@@ -84,6 +91,7 @@ Site.initSite = function (rootPath) {
8491
const boilerplatePath = path.join(rootPath, BOILERPLATE_FOLDER_NAME);
8592
const configPath = path.join(rootPath, SITE_CONFIG_NAME);
8693
const indexPath = path.join(rootPath, INDEX_MARKDOWN_FILE);
94+
const userDefinedVariablesPath = path.join(rootPath, USER_VARIABLES_PATH);
8795
// TODO: log the generate info
8896
return new Promise((resolve, reject) => {
8997
fs.accessAsync(boilerplatePath)
@@ -107,6 +115,13 @@ Site.initSite = function (rootPath) {
107115
}
108116
return fs.outputFileAsync(indexPath, INDEX_MARKDOWN_DEFAULT);
109117
})
118+
.then(() => fs.accessAsync(userDefinedVariablesPath))
119+
.catch(() => {
120+
if (fs.existsSync(userDefinedVariablesPath)) {
121+
return Promise.resolve();
122+
}
123+
return fs.outputFileAsync(userDefinedVariablesPath, USER_VARIABLES_DEFAULT);
124+
})
110125
.then(resolve)
111126
.catch(reject);
112127
});
@@ -153,6 +168,7 @@ Site.prototype.createPageData = function (config) {
153168
baseUrl: config.baseUrl,
154169
pageTemplate: config.pageTemplate,
155170
baseUrlMap: this.baseUrlMap,
171+
userDefinedVariablesMap: this.userDefinedVariablesMap,
156172
asset: {
157173
bootstrap: path.relative(path.dirname(resultPath),
158174
path.join(this.siteAssetsDestPath, 'css', 'bootstrap.min.css')),
@@ -185,6 +201,38 @@ Site.prototype.collectBaseUrl = function () {
185201
return Promise.resolve();
186202
};
187203

204+
/**
205+
* Collects the user defined variables map in the site/subsites
206+
*/
207+
Site.prototype.collectUserDefinedVariablesMap = function () {
208+
// The key is the base directory of the site/subsites,
209+
// while the value is a mapping of user defined variables
210+
this.userDefinedVariablesMap = {};
211+
212+
Object.keys(this.baseUrlMap).forEach((base) => {
213+
const userDefinedVariables = {};
214+
let content;
215+
try {
216+
const userDefinedVariablesPath = path.resolve(base, USER_VARIABLES_PATH);
217+
content = fs.readFileSync(userDefinedVariablesPath, 'utf8');
218+
} catch (e) {
219+
content = '';
220+
logger.warn(e.message);
221+
}
222+
const $ = cheerio.load(content);
223+
$.root().children().each(function () {
224+
const id = $(this).attr('id');
225+
const html = $(this).html();
226+
userDefinedVariables[id] = html;
227+
});
228+
229+
// This is to prevent the first nunjuck call from converting {{baseUrl}} to an empty string
230+
// and let the baseUrl value be injected later.
231+
userDefinedVariables.baseUrl = '{{baseUrl}}';
232+
this.userDefinedVariablesMap[base] = userDefinedVariables;
233+
});
234+
};
235+
188236
Site.prototype.generate = function () {
189237
// Create the .tmp folder for storing intermediate results.
190238
fs.emptydirSync(this.tempPath);
@@ -193,6 +241,7 @@ Site.prototype.generate = function () {
193241
return new Promise((resolve, reject) => {
194242
this.readSiteConfig()
195243
.then(() => this.collectBaseUrl())
244+
.then(() => this.collectUserDefinedVariablesMap())
196245
.then(() => this.buildAssets())
197246
.then(() => this.buildSourceFiles())
198247
.then(() => this.copyMarkBindAsset())

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"dependencies": {
2020
"bluebird": "^3.4.7",
2121
"chalk": "^1.1.3",
22+
"cheerio": "^0.22.0",
2223
"chokidar": "^1.6.1",
2324
"clear": "^0.0.1",
2425
"commander": "^2.9.0",

0 commit comments

Comments
 (0)