From 727ba2d12a05166444dc20ee2d5bcf49bb0c51a0 Mon Sep 17 00:00:00 2001 From: Aaron Chong Date: Thu, 18 Jan 2018 11:40:01 +0800 Subject: [PATCH 1/2] Rebase reference for static includes that target a div --- lib/parser.js | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/lib/parser.js b/lib/parser.js index 381548e4a7..344393751e 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -37,7 +37,7 @@ Parser.prototype.getDynamicIncludeSrc = function () { return _.clone(this.dynamicIncludeSrc); }; -Parser.prototype._preprocess = function (element, context) { +Parser.prototype._preprocess = function (element, context, config) { let self = this; element.attribs = element.attribs || {}; element.attribs[ATTRIB_CWF] = path.resolve(context.cwf); @@ -98,6 +98,7 @@ Parser.prototype._preprocess = function (element, context) { let actualContent = context.mode === 'include' ? (isInline ? utils.wrapContent(htmlContent) : utils.wrapContent(htmlContent, '\n\n', '\n')) : md.render(htmlContent); + actualContent = self._rebaseReferenceForStaticIncludes(actualContent, element, config); if (!isIncludeSrcMd) { // Include HTML. Just let it be. actualContent = htmlContent; @@ -121,7 +122,7 @@ Parser.prototype._preprocess = function (element, context) { childContext.source = isIncludeSrcMd ? 'md' : 'html'; if (element.children && element.children.length > 0) { element.children = element.children.map((e) => { - return self._preprocess(e, childContext); + return self._preprocess(e, childContext, config); }); } } else if (element.name === 'dynamic-panel') { @@ -159,7 +160,7 @@ Parser.prototype._preprocess = function (element, context) { } else { if (element.children && element.children.length > 0) { element.children = element.children.map((e) => { - return self._preprocess(e, context); + return self._preprocess(e, context, config); }); } } @@ -275,7 +276,7 @@ Parser.prototype._trimNodes = function (node) { } }; -Parser.prototype.includeFile = function (file, cb) { +Parser.prototype.includeFile = function (file, cb, config) { cb = cb || function () {}; // create empty callback let context = {}; @@ -290,7 +291,7 @@ Parser.prototype.includeFile = function (file, cb) { return; } let nodes = dom.map(d => { - return this._preprocess(d, context); + return this._preprocess(d, context, config); }); resolve(cheerio.html(nodes)); cb(null, cheerio.html(nodes)); @@ -466,6 +467,31 @@ Parser.prototype._rebaseReference = function(node, foundBase) { } }; +Parser.prototype._rebaseReferenceForStaticIncludes = function (pageData, element, config) { + if (!config) { + return pageData; + } + + if (!pageData.includes('{{baseUrl}}')) { + return pageData; + } + + let filePath = element.attribs[ATTRIB_INCLUDE_PATH]; + let fileBase = calculateNewBaseUrl(filePath, config.rootPath, config.baseUrlMap); + if (!fileBase.relative) { + return pageData; + } + + let currentPath = element.attribs[ATTRIB_CWF]; + let currentBase = calculateNewBaseUrl(currentPath, config.rootPath, config.baseUrlMap); + if (currentBase.relative === fileBase.relative) { + return pageData; + } + + let newBase = fileBase.relative; + return nunjucks.renderString(pageData, { baseUrl: `{{hostBaseUrl}}/${newBase}` }); +}; + function calculateNewBaseUrl(filePath, root, lookUp) { function calculate(file, result) { if (file === root) { From 22cab7eb2c34a952a0878d5a2e74c274bc0a98b7 Mon Sep 17 00:00:00 2001 From: Aaron Chong Date: Fri, 19 Jan 2018 09:45:11 +0800 Subject: [PATCH 2/2] Swap parameters for `config` and `cb` (deprecated) --- lib/parser.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/parser.js b/lib/parser.js index 344393751e..0e5cc948c3 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -276,7 +276,7 @@ Parser.prototype._trimNodes = function (node) { } }; -Parser.prototype.includeFile = function (file, cb, config) { +Parser.prototype.includeFile = function (file, config, cb) { cb = cb || function () {}; // create empty callback let context = {};