@contentstack/utils
Version:
Contentstack utilities for Javascript
51 lines • 2.35 kB
JavaScript
import { findRenderContent } from './helper/find-render-content';
import { findGQLEmbeddedItems } from './helper/find-embeded-object';
import { enumerate, enumerateContents } from './helper/enumerate-entries';
function jsonToHTML(option) {
if (option.entry instanceof Array) {
enumerate(option.entry, function (entry) {
jsonToHTML({ entry: entry, paths: option.paths, renderOption: option.renderOption });
});
}
else {
enumerateKeys({
entry: option.entry,
paths: option.paths,
renderOption: option.renderOption,
});
}
}
function enumerateKeys(option) {
for (var _i = 0, _a = option.paths; _i < _a.length; _i++) {
var key = _a[_i];
findRenderContent(key, option.entry, (function (content) {
if (content === null || content === void 0 ? void 0 : content.json) {
var edges = content.embedded_itemsConnection ? content.embedded_itemsConnection.edges : [];
var items_1 = Object.values(edges || []).reduce(function (accumulator, value) { return accumulator.concat(value.node); }, []);
return enumerateContents(content.json, option.renderOption, function (metadata) {
return findGQLEmbeddedItems(metadata, items_1)[0];
});
}
return content;
}));
}
}
/**
* GraphQL API utilities for Contentstack. Provides methods to work with
* content fetched via the GraphQL API, including rendering Supercharged RTE
* (JSON) with embedded items from the GQL response.
*/
export var GQL = {
/**
* Converts Supercharged RTE (JSON) content to HTML for entries from a GraphQL response.
* Uses `embedded_itemsConnection.edges` to resolve embedded items. Mutates the entry
* JSON in-place by replacing JSON RTE content with the generated HTML.
*
* @param option - Configuration for conversion.
* @param option.entry - Entry or array of entries (EmbeddedItem) from a GQL response with JSON RTE and embedded_itemsConnection.
* @param option.paths - Key paths to the JSON RTE fields on the entry.
* @param option.renderOption - Optional render options to customize how nodes and embedded items are rendered to HTML.
*/
jsonToHTML: jsonToHTML
};
//# sourceMappingURL=gql.js.map