external.astro 1.13 KB
---
import { parseHtml, createAdjuster, createMatcher, findNode } from './html.ts'
import { getUrl, getSitePage } from './remote-content.ts'
import Match from './match.astro'

export const getHtmlFromProps = (props) => {
  const { site, page, url, html } = props
  if (html) return { data: html }
  if (url) return getUrl(url)
  if (site && page !== undefined) return getSitePage(site, page)
  return { data: null }
}

export const getDocFromProps = async (props, options) => {
  if (props.node) return props.node;
  const { data: html } = await getHtmlFromProps(props)
  const adjuster = createAdjuster(props.adjustments)
  return parseHtml(html, { ...options, adjuster })
}

export const getNodesFromProps = async (props, options) => {
  const doc = await getDocFromProps(props, options)
  const { xpath } = props
  return xpath ? findNode(doc, xpath, { single: false }) : [ doc ]
}

const { props } = Astro
const { debug, replacers, slotHandler } = props
const nodes = await getNodesFromProps(props)

const nextDebug = debug ? debug - 1 : 0
---
{nodes.map(node => <Match node={node} debug={nextDebug} replacers={replacers} slotHandler={slotHandler}/>)}