{"version":3,"file":"rehypeStreamAnimated.mjs","names":[],"sources":["../../../src/Markdown/plugins/rehypeStreamAnimated.ts"],"sourcesContent":["import { type Element, type ElementContent, type Root } from 'hast';\nimport { type BuildVisitor } from 'unist-util-visit';\nimport { visit } from 'unist-util-visit';\n\nexport interface StreamAnimatedOptions {\n  births?: number[];\n  fadeDuration?: number;\n  nowMs?: number;\n  revealed?: boolean;\n}\n\nconst BLOCK_TAGS = new Set(['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'li']);\nconst SKIP_TAGS = new Set(['pre', 'code', 'table', 'svg']);\n\nfunction hasClass(node: Element, cls: string): boolean {\n  const cn = node.properties?.className;\n  if (Array.isArray(cn)) return cn.some((c) => String(c).includes(cls));\n  if (typeof cn === 'string') return cn.includes(cls);\n  return false;\n}\n\nexport const rehypeStreamAnimated = (options: StreamAnimatedOptions = {}) => {\n  const { births, fadeDuration = 150, nowMs, revealed = false } = options;\n  const hasBirths = !revealed && Array.isArray(births) && typeof nowMs === 'number';\n\n  return (tree: Root) => {\n    let globalCharIndex = 0;\n\n    const shouldSkip = (node: Element): boolean => {\n      return SKIP_TAGS.has(node.tagName) || hasClass(node, 'katex');\n    };\n\n    const wrapText = (node: Element) => {\n      const newChildren: ElementContent[] = [];\n      for (const child of node.children) {\n        if (child.type === 'text') {\n          for (const char of child.value) {\n            let className = 'stream-char';\n            let delay: number | undefined;\n\n            if (revealed) {\n              className = 'stream-char stream-char-revealed';\n            } else if (hasBirths) {\n              const birthTs = births![globalCharIndex];\n              if (birthTs === undefined) {\n                className = 'stream-char stream-char-revealed';\n              } else {\n                const elapsed = (nowMs as number) - birthTs;\n                if (elapsed >= fadeDuration) {\n                  className = 'stream-char stream-char-revealed';\n                } else {\n                  // Negative delay = already elapsed ms into the fade.\n                  // Positive delay = not started yet (char born in the future,\n                  // i.e. staggered within the same commit).\n                  delay = -elapsed;\n                }\n              }\n            }\n\n            const properties: Record<string, any> = { className };\n            if (delay !== undefined && delay !== 0) {\n              properties.style = `animation-delay:${delay}ms`;\n            }\n            newChildren.push({\n              children: [{ type: 'text', value: char }],\n              properties,\n              tagName: 'span',\n              type: 'element',\n            });\n            globalCharIndex++;\n          }\n        } else if (child.type === 'element') {\n          if (!shouldSkip(child)) {\n            wrapText(child);\n          }\n          newChildren.push(child);\n        } else {\n          newChildren.push(child);\n        }\n      }\n      node.children = newChildren;\n    };\n\n    visit(tree, 'element', ((node: Element) => {\n      if (shouldSkip(node)) return 'skip';\n      if (BLOCK_TAGS.has(node.tagName)) {\n        wrapText(node);\n        return 'skip';\n      }\n    }) as BuildVisitor<Root, 'element'>);\n  };\n};\n"],"mappings":";;AAWA,MAAM,aAAa,IAAI,IAAI;CAAC;CAAK;CAAM;CAAM;CAAM;CAAM;CAAM;CAAM;CAAK,CAAC;AAC3E,MAAM,YAAY,IAAI,IAAI;CAAC;CAAO;CAAQ;CAAS;CAAM,CAAC;AAE1D,SAAS,SAAS,MAAe,KAAsB;CACrD,MAAM,KAAK,KAAK,YAAY;AAC5B,KAAI,MAAM,QAAQ,GAAG,CAAE,QAAO,GAAG,MAAM,MAAM,OAAO,EAAE,CAAC,SAAS,IAAI,CAAC;AACrE,KAAI,OAAO,OAAO,SAAU,QAAO,GAAG,SAAS,IAAI;AACnD,QAAO;;AAGT,MAAa,wBAAwB,UAAiC,EAAE,KAAK;CAC3E,MAAM,EAAE,QAAQ,eAAe,KAAK,OAAO,WAAW,UAAU;CAChE,MAAM,YAAY,CAAC,YAAY,MAAM,QAAQ,OAAO,IAAI,OAAO,UAAU;AAEzE,SAAQ,SAAe;EACrB,IAAI,kBAAkB;EAEtB,MAAM,cAAc,SAA2B;AAC7C,UAAO,UAAU,IAAI,KAAK,QAAQ,IAAI,SAAS,MAAM,QAAQ;;EAG/D,MAAM,YAAY,SAAkB;GAClC,MAAM,cAAgC,EAAE;AACxC,QAAK,MAAM,SAAS,KAAK,SACvB,KAAI,MAAM,SAAS,OACjB,MAAK,MAAM,QAAQ,MAAM,OAAO;I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