kentico-cloud-delivery
Version:
Official Kentico Cloud Delivery SDK
128 lines • 5.65 kB
JavaScript
import * as parse5 from 'parse5/lib';
var NodeRichTextParser = /** @class */ (function () {
function NodeRichTextParser() {
this.modularContentWrapperElem = 'div';
this.modularContent = {
type: 'application/kenticocloud',
dataType: 'data-type',
dataCodename: 'data-codename'
};
this.link = {
nodeName: 'a',
dataItemId: 'data-item-id'
};
}
NodeRichTextParser.prototype.resolveRichTextField = function (html, replacement, config) {
try {
// create document
var documentFragment = parse5.parseFragment(html);
// get all modular content items
var result = this.processRichTextField(this.getChildNodes(documentFragment), replacement, config, {
links: [],
modularContentItems: []
});
var resolvedHtml = parse5.serialize(documentFragment);
return {
links: result.links,
modularContentItems: result.modularContentItems,
resolvedHtml: resolvedHtml
};
}
catch (error) {
throw Error('Parsing HTML failed:' + error);
}
};
NodeRichTextParser.prototype.processRichTextField = function (elements, replacement, config, result) {
var _this = this;
if (!elements || elements.length === 0) {
// there are no more elements
}
else {
elements.forEach(function (element) {
if (element.attrs) {
// process modular content
_this.processModularContent(element, replacement, config, result);
// process link
_this.processLink(element, replacement, config, result);
}
if (element.childNodes) {
// recursively process all childs
_this.processRichTextField(_this.getChildNodes(element), replacement, config, result);
}
});
}
return result;
};
NodeRichTextParser.prototype.processLink = function (element, replacement, config, result) {
var _this = this;
var attributes = element.attrs;
if (element.nodeName !== this.link.nodeName) {
// node is not a link
return;
}
// get all links which have item it attribute, ignore all other links (they can be regular links in rich text)
var dataItemIdAttribute = attributes.find(function (m) { return m.name === _this.link.dataItemId; });
if (!dataItemIdAttribute) {
// its either a regular link or the attribute is not defined
return;
}
// get id of content item
var itemId = dataItemIdAttribute.value;
var link = {
dataItemId: dataItemIdAttribute ? dataItemIdAttribute.value : ''
};
// add to result
result.links.push(link);
var resolvedUrl = replacement.getLinkUrl(link.dataItemId);
// assign url to 'href' attribute of the link
var hrefAttribute = attributes.find(function (m) { return m.name === 'href'; });
if (!hrefAttribute) {
// href attribute is missing
if (config.enableAdvancedLogging) {
console.warn("Cannot set url '" + resolvedUrl + "' because 'href' attribute is not present in the link element");
}
return;
}
hrefAttribute.value = resolvedUrl;
};
NodeRichTextParser.prototype.processModularContent = function (element, replacement, config, result) {
var _this = this;
var attributes = element.attrs;
var dataTypeAttribute = attributes.find(function (m) { return m.value === _this.modularContent.type; });
if (!dataTypeAttribute) {
// node is not of modular content type
return;
}
// get codename of the modular content
var dataCodenameAttribute = attributes.find(function (m) { return m.name === _this.modularContent.dataCodename; });
if (dataCodenameAttribute == null) {
throw Error("The '" + this.modularContent.dataCodename + "' attribute is missing and therefore modular content item cannot be retrieved");
}
var itemCodename = dataCodenameAttribute.value;
var modularItem = {
dataCodename: dataCodenameAttribute ? dataCodenameAttribute.value : '',
dataType: dataTypeAttribute ? dataTypeAttribute.value : ''
};
// add to result
result.modularContentItems.push(modularItem);
// get html
var resultHtml = replacement.getModularContentHtml(itemCodename);
// replace 'object' tag name
element.tagName = config.modularContentWrapperTag;
// add classes
element.attrs.push({
name: 'class',
value: config.modularContentWrapperClasses.map(function (m) { return m; }).join(' ')
});
// get serialized set of nodes from HTML
var serializedChildNodes = parse5.parseFragment(resultHtml);
// add child nodes
element.childNodes = serializedChildNodes.childNodes;
};
NodeRichTextParser.prototype.getChildNodes = function (documentFragment) {
return documentFragment.childNodes;
};
return NodeRichTextParser;
}());
export { NodeRichTextParser };
//# sourceMappingURL=node-rich-text.parser.js.map