replace.astro 895 Bytes
---
import html from '@resources/provider-portal.html?raw'
import { walkSync } from 'ultrahtml'
import { parseHtml, createMatcher, findNode } from './html.ts'
import Match from './match.astro'
import type { SlotHandler, Replacements } from './astro.ts'

interface Props {
  html?: string,
  debug?: boolean,
  xpath?: string,
  replacements?: Replacements,
}

const { props } = Astro
const { html, debug = false, xpath, replacements = {} } = props
const doc = props.node ? props.node : parseHtml(props.html)

const node = xpath ? findNode(doc, xpath) : doc
const replacers = Object.entries(replacements).map(([ selector, handler ]) => [ createMatcher(selector), handler ])

const slotHandler: SlotHandler = (slotName, node) => {
  return Astro.slots.render(slotName, [ node, { slotHandler, replacers } ] )
}
---
<Match node={node} debug={debug} replacers={replacers} slotHandler={slotHandler}/>