|
| 1 | +import type { Content, Element, Literal, Parents, Root } from 'hast'; |
| 2 | + |
| 3 | +// dynamic import of 'unist-util-visit' within factory to support CJS build |
| 4 | + |
| 5 | +interface Match { |
| 6 | + index: number; |
| 7 | + node: Element; |
| 8 | + parent: Parents; |
| 9 | +} |
| 10 | + |
| 11 | +// `raw` is an unofficial HAST node used by rehype to pass through HTML verbatim. |
| 12 | +// Model it locally to avoid `any` casts while keeping the rest of the tree typed. |
| 13 | +interface Raw extends Literal { |
| 14 | + type: 'raw'; |
| 15 | + value: string; |
| 16 | +} |
| 17 | + |
| 18 | +interface ParentWithRaw { |
| 19 | + children: (Content | Raw)[]; |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * Returns a rehype plugin that replaces `<jsx-email-cond>` elements (from |
| 24 | + * the Conditional component) with conditional comment wrappers, based on the |
| 25 | + * `data-mso` and `data-expression` attributes. |
| 26 | + * |
| 27 | + * Mirrors the async factory pattern used by `getRawPlugin()`. |
| 28 | + */ |
| 29 | +export const getConditionalPlugin = async () => { |
| 30 | + const { visit } = await import('unist-util-visit'); |
| 31 | + |
| 32 | + return function conditionalPlugin() { |
| 33 | + return function transform(tree: Root) { |
| 34 | + const matches: Match[] = []; |
| 35 | + let headEl: Element | undefined; |
| 36 | + |
| 37 | + visit(tree, 'element', (node, index, parent) => { |
| 38 | + if (node.tagName === 'head') headEl = node; |
| 39 | + |
| 40 | + if (!parent || typeof index !== 'number') return; |
| 41 | + if (node.tagName !== 'jsx-email-cond') return; |
| 42 | + |
| 43 | + matches.push({ index, node, parent }); |
| 44 | + }); |
| 45 | + |
| 46 | + for (const { node, parent, index } of matches) { |
| 47 | + const props = (node.properties || {}) as Record<string, unknown>; |
| 48 | + const msoProp = (props['data-mso'] ?? (props as any).dataMso) as unknown; |
| 49 | + const msoAttr = |
| 50 | + typeof msoProp === 'undefined' ? void 0 : msoProp === 'false' ? false : Boolean(msoProp); |
| 51 | + const exprRaw = (props['data-expression'] ?? (props as any).dataExpression) as unknown; |
| 52 | + const exprAttr = typeof exprRaw === 'string' ? exprRaw : void 0; |
| 53 | + const headProp = (props['data-head'] ?? (props as any).dataHead) as unknown; |
| 54 | + const toHead = |
| 55 | + typeof headProp === 'undefined' |
| 56 | + ? false |
| 57 | + : headProp === 'false' |
| 58 | + ? false |
| 59 | + : Boolean(headProp); |
| 60 | + |
| 61 | + let openRaw: string | undefined; |
| 62 | + let closeRaw: string | undefined; |
| 63 | + |
| 64 | + if (msoAttr === false) { |
| 65 | + // Not MSO: <!--[if !mso]><!--> ... <!--<![endif]--> |
| 66 | + openRaw = '<!--[if !mso]><!-->'; |
| 67 | + closeRaw = '<!--<![endif]-->'; |
| 68 | + } else { |
| 69 | + // MSO / expression path |
| 70 | + const expression = exprAttr || (msoAttr === true ? 'mso' : void 0); |
| 71 | + if (expression) { |
| 72 | + openRaw = `<!--[if ${expression}]>`; |
| 73 | + // Older Outlook/Word HTML parsers prefer the self-closing |
| 74 | + // conditional terminator variant to avoid comment spillover |
| 75 | + // when adjacent comments appear. Use the `<![endif]/-->` form |
| 76 | + // for maximum compatibility. |
| 77 | + closeRaw = '<![endif]/-->'; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // If no directive attributes present, leave the element in place. |
| 82 | + // eslint-disable-next-line no-continue |
| 83 | + if (!openRaw || !closeRaw) continue; |
| 84 | + |
| 85 | + const before: Raw = { type: 'raw', value: openRaw }; |
| 86 | + const after: Raw = { type: 'raw', value: closeRaw }; |
| 87 | + const children = (node.children || []) as Content[]; |
| 88 | + |
| 89 | + if (toHead && headEl) { |
| 90 | + if (parent === headEl) { |
| 91 | + // Replace in place: open raw, original children, close raw. |
| 92 | + (parent as ParentWithRaw).children.splice(index, 1, before, ...children, after); |
| 93 | + } else { |
| 94 | + // Remove wrapper from current location |
| 95 | + (parent as ParentWithRaw).children.splice(index, 1); |
| 96 | + // Append the conditional to the <head> |
| 97 | + (headEl as unknown as ParentWithRaw).children.push(before, ...children, after); |
| 98 | + } |
| 99 | + } else { |
| 100 | + // Replace in place: open raw, original children, close raw. |
| 101 | + (parent as ParentWithRaw).children.splice(index, 1, before, ...children, after); |
| 102 | + } |
| 103 | + } |
| 104 | + }; |
| 105 | + }; |
| 106 | +}; |
0 commit comments