Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return refinementsArray.map(refinementAsHTML => {
// element is (simplified) <div>facetName <span>facetCount</span></div>
const element = cheerio.parseHTML(refinementAsHTML)[0];
// <div>**facetName** <span>facetCount</span></div>
const name = element.firstChild.nodeValue.trim();
// some widgets have no counts (pricesRanges)
// <div>facetName <span>**facetCount**</span></div>
const maybeCount = element.firstChild.nextSibling.children;
return {
name,
count: maybeCount && maybeCount[0] && maybeCount[0].nodeValue !== undefined ?
parseInt(maybeCount[0].nodeValue, 10) :
'n/a'
};
});
if (isOptional) {
// optional includes of segments have now been handled, so delete the attribute
delete element.attribs.optional;
}
if (isIncludeSrcMd) {
if (context.mode === 'include') {
actualContent = isInline ? actualContent : utils.wrapContent(actualContent, '\n\n', '\n');
} else {
actualContent = md.render(actualContent);
}
actualContent = self._rebaseReferenceForStaticIncludes(actualContent, element, config);
}
const wrapperType = isInline ? 'span' : 'div';
element.children = cheerio.parseHTML(
`<${wrapperType} data-included-from="${filePath}">${actualContent}`,
true,
);
} else {
if (isImport) {
// eslint-disable-next-line no-console
console.warn(`Missing hash for import variable ${includeSrc.pathname}`
+ ` in ${element.attribs[ATTRIB_CWF]}.`);
return createEmptyNode();
}
let actualContent = (fileContent && isTrim) ? fileContent.trim() : fileContent;
if (isIncludeSrcMd) {
if (context.mode === 'include') {
actualContent = isInline ? actualContent : utils.wrapContent(actualContent, '\n\n', '\n');
} else {
actualContent = md.render(actualContent);
const wrapperType = isInline ? 'span' : 'div';
element.children = cheerio.parseHTML(
`<${wrapperType} data-included-from="${filePath}">${actualContent}`,
true,
);
} else {
let actualContent = (fileContent && isTrim) ? fileContent.trim() : fileContent;
if (isIncludeSrcMd) {
if (context.mode === 'include') {
actualContent = isInline ? actualContent : utils.wrapContent(actualContent, '\n\n', '\n');
} else {
actualContent = md.render(actualContent);
}
}
const wrapperType = isInline ? 'span' : 'div';
element.children = cheerio.parseHTML(
`<${wrapperType} data-included-from="${filePath}">${actualContent}`,
true,
);
}
if (element.children && element.children.length > 0) {
if (childContext.callStack.length > CyclicReferenceError.MAX_RECURSIVE_DEPTH) {
const error = new CyclicReferenceError(childContext.callStack);
this._onError(error);
return createErrorNode(element, error);
}
element.children = element.children.map(e => self._preprocess(e, childContext, config));
}
} else if ((element.name === 'panel') && hasSrc) {
if (!isUrl && includeSrc.hash) {
element.attribs.fragment = includeSrc.hash.substring(1); // save hash to fragment attribute
} else {
actualContent = md.render(actualContent);
}
actualContent = self._rebaseReferenceForStaticIncludes(actualContent, element, config);
}
element.children = cheerio.parseHTML(actualContent, true); // the needed content;
} else {
let actualContent = fileContent;
if (isIncludeSrcMd) {
if (context.mode === 'include') {
actualContent = isInline ? actualContent : utils.wrapContent(actualContent, '\n\n', '\n');
} else {
actualContent = md.render(actualContent);
}
}
element.children = cheerio.parseHTML(actualContent, true);
}
// The element's children are in the new context
// Process with new context
const childContext = _.cloneDeep(context);
childContext.cwf = filePath;
childContext.source = isIncludeSrcMd ? 'md' : 'html';
if (element.children && element.children.length > 0) {
element.children = element.children.map(e => self._preprocess(e, childContext, config));
}
} else if (element.name === 'dynamic-panel') {
if (!isUrl && includeSrc.hash) {
element.attribs.fragment = includeSrc.hash.substring(1); // save hash to fragment attribute
}
element.attribs.src = filePath;
this.dynamicIncludeSrc.push({ from: context.cwf, to: actualFilePath, asIfTo: filePath });
}
if (element.name) {
element.name = element.name.toLowerCase();
}
switch (element.name) {
case 'md':
element.name = 'span';
cheerio.prototype.options.xmlMode = false;
element.children = cheerio.parseHTML(md.renderInline(cheerio.html(element.children)), true);
cheerio.prototype.options.xmlMode = true;
break;
case 'markdown':
element.name = 'div';
cheerio.prototype.options.xmlMode = false;
element.children = cheerio.parseHTML(md.render(cheerio.html(element.children)), true);
cheerio.prototype.options.xmlMode = true;
break;
case 'panel': {
if (!_.hasIn(element.attribs, 'src')) { // dynamic panel
break;
}
const fileExists = utils.fileExists(element.attribs.src)
|| utils.fileExists(calculateBoilerplateFilePath(element.attribs.boilerplate,
element.attribs.src, config));
if (fileExists) {
const { src, fragment } = element.attribs;
const resultDir = path.dirname(path.join('{{hostBaseUrl}}', path.relative(config.rootPath, src)));
const resultPath = path.join(resultDir, utils.setExtension(path.basename(src), '._include_.html'));
element.attribs.src = utils.ensurePosix(fragment ? `${resultPath}#${fragment}` : resultPath);
}
delete element.attribs.boilerplate;
}
if (element.name) {
element.name = element.name.toLowerCase();
}
switch (element.name) {
case 'md':
element.name = 'span';
cheerio.prototype.options.xmlMode = false;
element.children = cheerio.parseHTML(md.renderInline(cheerio.html(element.children)), true);
cheerio.prototype.options.xmlMode = true;
break;
case 'markdown':
element.name = 'div';
cheerio.prototype.options.xmlMode = false;
element.children = cheerio.parseHTML(md.render(cheerio.html(element.children)), true);
cheerio.prototype.options.xmlMode = true;
break;
case 'panel': {
if (!_.hasIn(element.attribs, 'src')) { // dynamic panel
break;
}
const fileExists = utils.fileExists(element.attribs.src)
|| utils.fileExists(calculateBoilerplateFilePath(element.attribs.boilerplate,
element.attribs.src, config));
if (fileExists) {
const { src, fragment } = element.attribs;
const resultDir = path.dirname(path.join('{{hostBaseUrl}}', path.relative(config.rootPath, src)));
const resultPath = path.join(resultDir, utils.setExtension(path.basename(src), '._include_.html'));
element.attribs.src = utils.ensurePosix(fragment ? `${resultPath}#${fragment}` : resultPath);
}
delete element.attribs.boilerplate;
}
if (element.name) {
element.name = element.name.toLowerCase();
}
switch (element.name) {
case 'md':
element.name = 'span';
cheerio.prototype.options.xmlMode = false;
element.children = cheerio.parseHTML(md.renderInline(cheerio.html(element.children)), true);
cheerio.prototype.options.xmlMode = true;
break;
case 'markdown':
element.name = 'div';
cheerio.prototype.options.xmlMode = false;
element.children = cheerio.parseHTML(md.render(cheerio.html(element.children)), true);
cheerio.prototype.options.xmlMode = true;
break;
case 'dynamic-panel': {
const hasIsOpen = _.hasIn(element.attribs, 'isOpen');
element.attribs.isOpen = hasIsOpen || false;
const fileExists = utils.fileExists(element.attribs.src)
|| utils.fileExists(calculateBoilerplateFilePath(element.attribs.boilerplate,
element.attribs.src, config));
if (fileExists) {
const { src, fragment } = element.attribs;
const resultDir = path.dirname(path.join('{{hostBaseUrl}}', path.relative(process.cwd(), src)));
const resultPath = path.join(resultDir, utils.setExtension(path.basename(src), '._include_.html'));
element.attribs.src = fragment ? `${resultPath}#${fragment}` : resultPath;
}
element.attribs['no-close'] = 'true';
element.attribs['no-switch'] = 'true';
function createErrorNode(element, error) {
const errorElement = cheerio.parseHTML(utils.createErrorElement(error), true)[0];
return Object.assign(element, _.pick(errorElement, ['name', 'attribs', 'children']));
}
function createErrorNode(element, error) {
const errorElement = cheerio.parseHTML(utils.createErrorElement(error), true)[0];
return Object.assign(element, _.pick(errorElement, ['name', 'attribs', 'children']));
}