@contentstack/utils
Version:
Contentstack utilities for Javascript
86 lines • 3.64 kB
JavaScript
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
import './extensions';
import { findEmbeddedItems, findRenderString } from './helper/find-embeded-object';
import { findRenderContent } from './helper/find-render-content';
/**
* Renders RTE (Rich Text Editor) content with embedded objects in-place.
* Mutates the entry/entries by replacing embedded item tags with HTML produced
* by the provided render options. Works with a single entry or an array of entries.
*
* @param option - Configuration for rendering.
* @param option.entry - Entry or array of entries containing RTE fields with embedded objects.
* @param option.renderOption - Optional render options (node/item handlers) to produce HTML for embedded content.
* @param option.paths - Optional key paths to specific RTE fields. If omitted, all RTE paths on the entry are rendered.
*/
export function render(option) {
function findContent(path, entry) {
findRenderContent(path, entry, function (content) {
return renderContent(content, { entry: entry, renderOption: option.renderOption });
});
}
function findAndRender(entry) {
if (!option.paths || option.paths.length === 0) {
Object.keys(__assign({}, entry._embedded_items)).forEach(function (path) {
findContent(path, entry);
});
}
else {
option.paths.forEach(function (path) {
findContent(path, entry);
});
}
}
if (option.entry instanceof Array) {
option.entry.forEach(function (entry) {
findAndRender(entry);
});
}
else {
findAndRender(option.entry);
}
}
/**
* Renders a single RTE content string or array of strings by replacing embedded
* item tags with HTML. Uses the entry and renderOption from the given option to
* resolve embedded references and produce output.
*
* @param content - RTE content string or array of strings containing embedded item tags.
* @param option - Must include the entry (for resolving embedded items) and optionally renderOption.
* @returns The same shape as content: a string or array of strings with embedded tags replaced by rendered HTML.
*/
export function renderContent(content, option) {
// return blank if content not present
if (!content || content === undefined) {
return '';
}
// render content of type string
if (typeof content === 'string') {
var contentToReplace_1 = content;
content.forEachEmbeddedItem(function (embededObjectTag, object) {
contentToReplace_1 = findAndReplaceEmbeddedItem(contentToReplace_1, embededObjectTag, object, option);
});
return contentToReplace_1;
}
// render content of type array of string
var resultContent = [];
content.forEach(function (element) {
resultContent.push(renderContent(element, option));
});
return resultContent;
}
function findAndReplaceEmbeddedItem(content, embededObjectTag, metadata, option) {
var embeddedObjects = findEmbeddedItems(metadata, option.entry);
var renderString = findRenderString(embeddedObjects[0], metadata, option.renderOption);
return content.replace(embededObjectTag, renderString);
}
//# sourceMappingURL=render-embedded-objects.js.map