match.astro 837 Bytes
---
import { ELEMENT_NODE, TEXT_NODE } from 'ultrahtml'
import Node from './node.astro'

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

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

const nextDebug = debug ? debug - 1 : 0
---
{
  slotName ? slotHandler(slotName, fixedNode)
  : <Node parent={parent} node={fixedNode} index={index} debug={nextDebug} replacers={replacers} slotHandler={slotHandler} adjuster={adjuster}/>
}