external.astro
1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
---
import { parseHtml, 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) 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 ]
}
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 adjuster
}
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}/>)}