@contentstack/utils
Version:
Contentstack utilities for Javascript
277 lines • 13.8 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);
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
var _a;
import MarkType from "../nodes/mark-type";
import NodeType from "../nodes/node-type";
import { sanitizeHTML } from "../helper/sanitize";
/**
* Safely gets an attribute value from node.attrs
*/
function getAttr(node, key) {
var _a;
return (_a = node.attrs) === null || _a === void 0 ? void 0 : _a[key];
}
/**
* Safely gets a string attribute value from node.attrs
*/
function getAttrString(node, key) {
var _a;
var value = (_a = node.attrs) === null || _a === void 0 ? void 0 : _a[key];
return typeof value === 'string' ? value : undefined;
}
/**
* Builds common HTML attributes string (style, class-name, id)
*/
function buildCommonAttrs(node) {
if (!node.attrs)
return '';
var attrs = [];
if (node.attrs.style) {
attrs.push(" style=\"".concat(node.attrs.style, "\""));
}
if (node.attrs['class-name']) {
attrs.push(" class=\"".concat(node.attrs['class-name'], "\""));
}
if (node.attrs.id) {
attrs.push(" id=\"".concat(node.attrs.id, "\""));
}
return attrs.join('');
}
/**
* JSON RTE exports nested lists as siblings of the preceding <li> (not children).
* This folds any ol/ul that immediately follows an li into that li's children
* so the rendered HTML is valid (nested list inside the li).
*/
function foldNestedListSiblingsIntoPreviousLi(children) {
var result = [];
for (var i = 0; i < children.length; i++) {
var node = children[i];
var isList = node && (node.type === NodeType.ORDER_LIST || node.type === NodeType.UNORDER_LIST);
var last = result[result.length - 1];
var lastIsLi = last && last.type === NodeType.LIST_ITEM;
if (isList && lastIsLi && last) {
result[result.length - 1] = __assign(__assign({}, last), { children: __spreadArray(__spreadArray([], (last.children || []), true), [node], false) });
}
else {
result.push(children[i]);
}
}
return result;
}
export var defaultNodeOption = (_a = {},
_a[NodeType.DOCUMENT] = function () {
return "";
},
_a[NodeType.PARAGRAPH] = function (node, next) {
return "<p".concat(buildCommonAttrs(node), ">").concat(sanitizeHTML(next(node.children)), "</p>");
},
_a[NodeType.LINK] = function (node, next) {
var href = getAttrString(node, 'href') || getAttrString(node, 'url') || '';
var sanitizedHref = sanitizeHTML(href);
var target = getAttrString(node, 'target');
var targetAttr = target ? " target=\"".concat(target, "\"") : '';
return "<a".concat(buildCommonAttrs(node)).concat(sanitizedHref ? " href=\"".concat(sanitizedHref, "\"") : '').concat(targetAttr, ">").concat(sanitizeHTML(next(node.children)), "</a>");
},
_a[NodeType.IMAGE] = function (node, next) {
var src = getAttrString(node, 'src') || getAttrString(node, 'url');
var sanitizedSrc = src ? encodeURI(sanitizeHTML(src)) : '';
return "<img".concat(buildCommonAttrs(node)).concat(sanitizedSrc ? " src=\"".concat(sanitizedSrc, "\"") : '', " />").concat(sanitizeHTML(next(node.children)));
},
_a[NodeType.EMBED] = function (node, next) {
var src = getAttrString(node, 'src') || getAttrString(node, 'url');
var sanitizedSrc = src ? encodeURI(sanitizeHTML(src)) : '';
return "<iframe".concat(buildCommonAttrs(node)).concat(sanitizedSrc ? " src=\"".concat(sanitizedSrc, "\"") : '', ">").concat(sanitizeHTML(next(node.children)), "</iframe>");
},
_a[NodeType.HEADING_1] = function (node, next) {
return "<h1".concat(buildCommonAttrs(node), ">").concat(sanitizeHTML(next(node.children)), "</h1>");
},
_a[NodeType.HEADING_2] = function (node, next) {
return "<h2".concat(buildCommonAttrs(node), ">").concat(sanitizeHTML(next(node.children)), "</h2>");
},
_a[NodeType.HEADING_3] = function (node, next) {
return "<h3".concat(buildCommonAttrs(node), ">").concat(sanitizeHTML(next(node.children)), "</h3>");
},
_a[NodeType.HEADING_4] = function (node, next) {
return "<h4".concat(buildCommonAttrs(node), ">").concat(sanitizeHTML(next(node.children)), "</h4>");
},
_a[NodeType.HEADING_5] = function (node, next) {
return "<h5".concat(buildCommonAttrs(node), ">").concat(sanitizeHTML(next(node.children)), "</h5>");
},
_a[NodeType.HEADING_6] = function (node, next) {
return "<h6".concat(buildCommonAttrs(node), ">").concat(sanitizeHTML(next(node.children)), "</h6>");
},
_a[NodeType.ORDER_LIST] = function (node, next) {
var children = foldNestedListSiblingsIntoPreviousLi(node.children);
return "<ol".concat(buildCommonAttrs(node), ">").concat(sanitizeHTML(next(children)), "</ol>");
},
_a[NodeType.FRAGMENT] = function (node, next) {
return "<fragment>".concat(sanitizeHTML(next(node.children)), "</fragment>");
},
_a[NodeType.UNORDER_LIST] = function (node, next) {
var children = foldNestedListSiblingsIntoPreviousLi(node.children);
return "<ul".concat(buildCommonAttrs(node), ">").concat(sanitizeHTML(next(children)), "</ul>");
},
_a[NodeType.LIST_ITEM] = function (node, next) {
return "<li".concat(buildCommonAttrs(node), ">").concat(sanitizeHTML(next(node.children)), "</li>");
},
_a[NodeType.HR] = function () {
return "<hr>";
},
_a[NodeType.TABLE] = function (node, next) {
// Generate colgroup if colWidths attribute is present
var colgroupHTML = '';
var colWidths = getAttr(node, 'colWidths');
if (colWidths && Array.isArray(colWidths)) {
var totalWidth_1 = colWidths.reduce(function (sum, width) { return sum + width; }, 0);
colgroupHTML = "<".concat(NodeType.COL_GROUP, " data-width=\"").concat(totalWidth_1, "\">");
colWidths.forEach(function (colWidth) {
var widthPercentage = (colWidth / totalWidth_1) * 100;
colgroupHTML += "<".concat(NodeType.COL, " style=\"width:").concat(widthPercentage.toFixed(2), "%\"/>");
});
colgroupHTML += "</".concat(NodeType.COL_GROUP, ">");
}
// Generate table with colgroup and other attributes
return "<table".concat(buildCommonAttrs(node), ">").concat(colgroupHTML).concat(sanitizeHTML(next(node.children)), "</table>");
},
_a[NodeType.TABLE_HEADER] = function (node, next) {
return "<thead".concat(buildCommonAttrs(node), ">").concat(sanitizeHTML(next(node.children)), "</thead>");
},
_a[NodeType.TABLE_BODY] = function (node, next) {
return "<tbody".concat(buildCommonAttrs(node), ">").concat(sanitizeHTML(next(node.children)), "</tbody>");
},
_a[NodeType.TABLE_FOOTER] = function (node, next) {
return "<tfoot".concat(buildCommonAttrs(node), ">").concat(sanitizeHTML(next(node.children)), "</tfoot>");
},
_a[NodeType.TABLE_ROW] = function (node, next) {
return "<tr".concat(buildCommonAttrs(node), ">").concat(sanitizeHTML(next(node.children)), "</tr>");
},
_a[NodeType.TABLE_HEAD] = function (node, next) {
if (getAttr(node, 'void'))
return '';
var rowSpan = getAttr(node, 'rowSpan');
var colSpan = getAttr(node, 'colSpan');
var rowSpanAttr = rowSpan ? " rowspan=\"".concat(rowSpan, "\"") : '';
var colSpanAttr = colSpan ? " colspan=\"".concat(colSpan, "\"") : '';
return "<th".concat(rowSpanAttr).concat(colSpanAttr).concat(buildCommonAttrs(node), ">").concat(sanitizeHTML(next(node.children)), "</th>");
},
_a[NodeType.TABLE_DATA] = function (node, next) {
if (getAttr(node, 'void'))
return '';
var rowSpan = getAttr(node, 'rowSpan');
var colSpan = getAttr(node, 'colSpan');
var rowSpanAttr = rowSpan ? " rowspan=\"".concat(rowSpan, "\"") : '';
var colSpanAttr = colSpan ? " colspan=\"".concat(colSpan, "\"") : '';
return "<td".concat(rowSpanAttr).concat(colSpanAttr).concat(buildCommonAttrs(node), ">").concat(sanitizeHTML(next(node.children)), "</td>");
},
_a[NodeType.BLOCK_QUOTE] = function (node, next) {
return "<blockquote".concat(buildCommonAttrs(node), ">").concat(sanitizeHTML(next(node.children)), "</blockquote>");
},
_a[NodeType.CODE] = function (node, next) {
return "<code".concat(buildCommonAttrs(node), ">").concat(sanitizeHTML(next(node.children)), "</code>");
},
_a['reference'] = function (node, next) {
var type = getAttr(node, 'type');
var displayType = getAttr(node, 'display-type');
if ((type === 'entry' || type === 'asset') && displayType === 'link') {
var href = getAttrString(node, 'href') || getAttrString(node, 'url') || '';
var target = getAttrString(node, 'target');
var assetUid = getAttrString(node, 'asset-uid');
var aTagAttrs = buildCommonAttrs(node);
if (href)
aTagAttrs += " href=\"".concat(href, "\"");
if (target) {
aTagAttrs += " target=\"".concat(target, "\"");
}
if (type === 'asset') {
aTagAttrs += " type=\"asset\" content-type-uid=\"sys_assets\"";
if (assetUid) {
aTagAttrs += " data-sys-asset-uid=\"".concat(assetUid, "\"");
}
aTagAttrs += " sys-style-type=\"download\"";
}
return "<a".concat(aTagAttrs, ">").concat(sanitizeHTML(next(node.children)), "</a>");
}
if (type === 'asset') {
var assetLink = getAttrString(node, 'asset-link');
var src = assetLink ? encodeURI(assetLink) : '';
var redactorAttrs = getAttr(node, 'redactor-attributes');
var alt = redactorAttrs === null || redactorAttrs === void 0 ? void 0 : redactorAttrs['alt'];
var link = getAttrString(node, 'link');
var target = getAttrString(node, 'target') || "";
var caption = (redactorAttrs === null || redactorAttrs === void 0 ? void 0 : redactorAttrs['asset-caption']) || getAttrString(node, 'asset-caption') || "";
var style = getAttrString(node, 'style');
var assetUid = getAttrString(node, 'asset-uid');
var className = getAttrString(node, 'class-name');
var assetUidAttr = assetUid ? " asset_uid=\"".concat(assetUid, "\"") : '';
var classAttr = className ? " class=\"".concat(sanitizeHTML(className), "\"") : '';
var srcAttr = src ? " src=\"".concat(sanitizeHTML(src), "\"") : '';
var altAttr = alt ? " alt=\"".concat(alt, "\"") : '';
var targetAttr = target === "_blank" ? " target=\"_blank\"" : '';
var styleAttr = style ? " style=\"".concat(style, "\"") : '';
var imageTag = "<img".concat(assetUidAttr).concat(classAttr).concat(srcAttr).concat(altAttr).concat(targetAttr).concat(styleAttr, " />");
var styleAttrFig = style ? " style=\"".concat(style, "\"") : '';
return "<figure".concat(styleAttrFig, ">") +
(link ? "<a href=\"".concat(link, "\" target=\"").concat(target || "", "\">") : "") +
imageTag +
(link ? "</a>" : "") +
(caption ? "<figcaption>".concat(caption, "</figcaption>") : "") +
"</figure>";
}
return "";
},
_a['default'] = function (node, next) {
return sanitizeHTML(next(node.children));
},
_a[MarkType.BOLD] = function (text) {
return "<strong>".concat(sanitizeHTML(text), "</strong>");
},
_a[MarkType.ITALIC] = function (text) {
return "<em>".concat(sanitizeHTML(text), "</em>");
},
_a[MarkType.UNDERLINE] = function (text) {
return "<u>".concat(sanitizeHTML(text), "</u>");
},
_a[MarkType.STRIKE_THROUGH] = function (text) {
return "<strike>".concat(sanitizeHTML(text), "</strike>");
},
_a[MarkType.INLINE_CODE] = function (text) {
return "<span data-type='inlineCode'>".concat(sanitizeHTML(text), "</span>");
},
_a[MarkType.SUBSCRIPT] = function (text) {
return "<sub>".concat(sanitizeHTML(text), "</sub>");
},
_a[MarkType.SUPERSCRIPT] = function (text) {
return "<sup>".concat(sanitizeHTML(text), "</sup>");
},
_a[MarkType.BREAK] = function (text) {
// Check if text is only newlines (which will be converted to <br /> by sanitizeHTML)
// If so, don't add an extra <br /> to avoid duplication
var onlyNewlines = /^\n+$/.test(text);
if (onlyNewlines) {
return sanitizeHTML(text);
}
return "<br />".concat(sanitizeHTML(text));
},
_a[MarkType.CLASSNAME_OR_ID] = function (text, classname, id) {
return "<span".concat(classname ? " class=\"".concat(classname, "\"") : "").concat(id ? " id=\"".concat(id, "\"") : "", ">").concat(sanitizeHTML(text), "</span>");
},
_a);
//# sourceMappingURL=default-node-options.js.map