@contentstack/utils
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -7,2 +7,60 @@ 'use strict'; | ||
function createMetadata(attribute) { | ||
return { | ||
text: attribute['#text'], | ||
itemUid: attribute["data-sys-entry-uid"] || attribute["data-sys-asset-uid"], | ||
itemType: attribute.type, | ||
styleType: attribute["sys-style-type"], | ||
attributes: attribute, | ||
contentTypeUid: attribute["data-sys-content-type-uid"], | ||
}; | ||
} | ||
function nodeToMetadata(attribute, textNode) { | ||
return { | ||
text: textNode.text, | ||
itemUid: attribute["entry-uid"] || attribute["asset-uid"], | ||
itemType: attribute.type, | ||
styleType: attribute["display-type"], | ||
attributes: attribute, | ||
contentTypeUid: attribute["content-type-uid"], | ||
}; | ||
} | ||
function attributeToString(attributes) { | ||
var result = ''; | ||
var _loop_1 = function (key) { | ||
if (Object.prototype.hasOwnProperty.call(attributes, key)) { | ||
var element = attributes[key]; | ||
if (element instanceof Array) { | ||
var elementString_1 = ''; | ||
var isFirst_1 = true; | ||
element.forEach(function (value) { | ||
if (isFirst_1) { | ||
elementString_1 += "" + value; | ||
isFirst_1 = false; | ||
} | ||
else { | ||
elementString_1 += ", " + value; | ||
} | ||
}); | ||
element = elementString_1; | ||
} | ||
else if (typeof element === 'object') { | ||
var elementString = ''; | ||
for (var elementKey in element) { | ||
if (Object.prototype.hasOwnProperty.call(element, elementKey)) { | ||
var value = element[elementKey]; | ||
elementString += elementKey + ":" + value + "; "; | ||
} | ||
} | ||
element = elementString; | ||
} | ||
result += " " + key + "=\"" + element + "\""; | ||
} | ||
}; | ||
for (var key in attributes) { | ||
_loop_1(key); | ||
} | ||
return result; | ||
} | ||
var StyleType; | ||
@@ -32,3 +90,17 @@ (function (StyleType) { | ||
***************************************************************************** */ | ||
/* global Reflect, Promise */ | ||
var extendStatics = function(d, b) { | ||
extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; | ||
return extendStatics(d, b); | ||
}; | ||
function __extends(d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
} | ||
var __assign = function() { | ||
@@ -64,16 +136,4 @@ __assign = Object.assign || function __assign(t) { | ||
function createMetadata(attribute) { | ||
var metadata = { | ||
text: attribute['#text'], | ||
itemUid: attribute["data-sys-entry-uid"] || attribute["data-sys-asset-uid"], | ||
itemType: attribute.type, | ||
styleType: attribute["sys-style-type"], | ||
attributes: attribute, | ||
contentTypeUid: attribute["data-sys-content-type-uid"] | ||
}; | ||
return metadata; | ||
} | ||
var frameflag = 'documentfragmentcontainer'; | ||
String.prototype.forEachEmbeddedObject = function (callbackfn) { | ||
String.prototype.forEachEmbeddedItem = function (callbackfn) { | ||
var str = "<" + frameflag + ">" + this.toString() + "</" + frameflag + ">"; | ||
@@ -93,9 +153,9 @@ var root = nodeHtmlParser.parse(str); | ||
var defaultOptions = (_a = {}, | ||
_a[StyleType$1.BLOCK] = function (entry) { | ||
return "<div><p>" + (entry.title || entry.uid) + "</p><p>Content type: <span>" + entry._content_type_uid + "</span></p></div>"; | ||
_a[StyleType$1.BLOCK] = function (item) { | ||
return "<div><p>" + (item.title || item.uid) + "</p><p>Content type: <span>" + item._content_type_uid + "</span></p></div>"; | ||
}, | ||
_a[StyleType$1.INLINE] = function (entry) { return "<span>" + (entry.title || entry.uid) + "</span>"; }, | ||
_a[StyleType$1.LINK] = function (entry, metadata) { return "<a href=\"" + entry.url + "\">" + (metadata.text || entry.title || entry.uid) + "</a>"; }, | ||
_a[StyleType$1.DISPLAY] = function (asset, metadata) { return "<img src=\"" + asset.url + "\" alt=\"" + (metadata.attributes.alt || asset.title || asset.filename || asset.uid) + "\" />"; }, | ||
_a[StyleType$1.DOWNLOAD] = function (entry, metadata) { return "<a href=\"" + entry.url + "\">" + (metadata.text || entry.title || entry.uid) + "</a>"; }, | ||
_a[StyleType$1.INLINE] = function (item) { return "<span>" + (item.title || item.uid) + "</span>"; }, | ||
_a[StyleType$1.LINK] = function (item, metadata) { return "<a href=\"" + item.url + "\">" + (metadata.text || item.title || item.uid) + "</a>"; }, | ||
_a[StyleType$1.DISPLAY] = function (item, metadata) { return "<img src=\"" + item.url + "\" alt=\"" + (metadata.attributes.alt || item.title || item.filename || item.uid) + "\" />"; }, | ||
_a[StyleType$1.DOWNLOAD] = function (item, metadata) { return "<a href=\"" + item.url + "\">" + (metadata.text || item.title || item.uid) + "</a>"; }, | ||
_a); | ||
@@ -120,3 +180,3 @@ | ||
} | ||
function findEmbeddedObjects(object, entry) { | ||
function findEmbeddedItems(object, entry) { | ||
if (object && object !== undefined && entry && entry !== undefined) { | ||
@@ -135,4 +195,4 @@ if (entry._embedded_items !== undefined) { | ||
} | ||
function findRenderString(metadata, renderModel, renderOptions) { | ||
if ((!renderModel && renderModel === undefined) || (!metadata && metadata === undefined)) { | ||
function findRenderString(item, metadata, renderOptions) { | ||
if ((!item && item === undefined) || (!metadata && metadata === undefined)) { | ||
return ''; | ||
@@ -145,3 +205,3 @@ } | ||
renderFunction[metadata.attributes['data-sys-content-type-uid']] !== undefined) { | ||
return renderFunction[metadata.attributes['data-sys-content-type-uid']](renderModel, metadata); | ||
return renderFunction[metadata.attributes['data-sys-content-type-uid']](item, metadata); | ||
} | ||
@@ -151,10 +211,20 @@ else if (metadata.attributes['data-sys-content-type-uid'] !== undefined && | ||
renderFunction.$default !== undefined) { | ||
return renderFunction.$default(renderModel, metadata); | ||
return renderFunction.$default(item, metadata); | ||
} | ||
else if (metadata.contentTypeUid !== undefined && | ||
typeof renderFunction !== 'function' && | ||
renderFunction[metadata.contentTypeUid] !== undefined) { | ||
return renderFunction[metadata.contentTypeUid](item, metadata); | ||
} | ||
else if (metadata.contentTypeUid !== undefined && | ||
typeof renderFunction !== 'function' && | ||
renderFunction.$default !== undefined) { | ||
return renderFunction.$default(item, metadata); | ||
} | ||
else if (typeof renderFunction === 'function') { | ||
return renderFunction(renderModel, metadata); | ||
return renderFunction(item, metadata); | ||
} | ||
} | ||
var defaultRenderFunction = defaultOptions[metadata.styleType]; | ||
return defaultRenderFunction(renderModel, metadata); | ||
return defaultRenderFunction(item, metadata); | ||
} | ||
@@ -176,4 +246,5 @@ | ||
// tslint:disable-next-line: prefer-for-of | ||
for (var i = 0; i < object[key].length; i++) { | ||
getContent(newKeys, object[key][i], render); | ||
for (var _i = 0, _a = object[key]; _i < _a.length; _i++) { | ||
var objKey = _a[_i]; | ||
getContent(newKeys, objKey, render); | ||
} | ||
@@ -236,4 +307,4 @@ } | ||
var contentToReplace_1 = content; | ||
content.forEachEmbeddedObject(function (embededObjectTag, object) { | ||
contentToReplace_1 = findAndReplaceEmbeddedObject(contentToReplace_1, embededObjectTag, object, option); | ||
content.forEachEmbeddedItem(function (embededObjectTag, object) { | ||
contentToReplace_1 = findAndReplaceEmbeddedItem(contentToReplace_1, embededObjectTag, object, option); | ||
}); | ||
@@ -249,11 +320,280 @@ return contentToReplace_1; | ||
} | ||
function findAndReplaceEmbeddedObject(content, embededObjectTag, object, option) { | ||
var embeddedObjects = findEmbeddedObjects(object, option.entry); | ||
var renderString = findRenderString(object, embeddedObjects[0], option.renderOption); | ||
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); | ||
} | ||
var NodeType; | ||
(function (NodeType) { | ||
NodeType["DOCUMENT"] = "doc"; | ||
NodeType["PARAGRAPH"] = "p"; | ||
NodeType["LINK"] = "a"; | ||
NodeType["IMAGE"] = "img"; | ||
NodeType["EMBED"] = "embed"; | ||
NodeType["HEADING_1"] = "h1"; | ||
NodeType["HEADING_2"] = "h2"; | ||
NodeType["HEADING_3"] = "h3"; | ||
NodeType["HEADING_4"] = "h4"; | ||
NodeType["HEADING_5"] = "h5"; | ||
NodeType["HEADING_6"] = "h6"; | ||
NodeType["ORDER_LIST"] = "ol"; | ||
NodeType["UNORDER_LIST"] = "ul"; | ||
NodeType["LIST_ITEM"] = "li"; | ||
NodeType["HR"] = "hr"; | ||
NodeType["TABLE"] = "table"; | ||
NodeType["TABLE_HEADER"] = "thead"; | ||
NodeType["TABLE_BODY"] = "tbody"; | ||
NodeType["TABLE_FOOTER"] = "tfoot"; | ||
NodeType["TABLE_ROW"] = "tr"; | ||
NodeType["TABLE_HEAD"] = "th"; | ||
NodeType["TABLE_DATA"] = "td"; | ||
NodeType["BLOCK_QUOTE"] = "blockquote"; | ||
NodeType["CODE"] = "code"; | ||
NodeType["TEXT"] = "text"; | ||
NodeType["REFERENCE"] = "reference"; | ||
})(NodeType || (NodeType = {})); | ||
var NodeType$1 = NodeType; | ||
var MarkType; | ||
(function (MarkType) { | ||
MarkType["BOLD"] = "bold"; | ||
MarkType["ITALIC"] = "italic"; | ||
MarkType["UNDERLINE"] = "underline"; | ||
MarkType["STRIKE_THROUGH"] = "strikethrough"; | ||
MarkType["INLINE_CODE"] = "inlineCode"; | ||
MarkType["SUBSCRIPT"] = "subscript"; | ||
MarkType["SUPERSCRIPT"] = "superscript"; | ||
})(MarkType || (MarkType = {})); | ||
var MarkType$1 = MarkType; | ||
var Node = /** @class */ (function () { | ||
function Node() { | ||
} | ||
return Node; | ||
}()); | ||
var Document = /** @class */ (function (_super) { | ||
__extends(Document, _super); | ||
function Document() { | ||
var _this = _super.call(this) || this; | ||
_this.type = NodeType$1.DOCUMENT; | ||
return _this; | ||
} | ||
return Document; | ||
}(Node)); | ||
var TextNode = /** @class */ (function (_super) { | ||
__extends(TextNode, _super); | ||
function TextNode(text) { | ||
var _this = _super.call(this) || this; | ||
_this.text = text; | ||
return _this; | ||
} | ||
return TextNode; | ||
}(Node)); | ||
var _a$1; | ||
var defaultNodeOption = (_a$1 = {}, | ||
_a$1[NodeType$1.DOCUMENT] = function (node) { | ||
return ""; | ||
}, | ||
_a$1[NodeType$1.PARAGRAPH] = function (node, next) { | ||
return "<p>" + next(node.children) + "</p>"; | ||
}, | ||
_a$1[NodeType$1.LINK] = function (node, next) { | ||
return "<a href=\"" + node.attrs.href + "\">" + next(node.children) + "</a>"; | ||
}, | ||
_a$1[NodeType$1.IMAGE] = function (node, next) { | ||
return "<img src=\"" + node.attrs.src + "\" />" + next(node.children); | ||
}, | ||
_a$1[NodeType$1.EMBED] = function (node, next) { | ||
return "<iframe src=\"" + node.attrs.src + "\">" + next(node.children) + "</iframe>"; | ||
}, | ||
_a$1[NodeType$1.HEADING_1] = function (node, next) { | ||
return "<h1>" + next(node.children) + "</h1>"; | ||
}, | ||
_a$1[NodeType$1.HEADING_2] = function (node, next) { | ||
return "<h2>" + next(node.children) + "</h2>"; | ||
}, | ||
_a$1[NodeType$1.HEADING_3] = function (node, next) { | ||
return "<h3>" + next(node.children) + "</h3>"; | ||
}, | ||
_a$1[NodeType$1.HEADING_4] = function (node, next) { | ||
return "<h4>" + next(node.children) + "</h4>"; | ||
}, | ||
_a$1[NodeType$1.HEADING_5] = function (node, next) { | ||
return "<h5>" + next(node.children) + "</h5>"; | ||
}, | ||
_a$1[NodeType$1.HEADING_6] = function (node, next) { | ||
return "<h6>" + next(node.children) + "</h6>"; | ||
}, | ||
_a$1[NodeType$1.ORDER_LIST] = function (node, next) { | ||
return "<ol>" + next(node.children) + "</ol>"; | ||
}, | ||
_a$1[NodeType$1.UNORDER_LIST] = function (node, next) { | ||
return "<ul>" + next(node.children) + "</ul>"; | ||
}, | ||
_a$1[NodeType$1.LIST_ITEM] = function (node, next) { | ||
return "<li>" + next(node.children) + "</li>"; | ||
}, | ||
_a$1[NodeType$1.HR] = function (node, next) { | ||
return "<hr>"; | ||
}, | ||
_a$1[NodeType$1.TABLE] = function (node, next) { | ||
return "<table>" + next(node.children) + "</table>"; | ||
}, | ||
_a$1[NodeType$1.TABLE_HEADER] = function (node, next) { | ||
return "<thead>" + next(node.children) + "</thead>"; | ||
}, | ||
_a$1[NodeType$1.TABLE_BODY] = function (node, next) { | ||
return "<tbody>" + next(node.children) + "</tbody>"; | ||
}, | ||
_a$1[NodeType$1.TABLE_FOOTER] = function (node, next) { | ||
return "<tfoot>" + next(node.children) + "</tfoot>"; | ||
}, | ||
_a$1[NodeType$1.TABLE_ROW] = function (node, next) { | ||
return "<tr>" + next(node.children) + "</tr>"; | ||
}, | ||
_a$1[NodeType$1.TABLE_HEAD] = function (node, next) { | ||
return "<th>" + next(node.children) + "</th>"; | ||
}, | ||
_a$1[NodeType$1.TABLE_DATA] = function (node, next) { | ||
return "<td>" + next(node.children) + "</td>"; | ||
}, | ||
_a$1[NodeType$1.BLOCK_QUOTE] = function (node, next) { | ||
return "<blockquote>" + next(node.children) + "</blockquote>"; | ||
}, | ||
_a$1[NodeType$1.CODE] = function (node, next) { | ||
return "<code>" + next(node.children) + "</code>"; | ||
}, | ||
_a$1['reference'] = function (node, next) { | ||
return ""; | ||
}, | ||
_a$1[MarkType$1.BOLD] = function (text) { | ||
return "<strong>" + text + "</strong>"; | ||
}, | ||
_a$1[MarkType$1.ITALIC] = function (text) { | ||
return "<em>" + text + "</em>"; | ||
}, | ||
_a$1[MarkType$1.UNDERLINE] = function (text) { | ||
return "<u>" + text + "</u>"; | ||
}, | ||
_a$1[MarkType$1.STRIKE_THROUGH] = function (text) { | ||
return "<strike>" + text + "</strike>"; | ||
}, | ||
_a$1[MarkType$1.INLINE_CODE] = function (text) { | ||
return "<span>" + text + "</span>"; | ||
}, | ||
_a$1[MarkType$1.SUBSCRIPT] = function (text) { | ||
return "<sub>" + text + "</sub>"; | ||
}, | ||
_a$1[MarkType$1.SUPERSCRIPT] = function (text) { | ||
return "<sup>" + text + "</sup>"; | ||
}, | ||
_a$1); | ||
function jsonToHTML(option) { | ||
if (option.entry instanceof Array) { | ||
enumerateEntries({ | ||
entry: option.entry, | ||
paths: option.paths, | ||
renderOption: option.renderOption, | ||
}); | ||
} | ||
else { | ||
enumerateKeys({ | ||
entry: option.entry, | ||
paths: option.paths, | ||
renderOption: option.renderOption, | ||
}); | ||
} | ||
} | ||
function enumerateEntries(option) { | ||
for (var _i = 0, _a = option.entry; _i < _a.length; _i++) { | ||
var entry = _a[_i]; | ||
jsonToHTML({ entry: 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) { | ||
return enumerateContents(content, option.entry, option.renderOption); | ||
})); | ||
} | ||
} | ||
function enumerateContents(content, entry, renderOption) { | ||
if (!(content instanceof Array) && content.type !== 'doc') { | ||
return content; | ||
} | ||
if (content instanceof Array) { | ||
var result_1 = []; | ||
content.forEach(function (doc) { | ||
result_1.push(enumerateContents(doc, entry, renderOption)); | ||
}); | ||
return result_1; | ||
} | ||
var commonRenderOption = __assign(__assign({}, defaultNodeOption), renderOption); | ||
return nodeChildrenToHTML(content.children, commonRenderOption, entry); | ||
} | ||
function textNodeToHTML(node, renderOption) { | ||
var text = node.text; | ||
if (node.superscript) { | ||
text = renderOption[MarkType$1.SUPERSCRIPT](text); | ||
} | ||
if (node.subscript) { | ||
text = renderOption[MarkType$1.SUBSCRIPT](text); | ||
} | ||
if (node.inlineCode) { | ||
text = renderOption[MarkType$1.INLINE_CODE](text); | ||
} | ||
if (node.strikethrough) { | ||
text = renderOption[MarkType$1.STRIKE_THROUGH](text); | ||
} | ||
if (node.underline) { | ||
text = renderOption[MarkType$1.UNDERLINE](text); | ||
} | ||
if (node.italic) { | ||
text = renderOption[MarkType$1.ITALIC](text); | ||
} | ||
if (node.bold) { | ||
text = renderOption[MarkType$1.BOLD](text); | ||
} | ||
return text; | ||
} | ||
function referenceToHTML(node, renderOption, entry) { | ||
if (!entry) { | ||
return ''; | ||
} | ||
var metadata = nodeToMetadata(node.attrs, ((node.children && node.children.length > 0) ? node.children[0] : {})); | ||
var item = findEmbeddedItems(metadata, entry)[0]; | ||
return findRenderString(item, metadata, renderOption); | ||
} | ||
function nodeChildrenToHTML(nodes, renderOption, entry) { | ||
return nodes.map(function (node) { return nodeToHTML(node, renderOption, entry); }).join(''); | ||
} | ||
function nodeToHTML(node, renderOption, entry) { | ||
if (!node.type) { | ||
return textNodeToHTML(node, renderOption); | ||
} | ||
else if (node.type === 'reference') { | ||
return referenceToHTML(node, renderOption, entry); | ||
} | ||
else { | ||
var next = function (nodes) { return nodeChildrenToHTML(nodes, renderOption, entry); }; | ||
return renderOption[node.type](node, next); | ||
} | ||
} | ||
exports.Document = Document; | ||
exports.MarkType = MarkType$1; | ||
exports.Node = Node; | ||
exports.NodeType = NodeType$1; | ||
exports.StyleType = StyleType$1; | ||
exports.TextNode = TextNode; | ||
exports.attributeToString = attributeToString; | ||
exports.jsonToHTML = jsonToHTML; | ||
exports.render = render; | ||
exports.renderContent = renderContent; | ||
//# sourceMappingURL=index.es.js.map |
@@ -5,3 +5,3 @@ import { elementToJson } from '../helper/html-to-json'; | ||
var frameflag = 'documentfragmentcontainer'; | ||
String.prototype.forEachEmbeddedObject = function (callbackfn) { | ||
String.prototype.forEachEmbeddedItem = function (callbackfn) { | ||
var str = "<" + frameflag + ">" + this.toString() + "</" + frameflag + ">"; | ||
@@ -8,0 +8,0 @@ var root = parse(str); |
@@ -19,3 +19,3 @@ import { defaultOptions } from '../options/default-options'; | ||
} | ||
export function findEmbeddedObjects(object, entry) { | ||
export function findEmbeddedItems(object, entry) { | ||
if (object && object !== undefined && entry && entry !== undefined) { | ||
@@ -34,4 +34,4 @@ if (entry._embedded_items !== undefined) { | ||
} | ||
export function findRenderString(metadata, renderModel, renderOptions) { | ||
if ((!renderModel && renderModel === undefined) || (!metadata && metadata === undefined)) { | ||
export function findRenderString(item, metadata, renderOptions) { | ||
if ((!item && item === undefined) || (!metadata && metadata === undefined)) { | ||
return ''; | ||
@@ -44,3 +44,3 @@ } | ||
renderFunction[metadata.attributes['data-sys-content-type-uid']] !== undefined) { | ||
return renderFunction[metadata.attributes['data-sys-content-type-uid']](renderModel, metadata); | ||
return renderFunction[metadata.attributes['data-sys-content-type-uid']](item, metadata); | ||
} | ||
@@ -50,11 +50,21 @@ else if (metadata.attributes['data-sys-content-type-uid'] !== undefined && | ||
renderFunction.$default !== undefined) { | ||
return renderFunction.$default(renderModel, metadata); | ||
return renderFunction.$default(item, metadata); | ||
} | ||
else if (metadata.contentTypeUid !== undefined && | ||
typeof renderFunction !== 'function' && | ||
renderFunction[metadata.contentTypeUid] !== undefined) { | ||
return renderFunction[metadata.contentTypeUid](item, metadata); | ||
} | ||
else if (metadata.contentTypeUid !== undefined && | ||
typeof renderFunction !== 'function' && | ||
renderFunction.$default !== undefined) { | ||
return renderFunction.$default(item, metadata); | ||
} | ||
else if (typeof renderFunction === 'function') { | ||
return renderFunction(renderModel, metadata); | ||
return renderFunction(item, metadata); | ||
} | ||
} | ||
var defaultRenderFunction = defaultOptions[metadata.styleType]; | ||
return defaultRenderFunction(renderModel, metadata); | ||
return defaultRenderFunction(item, metadata); | ||
} | ||
//# sourceMappingURL=find-embeded-object.js.map |
@@ -15,4 +15,5 @@ export function findRenderContent(keyPaths, entry, render) { | ||
// tslint:disable-next-line: prefer-for-of | ||
for (var i = 0; i < object[key].length; i++) { | ||
getContent(newKeys, object[key][i], render); | ||
for (var _i = 0, _a = object[key]; _i < _a.length; _i++) { | ||
var objKey = _a[_i]; | ||
getContent(newKeys, objKey, render); | ||
} | ||
@@ -19,0 +20,0 @@ } |
@@ -0,3 +1,10 @@ | ||
export { attributeToString } from './Models/metadata-model'; | ||
export { default as StyleType } from './embedded-types/style-type'; | ||
export { render, renderContent } from './render-embedded-objects'; | ||
export { default as NodeType } from './nodes/node-type'; | ||
export { default as MarkType } from './nodes/mark-type'; | ||
export { default as Node } from './nodes/node'; | ||
export { default as Document } from './nodes/document'; | ||
export { default as TextNode } from './nodes/text'; | ||
export { jsonToHTML } from './json-to-html'; | ||
//# sourceMappingURL=index.js.map |
export function createMetadata(attribute) { | ||
var metadata = { | ||
return { | ||
text: attribute['#text'], | ||
@@ -8,6 +8,52 @@ itemUid: attribute["data-sys-entry-uid"] || attribute["data-sys-asset-uid"], | ||
attributes: attribute, | ||
contentTypeUid: attribute["data-sys-content-type-uid"] | ||
contentTypeUid: attribute["data-sys-content-type-uid"], | ||
}; | ||
return metadata; | ||
} | ||
export function nodeToMetadata(attribute, textNode) { | ||
return { | ||
text: textNode.text, | ||
itemUid: attribute["entry-uid"] || attribute["asset-uid"], | ||
itemType: attribute.type, | ||
styleType: attribute["display-type"], | ||
attributes: attribute, | ||
contentTypeUid: attribute["content-type-uid"], | ||
}; | ||
} | ||
export function attributeToString(attributes) { | ||
var result = ''; | ||
var _loop_1 = function (key) { | ||
if (Object.prototype.hasOwnProperty.call(attributes, key)) { | ||
var element = attributes[key]; | ||
if (element instanceof Array) { | ||
var elementString_1 = ''; | ||
var isFirst_1 = true; | ||
element.forEach(function (value) { | ||
if (isFirst_1) { | ||
elementString_1 += "" + value; | ||
isFirst_1 = false; | ||
} | ||
else { | ||
elementString_1 += ", " + value; | ||
} | ||
}); | ||
element = elementString_1; | ||
} | ||
else if (typeof element === 'object') { | ||
var elementString = ''; | ||
for (var elementKey in element) { | ||
if (Object.prototype.hasOwnProperty.call(element, elementKey)) { | ||
var value = element[elementKey]; | ||
elementString += elementKey + ":" + value + "; "; | ||
} | ||
} | ||
element = elementString; | ||
} | ||
result += " " + key + "=\"" + element + "\""; | ||
} | ||
}; | ||
for (var key in attributes) { | ||
_loop_1(key); | ||
} | ||
return result; | ||
} | ||
//# sourceMappingURL=metadata-model.js.map |
var _a; | ||
import StyleType from '../embedded-types/style-type'; | ||
export var defaultOptions = (_a = {}, | ||
_a[StyleType.BLOCK] = function (entry) { | ||
return "<div><p>" + (entry.title || entry.uid) + "</p><p>Content type: <span>" + entry._content_type_uid + "</span></p></div>"; | ||
_a[StyleType.BLOCK] = function (item) { | ||
return "<div><p>" + (item.title || item.uid) + "</p><p>Content type: <span>" + item._content_type_uid + "</span></p></div>"; | ||
}, | ||
_a[StyleType.INLINE] = function (entry) { return "<span>" + (entry.title || entry.uid) + "</span>"; }, | ||
_a[StyleType.LINK] = function (entry, metadata) { return "<a href=\"" + entry.url + "\">" + (metadata.text || entry.title || entry.uid) + "</a>"; }, | ||
_a[StyleType.DISPLAY] = function (asset, metadata) { return "<img src=\"" + asset.url + "\" alt=\"" + (metadata.attributes.alt || asset.title || asset.filename || asset.uid) + "\" />"; }, | ||
_a[StyleType.DOWNLOAD] = function (entry, metadata) { return "<a href=\"" + entry.url + "\">" + (metadata.text || entry.title || entry.uid) + "</a>"; }, | ||
_a[StyleType.INLINE] = function (item) { return "<span>" + (item.title || item.uid) + "</span>"; }, | ||
_a[StyleType.LINK] = function (item, metadata) { return "<a href=\"" + item.url + "\">" + (metadata.text || item.title || item.uid) + "</a>"; }, | ||
_a[StyleType.DISPLAY] = function (item, metadata) { return "<img src=\"" + item.url + "\" alt=\"" + (metadata.attributes.alt || item.title || item.filename || item.uid) + "\" />"; }, | ||
_a[StyleType.DOWNLOAD] = function (item, metadata) { return "<a href=\"" + item.url + "\">" + (metadata.text || item.title || item.uid) + "</a>"; }, | ||
_a); | ||
//# sourceMappingURL=default-options.js.map |
@@ -13,3 +13,3 @@ var __assign = (this && this.__assign) || function () { | ||
import './extensions'; | ||
import { findEmbeddedObjects, findRenderString } from './helper/find-embeded-object'; | ||
import { findEmbeddedItems, findRenderString } from './helper/find-embeded-object'; | ||
import { findRenderContent } from './helper/find-render-content'; | ||
@@ -63,4 +63,4 @@ /** | ||
var contentToReplace_1 = content; | ||
content.forEachEmbeddedObject(function (embededObjectTag, object) { | ||
contentToReplace_1 = findAndReplaceEmbeddedObject(contentToReplace_1, embededObjectTag, object, option); | ||
content.forEachEmbeddedItem(function (embededObjectTag, object) { | ||
contentToReplace_1 = findAndReplaceEmbeddedItem(contentToReplace_1, embededObjectTag, object, option); | ||
}); | ||
@@ -76,7 +76,7 @@ return contentToReplace_1; | ||
} | ||
function findAndReplaceEmbeddedObject(content, embededObjectTag, object, option) { | ||
var embeddedObjects = findEmbeddedObjects(object, option.entry); | ||
var renderString = findRenderString(object, embeddedObjects[0], option.renderOption); | ||
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 |
import { Metadata } from '../Models/metadata-model'; | ||
declare global { | ||
interface String { | ||
forEachEmbeddedObject(callbackfn: (embededObjectTag: string, object: Metadata) => void): void; | ||
forEachEmbeddedItem(callbackfn: (embededObjectTag: string, object: Metadata) => void): void; | ||
} | ||
} |
@@ -1,7 +0,7 @@ | ||
import { EntryEmbedable, EmbeddedObject } from '../Models/embedded-object'; | ||
import { EntryEmbedable, EmbeddedItem } from '../Models/embedded-object'; | ||
import { RenderOption } from '../options/index'; | ||
import { Metadata } from '../Models/metadata-model'; | ||
export declare function findEmbeddedEntry(uid: string, contentTypeUid: string, embeddeditems?: EmbeddedObject[]): EmbeddedObject[]; | ||
export declare function findEmbeddedAsset(uid: string, embeddedAssets?: EmbeddedObject[]): EmbeddedObject[]; | ||
export declare function findEmbeddedObjects(object: Metadata, entry: EntryEmbedable): (EmbeddedObject)[]; | ||
export declare function findRenderString(metadata: Metadata, renderModel: EmbeddedObject, renderOptions?: RenderOption): string; | ||
export declare function findEmbeddedEntry(uid: string, contentTypeUid: string, embeddeditems?: EmbeddedItem[]): EmbeddedItem[]; | ||
export declare function findEmbeddedAsset(uid: string, embeddedAssets?: EmbeddedItem[]): EmbeddedItem[]; | ||
export declare function findEmbeddedItems(object: Metadata, entry: EntryEmbedable): (EmbeddedItem)[]; | ||
export declare function findRenderString(item: EmbeddedItem, metadata: Metadata, renderOptions?: RenderOption): string; |
import { EntryEmbedable } from '../Models/embedded-object'; | ||
export declare function findRenderContent(keyPaths: string, entry: EntryEmbedable, render: (content: string | string[]) => (string | string[])): void; | ||
export declare function getContent(keys: string[], object: any, render: (content: string | string[]) => (string | string[])): void; | ||
export declare function findRenderContent<Type>(keyPaths: string, entry: EntryEmbedable, render: (content: Type) => (string | string[])): void; | ||
export declare function getContent<Type>(keys: string[], object: any, render: (content: Type) => (string | string[])): void; |
@@ -1,5 +0,11 @@ | ||
export { Option, RenderContentType, RenderOption, RenderObject } from './options/index'; | ||
export { EntryEmbedable, EmbeddedObject as EntryModel } from './Models/embedded-object'; | ||
export { Metadata } from './Models/metadata-model'; | ||
export { Next, Option, RenderNode, RenderMark, RenderItem, RenderContentType, RenderOption } from './options/index'; | ||
export { EntryEmbedable, EmbeddedItem as EntryModel } from './Models/embedded-object'; | ||
export { Metadata, Attributes, attributeToString } from './Models/metadata-model'; | ||
export { default as StyleType } from './embedded-types/style-type'; | ||
export { render, renderContent } from './render-embedded-objects'; | ||
export { default as NodeType } from './nodes/node-type'; | ||
export { default as MarkType } from './nodes/mark-type'; | ||
export { default as Node } from './nodes/node'; | ||
export { default as Document } from './nodes/document'; | ||
export { default as TextNode } from './nodes/text'; | ||
export { jsonToHTML } from './json-to-html'; |
@@ -1,2 +0,2 @@ | ||
export interface EmbeddedObject { | ||
export interface EmbeddedItem { | ||
uid: string; | ||
@@ -11,4 +11,4 @@ _content_type_uid: string; | ||
uid: string; | ||
_embedded_items?: EmbedModel<EmbeddedObject>; | ||
_embedded_items?: EmbedModel<EmbeddedItem>; | ||
[propName: string]: any; | ||
} |
import StyleType from '../embedded-types/style-type'; | ||
import TextNode from '../nodes/text'; | ||
export interface Metadata { | ||
text: string; | ||
itemUid: string; | ||
itemType: 'entry' | 'asset'; | ||
styleType: StyleType; | ||
attributes: Attributes; | ||
itemUid: string | undefined; | ||
itemType: 'entry' | 'asset' | undefined; | ||
styleType: StyleType | undefined; | ||
contentTypeUid: string | undefined; | ||
} | ||
export interface Attributes { | ||
type: 'entry' | 'asset'; | ||
class: string; | ||
type?: 'entry' | 'asset'; | ||
class?: string; | ||
[key: string]: any; | ||
'sys-style-type': string; | ||
'sys-style-type'?: string; | ||
} | ||
@@ -27,1 +28,3 @@ export interface EntryAttributes extends Attributes { | ||
export declare function createMetadata(attribute: Attributes): Metadata; | ||
export declare function nodeToMetadata(attribute: Attributes, textNode: TextNode): Metadata; | ||
export declare function attributeToString(attributes: Attributes): string; |
@@ -1,9 +0,14 @@ | ||
import { EntryEmbedable, EmbeddedObject } from '../Models/embedded-object'; | ||
import { AnyNode } from '../json-to-html'; | ||
import { EntryEmbedable, EmbeddedItem } from '../Models/embedded-object'; | ||
import { Metadata } from '../Models/metadata-model'; | ||
export declare type RenderObject = (object: EmbeddedObject, metadata: Metadata) => string; | ||
import Node from '../nodes/node'; | ||
export declare type Next = (nodes: AnyNode[]) => string; | ||
export declare type RenderNode = (node: Node, next: Next) => string; | ||
export declare type RenderMark = (text: string) => string; | ||
export declare type RenderItem = (item: EmbeddedItem, metadata: Metadata) => string; | ||
export interface RenderOption { | ||
[embedType: string]: RenderObject | RenderContentType; | ||
[embedType: string]: RenderNode | RenderMark | RenderItem | RenderContentType; | ||
} | ||
export interface RenderContentType { | ||
[contentTypeUid: string]: RenderObject; | ||
[contentTypeUid: string]: RenderItem; | ||
} | ||
@@ -10,0 +15,0 @@ export interface Option { |
{ | ||
"name": "@contentstack/utils", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Contentstack utilities for Javascript", | ||
@@ -21,3 +21,3 @@ "main": "dist/index.es.js", | ||
"scripts": { | ||
"test": "jest --config jestconfig.json ", | ||
"test": "jest --config jestconfig.json", | ||
"test:badges": "npm run test && jest-coverage-badges --output ./badges", | ||
@@ -24,0 +24,0 @@ "test:debug": "jest --watchAll --config jestconfig.json --runInBand", |
121
README.md
@@ -25,37 +25,48 @@ ![Coverage statements](./badges/badge-statements.svg?raw=true) | ||
## Usage | ||
Let’s learn how you can use Utils SDK to render embedded items. | ||
Let’s learn how you can use Utils SDK to render RTE embedded items and Supercharged RTE Json to HTML. | ||
### Create Render Option | ||
To render embedded items on the front-end, use the renderOptions function, and define the UI elements you want to show in the front-end of your website, as shown in the example below: | ||
To render embedded items on the front-end, use the renderOptions function, and define the UI elements you want to show in the front-end of your website, as shown in the example below: | ||
```js | ||
const renderOptions = { | ||
//to render block-type embedded items | ||
// to render Supercharged RTE NodeType content like paragraph, link, table, order list, un-order list and more. | ||
p: (node, next) => { | ||
`<p class='class-id'>${next(node.children)}</p>` // you will need to call next function with node children contents | ||
} | ||
h1: (node, next) => { | ||
`<h1 class='class-id'>${next(node.children)}</h1>` // you will need to call next function with node children contents | ||
} | ||
// to render Supercharged RTE MarkType content like bold, italic, underline, strikethrough, inlineCode, subscript, and superscript | ||
bold: (text) => { | ||
`<b>${next(node.children)}</b>` | ||
} | ||
// to render block-type embedded items | ||
block: { | ||
'product': (entry, metadata) => { | ||
`<div> | ||
<h2 >${entry.title}</h2> | ||
<img src=${entry.product_image.url} alt=${entry.product_image.title}/> | ||
<p>${entry.price}</p> | ||
</div>` | ||
'product': (item, metadata) => { | ||
`<div> | ||
<h2 >${item.title}</h2> | ||
<img src=${item.product_image.url} alt=${item.product_image.title}/> | ||
<p>${item.price}</p> | ||
</div>` | ||
}, | ||
//to render the default | ||
'$default': (entry, metadata) => { | ||
`<div> | ||
<h2>${entry.title}</h2> | ||
<p>${ntry.description}</p> | ||
</div>` | ||
// to render the default | ||
'$default': (item, metadata) => { | ||
`<div> | ||
<h2>${item.title}</h2> | ||
<p>${item.description}</p> | ||
</div>` | ||
} | ||
}, | ||
//to display inline embedded items | ||
// to display inline embedded items | ||
inline: { | ||
'$default': (entry) => { | ||
`<span><b>${entry.title}</b> - ${entry.description}</span>` | ||
} | ||
'$default': (item, metadata) => { | ||
`<span><b>${item.title}</b> - ${item.description}</span>` | ||
} | ||
}, | ||
//to display embedded items inserted via link | ||
link: (entry, metadata) => { | ||
// to display embedded items inserted via link | ||
link: (item, metadata) => { | ||
`<a href="${metadata.attributes.href}">${metadata.text}</a>` | ||
}, | ||
// to display assets | ||
display: (asset, metadata) => { | ||
display: (item, metadata) => { | ||
`<img src=${metadata.attributes.src} alt=${metadata.alt} />` | ||
@@ -70,3 +81,4 @@ } | ||
### Fetch Embedded Item(s) from a Single Entry | ||
To get an embedded item of a single entry, you need to provide the stack API key, environment name, delivery token, content type and entry UID. Then, use the includeEmbeddedItems and Contentstack.Utils.render functions as shown below: | ||
#### Render HTML RTE Embedded object | ||
To get an embedded item of a single entry, you need to provide the stack API key, environment name, delivery token, content type and entry UID. Then, use the `includeEmbeddedItems` and `Contentstack.Utils.render` functions as shown below: | ||
```js | ||
@@ -93,5 +105,31 @@ import * as Contentstack from 'contentstack' | ||
//code to render embedded item from an RTE field and from another RTE field nested within a group field | ||
Contentstack.Utils.render({ entry, path: ["rte_fieldUid", "group.rtefieldUID"], renderOption }) | ||
Contentstack.Utils.render({ entry, path: ["rte_fieldUid", "group.rteFieldUID"], renderOption }) | ||
``` | ||
#### Render Supercharged RTE contents | ||
To get a single entry, you need to provide the stack API key, environment name, delivery token, content type and entry UID. Then, use `Contentstack.Utils.jsonToHtml` function as shown below: | ||
```js | ||
import * as Contentstack from 'contentstack' | ||
const stack = Contentstack.Stack({ | ||
api_key: '<API_KEY>', | ||
delivery_token: '<ENVIRONMENT_SPECIFIC_DELIVERY_TOKEN>', | ||
environment: '<ENVIRONMENT>'}) | ||
stack.ContentType('<CONTENT_TYPE_UID>') | ||
.Entry('<ENTRY_UID>') | ||
.toJSON() | ||
.fetch() | ||
.then(entry => { | ||
Contentstack.Utils.jsonToHtml({ | ||
entry, | ||
path: ["rte_fieldUid", "group.rteFieldUID"], | ||
renderOption | ||
}) | ||
}) | ||
``` | ||
> Node: Supercharged RTE also supports Embedded items to get all embedded items while fetching entry use `includeEmbeddedItems` function. | ||
### Fetch Embedded Item(s) from Multiple Entries | ||
#### Render HTML RTE Embedded object | ||
To get embedded items from multiple entries, you need to provide the content type UID. You can also use the path variable in case the entries have multiple RTE fields. | ||
@@ -113,5 +151,36 @@ ```js | ||
result.forEach(entry => { | ||
Contentstack.Utils.render({ entry, path: ['rte', 'group.rtefieldUID'], renderOption }) | ||
Contentstack.Utils.render({ | ||
entry, | ||
path: ['rte', 'group.rteFieldUID'], | ||
renderOption | ||
}) | ||
}) | ||
}) | ||
``` | ||
``` | ||
#### Render Supercharged RTE contents | ||
To get a multiple entries, you need to provide the stack API key, environment name, delivery token, content type and entry UID. Then, use `Contentstack.Utils.jsonToHtml` function as shown below: | ||
```js | ||
import * as Contentstack from 'contentstack' | ||
const stack = Contentstack.Stack({ | ||
api_key: '<API_KEY>', | ||
delivery_token: '<ENVIRONMENT_SPECIFIC_DELIVERY_TOKEN>', | ||
environment: '<ENVIRONMENT>'}) | ||
stack.ContentType('<CONTENT_TYPE_UID>') | ||
.Query() | ||
.toJSON() | ||
.where('title', '<entry_title_to_search>') | ||
.find() | ||
.then(result => { | ||
result.forEach(entry => { | ||
Contentstack.Utils.jsonToHtml({ | ||
entry, | ||
path: ["rte_fieldUid", "group.rteFieldUID"], | ||
renderOption | ||
}) | ||
}) | ||
}) | ||
``` | ||
> Node: Supercharged RTE also supports Embedded items to get all embedded items while fetching entry use `includeEmbeddedItems` function. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
125098
63
1384
183