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 = '_markbind/variables.md' ;
2022
2123const SITE_CONFIG_DEFAULT = {
2224 baseUrl : '' ,
@@ -38,6 +40,11 @@ const SITE_CONFIG_DEFAULT = {
3840
3941const 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+
4148function 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+
188236Site . 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 ( ) )
0 commit comments