1+ const cheerio = require ( 'cheerio' ) ;
12const path = require ( 'path' ) ;
23const ignore = require ( 'ignore' ) ;
34const ejs = require ( 'ejs' ) ;
@@ -17,6 +18,7 @@ const BOILERPLATE_FOLDER_NAME = '_boilerplates';
1718const SITE_ASSET_FOLDER_NAME = 'asset' ;
1819const TEMPLATE_ROOT_FOLDER_NAME = 'template' ;
1920const TEMPLATE_SITE_ASSET_FOLDER_NAME = 'markbind' ;
21+ const USER_VARIABLES_PATH = '_system/variables.md' ;
2022
2123const 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+
188223Site . 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 ( ) )
0 commit comments