match.astro 1.33 KB
---
import { ELEMENT_NODE, TEXT_NODE } from 'ultrahtml'
import Node from './node.astro'
import Children from './children.astro'

const { props: { parent = null, node, index = 0, special, debug = 0, replacers, slotHandler } } = Astro
const { name: Name, attributes } = node

if (debug) {
  console.log('trying to match against', {node})
}
let slotName
for (const [ matcher, handler ] of replacers) {
  if (debug) console.log('attempting matcher', matcher.toString())
  if (matcher(node, parent, index, false, special)) {
    if (debug) console.log('matched')
    slotName = handler
  }
}

const nextDebug = debug ? debug - 1 : 0
const [ Component, componentArgs, componentContent ] = Array.isArray(slotName) ? slotName : []
---
{
  typeof Component === 'string' ? <Component {...node.attributes} {...componentArgs}>{componentContent || <Children parent={node} children={node.children} debug={nextDebug} replacers={replacers} slotHandler={slotHandler}/>}</Component>
  : Component ? (<Component {...componentArgs} parent={parent} node={node} index={index} debug={nextDebug} replacers={replacers} slotHandler={slotHandler} special={special}>{componentContent}</Component>)
  : slotName ? slotHandler(slotName, node, special)
  : <Node parent={parent} node={node} index={index} debug={nextDebug} replacers={replacers} slotHandler={slotHandler} special={special}/>
}