3312ebad by Adam Heath

Add url, site, page options(in addition to still having html and node),

for parsing of external content.  This defers back into remote-content,
as needed.
1 parent 59c77f5f
---
import { parseHtml, createMatcher, findNode } from './html.ts'
import { getUrl, getSitePage } from './remote-content.ts'
import Match from './match.astro'
const { props } = Astro
const { debug, replacers, slotHandler } = props
export const getHtmlFromProps = (props) => {
const { site, page, url, html } = props
if (html) return { data: html }
if (url) return getUrl(url)
if (site && page) 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 })
}
const adjustmentsCompiled = Object.entries(adjustments).map(([ selector, handler ]) => [ createMatcher(selector), handler ])
const adjuster = (node, parent, index) => {
for (const [ matcher, handler ] of adjustmentsCompiled) {
if (matcher(node, parent, index, false)) {
node = handler(node)
export const getNodeFromProps = async (props, options) => {
const doc = await getDocFromProps(props, options)
const { xpath } = props
return xpath ? findNode(doc, xpath) : doc
}
export const createAdjuster = (adjustments = {}) => {
const adjustmentsCompiled = Object.entries(adjustments).map(([ selector, handler ]) => [ createMatcher(selector), handler ])
const adjuster = (node, parent, index) => {
for (const [ matcher, handler ] of adjustmentsCompiled) {
if (matcher(node, parent, index, false)) {
node = handler(node)
}
}
return node
}
return node
return adjuster
}
const doc = props.node ? props.node : parseHtml(props.html, { adjuster })
const node = xpath ? findNode(doc, xpath) : doc
const { props } = Astro
const { debug, replacers, slotHandler } = props
const node = await getNodeFromProps(props)
const nextDebug = debug ? debug - 1 : 0
---
......
......@@ -4,6 +4,9 @@ import External from './external.astro'
import type { SlotHandler, Replacements } from './astro.ts'
interface Props {
site?: string,
page?: string,
url?: string,
html?: string,
debug?: number,
xpath?: string,
......