( node: HastNode | HastTextNode, key: string, links?: Links, indent = '' )
| 272 | // Renders a Hast Node to a list of tokens. A token is either a string, a React element, or a token type (number) + string. |
| 273 | // These are flattened into an array that gets sent to the client. This format significantly reduces the payload size vs JSX. |
| 274 | function renderHast( |
| 275 | node: HastNode | HastTextNode, |
| 276 | key: string, |
| 277 | links?: Links, |
| 278 | indent = '' |
| 279 | ): Token | Token[] { |
| 280 | if (node.type === 'element' && 'children' in node) { |
| 281 | let childArray: Token[] = renderChildren(node.children, key, links); |
| 282 | if (node.tagName === 'div') { |
| 283 | if (typeof childArray.at(-1) === 'string' && typeof childArray.at(-2) !== 'number') { |
| 284 | childArray[childArray.length - 1] += '\n'; |
| 285 | } else { |
| 286 | childArray.push('\n'); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | let tokenType = |
| 291 | node.properties?.className |
| 292 | .split(' ') |
| 293 | .map(c => TokenType[c]) |
| 294 | .filter(v => v != null) || []; |
| 295 | if (node.properties?.className === 'comment' && text(node) === '/* PROPS */') { |
| 296 | return <CodeProps key={key} indent={indent} />; |
| 297 | } |
| 298 | |
| 299 | // CodeProps includes the indent and newlines in case there are no props to show. |
| 300 | if ( |
| 301 | node.tagName === 'div' && |
| 302 | typeof childArray[0] === 'string' && |
| 303 | /^\s+$/.test(childArray[0]) && |
| 304 | React.isValidElement(childArray[1]) && |
| 305 | childArray[1].type === CodeProps |
| 306 | ) { |
| 307 | // If the only thing after CodeProps is the newline from div processing, exclude it (CodeProps handles its own newlines). |
| 308 | // Otherwise, include all trailing content. |
| 309 | childArray = |
| 310 | childArray.length === 3 && childArray[2] === '\n' |
| 311 | ? childArray.slice(1, 2) |
| 312 | : childArray.slice(1); |
| 313 | } |
| 314 | |
| 315 | let children = childArray.length === 1 ? childArray[0] : childArray; |
| 316 | let tagName: any = node.tagName; |
| 317 | let properties: any = node.properties; |
| 318 | if (links && typeof children === 'string' && links[children]) { |
| 319 | let link = links[children]; |
| 320 | return ( |
| 321 | <CodeLink key={key} className={styles[properties?.className]} href={link}> |
| 322 | {children} |
| 323 | </CodeLink> |
| 324 | ); |
| 325 | } |
| 326 | |
| 327 | // Link to imported files. |
| 328 | if ( |
| 329 | properties?.className === 'string' && |
| 330 | typeof children === 'string' && |
| 331 | /^['"]\.\//.test(children) |
no test coverage detected