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

Commit 788c39b

Browse files
committed
collect user defined variables map
1 parent e1e47cf commit 788c39b

3 files changed

Lines changed: 40 additions & 0 deletions

File tree

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: 36 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 = '_system/variables.md';
2022

2123
const SITE_CONFIG_DEFAULT = {
2224
baseUrl: '',
@@ -153,6 +155,7 @@ Site.prototype.createPageData = function (config) {
153155
baseUrl: config.baseUrl,
154156
pageTemplate: config.pageTemplate,
155157
baseUrlMap: this.baseUrlMap,
158+
userDefinedVariablesMap: this.userDefinedVariablesMap,
156159
asset: {
157160
bootstrap: path.relative(path.dirname(resultPath),
158161
path.join(this.siteAssetsDestPath, 'css', 'bootstrap.min.css')),
@@ -185,6 +188,38 @@ Site.prototype.collectBaseUrl = function () {
185188
return Promise.resolve();
186189
};
187190

191+
/**
192+
* Collects the user defined variables map in the site/subsites
193+
*/
194+
Site.prototype.collectUserDefinedVariablesMap = function () {
195+
// The key is the base directory of the site/subsites,
196+
// while the value is a mapping of user defined variables
197+
this.userDefinedVariablesMap = {};
198+
199+
Object.keys(this.baseUrlMap).forEach((base) => {
200+
const userDefinedVariables = {};
201+
let content;
202+
try {
203+
const userDefinedVariablesPath = path.resolve(base, USER_VARIABLES_PATH);
204+
content = fs.readFileSync(userDefinedVariablesPath, 'utf8');
205+
} catch (e) {
206+
content = '';
207+
logger.warn(e.message);
208+
}
209+
const $ = cheerio.load(content);
210+
$.root().children().each(function () {
211+
const id = $(this).attr('id');
212+
const html = $(this).html();
213+
userDefinedVariables[id] = html;
214+
});
215+
216+
// This is to prevent the first nunjuck call from converting {{baseUrl}} to an empty string
217+
// and let the baseUrl value be injected later.
218+
userDefinedVariables.baseUrl = '{{baseUrl}}';
219+
this.userDefinedVariablesMap[base] = userDefinedVariables;
220+
});
221+
};
222+
188223
Site.prototype.generate = function () {
189224
// Create the .tmp folder for storing intermediate results.
190225
fs.emptydirSync(this.tempPath);
@@ -193,6 +228,7 @@ Site.prototype.generate = function () {
193228
return new Promise((resolve, reject) => {
194229
this.readSiteConfig()
195230
.then(() => this.collectBaseUrl())
231+
.then(() => this.collectUserDefinedVariablesMap())
196232
.then(() => this.buildAssets())
197233
.then(() => this.buildSourceFiles())
198234
.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)