UNPKG

@lobehub/ui

Version:

Lobe UI is an open-source UI component library for building AIGC web apps

1 lines 2.16 kB
{"version":3,"file":"rehypeStreamAnimated.mjs","names":["newChildren: Array<ElementContent>"],"sources":["../../../src/Markdown/plugins/rehypeStreamAnimated.ts"],"sourcesContent":["// Copyright (c) 2025 Bytedance Ltd. and/or its affiliates\n// SPDX-License-Identifier: MIT\nimport type { Element, ElementContent, Root } from 'hast';\nimport { visit } from 'unist-util-visit';\nimport type { BuildVisitor } from 'unist-util-visit';\n\nexport const rehypeStreamAnimated = () => {\n return (tree: Root) => {\n visit(tree, 'element', ((node: Element) => {\n if (\n ['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'li', 'strong'].includes(node.tagName) &&\n node.children\n ) {\n const newChildren: Array<ElementContent> = [];\n for (const child of node.children) {\n if (child.type === 'text') {\n const segmenter = new Intl.Segmenter('zh', { granularity: 'word' });\n const segments = segmenter.segment(child.value);\n const words = [...segments].map((segment) => segment.segment).filter(Boolean);\n words.forEach((word: string) => {\n newChildren.push({\n children: [{ type: 'text', value: word }],\n properties: {\n className: 'animate-fade-in',\n },\n tagName: 'span',\n type: 'element',\n });\n });\n } else {\n newChildren.push(child);\n }\n }\n node.children = newChildren;\n }\n }) as BuildVisitor<Root, 'element'>);\n };\n};\n"],"mappings":";;;AAMA,MAAa,6BAA6B;AACxC,SAAQ,SAAe;AACrB,QAAM,MAAM,aAAa,SAAkB;AACzC,OACE;IAAC;IAAK;IAAM;IAAM;IAAM;IAAM;IAAM;IAAM;IAAM;IAAS,CAAC,SAAS,KAAK,QAAQ,IAChF,KAAK,UACL;IACA,MAAMA,cAAqC,EAAE;AAC7C,SAAK,MAAM,SAAS,KAAK,SACvB,KAAI,MAAM,SAAS,OAIjB,CADc,CAAC,GAFG,IAAI,KAAK,UAAU,MAAM,EAAE,aAAa,QAAQ,CAAC,CACxC,QAAQ,MAAM,MAAM,CACpB,CAAC,KAAK,YAAY,QAAQ,QAAQ,CAAC,OAAO,QAAQ,CACvE,SAAS,SAAiB;AAC9B,iBAAY,KAAK;MACf,UAAU,CAAC;OAAE,MAAM;OAAQ,OAAO;OAAM,CAAC;MACzC,YAAY,EACV,WAAW,mBACZ;MACD,SAAS;MACT,MAAM;MACP,CAAC;MACF;QAEF,aAAY,KAAK,MAAM;AAG3B,SAAK,WAAW;;KAEgB"}