replace.astro 939 Bytes
---
import html from '@resources/provider-portal.html?raw'
import { walkSync } from 'ultrahtml'
import { parseHtml, createMatcher, findNode } from './html.js'
import Render from './render.astro'

const { props } = Astro
const { html, mapClassname = true, 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 fixAttributes = ({ attributes }) => {
  if (!mapClassname) return attributes
  const { class: className, ...rest } = attributes
  return { ...rest, className }
}

const slotHandler = (slotName, node) => {
  const attributes = fixAttributes(node)
  return Astro.slots.render(slotName, [ { ...node, attributes }, { slotHandler, replacers } ] )
}
---
<Render node={node} replacers={replacers} slotHandler={slotHandler}/>