UNPKG

kentico-cloud-delivery

Version:

Official Kentico Cloud Delivery SDK

132 lines 5.79 kB
import { stronglyTypedResolver } from './strongly-type.resolver'; var RichTextResolver = /** @class */ (function () { function RichTextResolver() { /** * Type identifying nested modular content in Rich text fields */ this.modularContentobjectType = 'application/kenticocloud'; /** * This tag wil be used instead of 'object' */ this.modularContentTagWrapper = 'div'; /** * Attribute used to identify modular item based on its codename */ this.modularContentCodenameAttributeName = 'data-codename'; /** * Tag identifying links */ this.linkTag = 'a'; /** * Attributes that identifies if of the content item referenced in links */ this.linkContentItemIdAttributeName = 'data-item-id'; } /** * Resolves modular content inside the Rich text field. * Rich text resolved needs to be configured either on the model or query level */ RichTextResolver.prototype.resolveHtml = function (html, data) { var _this = this; // prepare config var config = { enableAdvancedLogging: data.enableAdvancedLogging, queryConfig: data.queryConfig, modularContentWrapperTag: data.modularContentWrapperTag, modularContentWrapperClasses: data.modularContentWrapperClasses }; var result = data.richTextHtmlParser.resolveRichTextField(html, { getLinkUrl: function (itemId) { return _this.getLinkUrl({ config: config, links: data.links, itemId: itemId, typeResolvers: data.typeResolvers }); }, getModularContentHtml: function (itemCodename) { return _this.getHtmlOfModularContent({ itemCodename: itemCodename, config: config, modularItems: data.modularItems }); } }, { enableAdvancedLogging: data.enableAdvancedLogging, queryConfig: data.queryConfig, modularContentWrapperTag: data.modularContentWrapperTag, modularContentWrapperClasses: data.modularContentWrapperClasses }); return result.resolvedHtml; }; RichTextResolver.prototype.getHtmlOfModularContent = function (data) { // get modular content item var modularContentItem = data.modularItems.find(function (m) { return m.system.codename === data.itemCodename; }); // check if modular content really exists if (!modularContentItem) { throw Error("Cannot resolve modular content in 'RichTextField' for '" + data.itemCodename + "' content item"); } // create new replacement object // get html to replace object using Rich text resolver function var resolver = undefined; if (data.config.queryConfig.richTextResolver) { // use resolved defined by query if available resolver = data.config.queryConfig.richTextResolver; } else { // use default resolver defined in models if (modularContentItem.richTextResolver) { resolver = modularContentItem.richTextResolver; } } // check resolver if (!resolver) { if (data.config.enableAdvancedLogging) { console.warn("Cannot resolve modular content of '" + modularContentItem.system.type + "' type in 'RichTextField' because no rich text resolved was configured"); return ''; } return ''; } return resolver(modularContentItem); }; RichTextResolver.prototype.getLinkUrl = function (data) { // find link with the id of content item var link = data.links.find(function (m) { return m.itemId === data.itemId; }); if (!link) { if (data.config.enableAdvancedLogging) { console.warn("Cannot resolve URL for item '" + data.itemId + "' because no link with this id was found"); } return ''; } // try to resolve link using the resolver passed through the query config var queryLinkResolver = data.config.queryConfig.linkResolver; var url; if (queryLinkResolver) { // try to resolve url using the query config url = queryLinkResolver(link); } if (!url) { // url was not resolved, try to find global resolver for this particular type var emptyTypedItem = stronglyTypedResolver.createEmptyTypedObj(link.type, data.typeResolvers); if (!emptyTypedItem) { if (data.config.enableAdvancedLogging) { console.warn("Cannot resolve link for link of '" + link.type + "' type with id '" + link.itemId + "' and url slug '" + link.urlSlug + "'"); } } else { var globalLinkResolver = emptyTypedItem.linkResolver; if (globalLinkResolver) { url = globalLinkResolver(link); } } } // url still wasn't resolved if (!url) { if (data.config.enableAdvancedLogging) { console.warn("Url for content type '" + link.type + "' with id '" + link.itemId + "' resolved to null/undefined"); } return ''; } return url; }; return RichTextResolver; }()); export { RichTextResolver }; export var richTextResolver = new RichTextResolver(); //# sourceMappingURL=rich-text.resolver.js.map