jsx-email
Version:
Render JSX email components to HTML email
51 lines • 2.22 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getRawPlugin = void 0;
exports.escapeForRawComponent = escapeForRawComponent;
exports.unescapeForRawComponent = unescapeForRawComponent;
const START_TAG = '__COMMENT_START';
const END_TAG = '__COMMENT_END';
function escapeForRawComponent(input) {
// escape comment sequences
return input.replace(/<!--/g, START_TAG).replace(/-->/g, END_TAG);
}
function unescapeForRawComponent(input) {
return input
.replace(new RegExp(START_TAG, 'g'), '<!--')
.replace(new RegExp(END_TAG, 'g'), '/-->');
}
/**
* Returns a rehype plugin that replaces `<jsx-email-raw><!--...--></jsx-email-raw>`
* elements with a raw HTML node using the original, unescaped content.
*
* Mirrors the async factory pattern used by `getMovePlugin()`.
*/
const getRawPlugin = async () => {
const { visit } = await import('unist-util-visit');
return function rawPlugin() {
return function transform(tree) {
const matches = [];
visit(tree, 'element', (node, index, parent) => {
if (!parent || typeof index !== 'number')
return;
if (node.tagName !== 'jsx-email-raw')
return;
matches.push({ index, node: node, parent });
});
for (const { node, parent, index } of matches) {
// The Raw component renders a single HTML comment child containing the
// escaped raw content. Extract it and unescape back to the original.
const commentChild = node.children.find((c) => c.type === 'comment');
if (commentChild) {
const rawHtml = unescapeForRawComponent(commentChild.value);
// Replace the wrapper element with a `raw` node to inject HTML verbatim.
// rehype-stringify will pass this through when `allowDangerousHtml: true`.
const rawNode = { type: 'raw', value: rawHtml };
parent.children.splice(index, 1, rawNode);
}
}
};
};
};
exports.getRawPlugin = getRawPlugin;
//# sourceMappingURL=raw.js.map