render.astro 1.21 KB
---
import { ELEMENT_NODE, TEXT_NODE } from 'ultrahtml'
import Children from './children.astro'

export const slotPassThrough = (Astro) => (slotName, node) => {
console.log('calling slot', { slotName, node, Astro })
  return Astro.slots.render('default', [ slotName, node ])
}

const { props: { debug = false, parent = null, node, index = 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)) {
    if (debug) console.log('matched')
    slotName = handler
  }
}

//const slotCallback = slotPassThrough(Astro)
---
{
  slotName ? slotHandler(slotName, node)
  : node.type === TEXT_NODE ? <Fragment set:html={node.value}/>
  : node.type === ELEMENT_NODE ? (
    node.isSelfClosingTag ? <Name {...attributes}/>
    : <Name {...attributes}>
        <Children parent={node} children={node.children} replacers={replacers} slotHandler={slotHandler}/>
      </Name>
  ) : (
    <Children parent={node} children={node.children} replacers={replacers} slotHandler={slotHandler}/>
  )
}