@kubb/react
Version:
React integration for Kubb, providing JSX runtime support and React component generation capabilities for code generation plugins.
41 lines (31 loc) • 1.16 kB
text/typescript
import { nodeNames } from '../dom.ts'
import type { KubbFile } from '@kubb/core/fs'
import type React from 'react'
import type { File } from '../components/File.tsx'
import type { DOMElement, ElementNames } from '../types.ts'
import { squashTextNodes } from './squashTextNodes.ts'
export function squashSourceNodes(node: DOMElement, ignores: Array<ElementNames>): Set<KubbFile.Source> {
let sources = new Set<KubbFile.Source>()
for (const childNode of node.childNodes) {
if (!childNode) {
continue
}
if (childNode.nodeName !== '#text' && ignores.includes(childNode.nodeName)) {
continue
}
if (childNode.nodeName === 'kubb-source') {
const attributes = childNode.attributes as React.ComponentProps<typeof File.Source>
const value = squashTextNodes(childNode)
sources.add({
...attributes,
// remove end enter
value: value.trim().replace(/^\s+|\s+$/g, ''),
})
continue
}
if (childNode.nodeName !== '#text' && nodeNames.includes(childNode.nodeName)) {
sources = new Set([...sources, ...squashSourceNodes(childNode, ignores)])
}
}
return sources
}