@faire/mjml-react
Version:
React component library to generate the HTML emails on the fly
75 lines (74 loc) • 2.98 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.renderToJSON2 = renderToJSON2;
const react_1 = __importDefault(require("react"));
const server_1 = __importDefault(require("react-dom/server"));
const react_reconciler_1 = __importDefault(require("react-reconciler"));
const render_utils_1 = require("./render-utils");
const reconciler = (0, react_reconciler_1.default)({
supportsMutation: true,
isPrimaryRenderer: false,
createTextInstance(text) {
return (0, render_utils_1.escapeTextForBrowser)(text);
},
createInstance(type, props) {
if (!type.startsWith("mj")) {
return { isReact: true, type, props };
}
const { children, dangerouslySetInnerHTML, ...rest } = props;
const res = {
tagName: type,
attributes: rest,
};
Object.keys(res.attributes).forEach((key) => {
const attrKey = res.attributes[key];
if (attrKey === undefined) {
delete res.attributes[key];
}
if (typeof attrKey === "string") {
res.attributes[key] = (0, render_utils_1.escapeTextForBrowser)(attrKey);
}
});
if (props.dangerouslySetInnerHTML && props.dangerouslySetInnerHTML.__html) {
// using replace to prevent issue with $ sign in MJML
// https://github.com/mjmlio/mjml2json#L145
res.content = props.dangerouslySetInnerHTML.__html.replace("$", "$");
}
return res;
},
appendChildToContainer(container, child) {
(0, render_utils_1.trimContent)(child);
container.result = child;
},
appendInitialChild(parent, child) {
if (typeof parent === "string" || parent.isReact) {
return;
}
if (typeof child === "string") {
parent.content = (parent.content || "") + child;
}
else if (child.isReact) {
const content = server_1.default.renderToStaticMarkup(react_1.default.createElement(child.type, child.props));
parent.content = (parent.content || "") + content;
}
else {
parent.children = (parent.children || []).concat(child);
}
},
prepareForCommit: render_utils_1.noop,
resetAfterCommit: render_utils_1.noop,
clearContainer: render_utils_1.noop,
appendChild: render_utils_1.noop,
finalizeInitialChildren: render_utils_1.noop,
getChildHostContext: render_utils_1.noop,
getRootHostContext: render_utils_1.noop,
shouldSetTextContent: render_utils_1.noop,
});
function renderToJSON2(whatToRender) {
const container = reconciler.createContainer({}, false, false);
reconciler.updateContainer(whatToRender, container, null, null);
return container.containerInfo.result;
}