Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 31 additions & 5 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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') {
Expand Down Expand Up @@ -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);
});
}
}
Expand Down Expand Up @@ -275,7 +276,7 @@ Parser.prototype._trimNodes = function (node) {
}
};

Parser.prototype.includeFile = function (file, cb) {
Parser.prototype.includeFile = function (file, config, cb) {
cb = cb || function () {}; // create empty callback

let context = {};
Expand All @@ -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));
Expand Down Expand Up @@ -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) {
Expand Down