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.
Showing
2 changed files
with
36 additions
and
10 deletions
| 1 | --- | 1 | --- |
| 2 | import { parseHtml, createMatcher, findNode } from './html.ts' | 2 | import { parseHtml, createMatcher, findNode } from './html.ts' |
| 3 | import { getUrl, getSitePage } from './remote-content.ts' | ||
| 3 | import Match from './match.astro' | 4 | import Match from './match.astro' |
| 4 | 5 | ||
| 5 | const { props } = Astro | 6 | export const getHtmlFromProps = (props) => { |
| 6 | const { debug, replacers, slotHandler } = props | 7 | const { site, page, url, html } = props |
| 8 | if (html) return { data: html } | ||
| 9 | if (url) return getUrl(url) | ||
| 10 | if (site && page) return getSitePage(site, page) | ||
| 11 | return { data: null } | ||
| 12 | } | ||
| 13 | |||
| 14 | export const getDocFromProps = async (props, options) => { | ||
| 15 | if (props.node) return props.node; | ||
| 16 | const { data: html } = await getHtmlFromProps(props) | ||
| 17 | const adjuster = createAdjuster(props.adjustments) | ||
| 18 | return parseHtml(html, { ...options, adjuster }) | ||
| 19 | } | ||
| 7 | 20 | ||
| 8 | const adjustmentsCompiled = Object.entries(adjustments).map(([ selector, handler ]) => [ createMatcher(selector), handler ]) | 21 | export const getNodeFromProps = async (props, options) => { |
| 9 | const adjuster = (node, parent, index) => { | 22 | const doc = await getDocFromProps(props, options) |
| 10 | for (const [ matcher, handler ] of adjustmentsCompiled) { | 23 | const { xpath } = props |
| 11 | if (matcher(node, parent, index, false)) { | 24 | return xpath ? findNode(doc, xpath) : doc |
| 12 | node = handler(node) | 25 | } |
| 26 | |||
| 27 | export const createAdjuster = (adjustments = {}) => { | ||
| 28 | const adjustmentsCompiled = Object.entries(adjustments).map(([ selector, handler ]) => [ createMatcher(selector), handler ]) | ||
| 29 | const adjuster = (node, parent, index) => { | ||
| 30 | for (const [ matcher, handler ] of adjustmentsCompiled) { | ||
| 31 | if (matcher(node, parent, index, false)) { | ||
| 32 | node = handler(node) | ||
| 33 | } | ||
| 13 | } | 34 | } |
| 35 | return node | ||
| 14 | } | 36 | } |
| 15 | return node | 37 | return adjuster |
| 16 | } | 38 | } |
| 17 | 39 | ||
| 18 | const doc = props.node ? props.node : parseHtml(props.html, { adjuster }) | 40 | const { props } = Astro |
| 19 | const node = xpath ? findNode(doc, xpath) : doc | 41 | const { debug, replacers, slotHandler } = props |
| 42 | const node = await getNodeFromProps(props) | ||
| 20 | 43 | ||
| 21 | const nextDebug = debug ? debug - 1 : 0 | 44 | const nextDebug = debug ? debug - 1 : 0 |
| 22 | --- | 45 | --- | ... | ... |
| ... | @@ -4,6 +4,9 @@ import External from './external.astro' | ... | @@ -4,6 +4,9 @@ import External from './external.astro' |
| 4 | import type { SlotHandler, Replacements } from './astro.ts' | 4 | import type { SlotHandler, Replacements } from './astro.ts' |
| 5 | 5 | ||
| 6 | interface Props { | 6 | interface Props { |
| 7 | site?: string, | ||
| 8 | page?: string, | ||
| 9 | url?: string, | ||
| 7 | html?: string, | 10 | html?: string, |
| 8 | debug?: number, | 11 | debug?: number, |
| 9 | xpath?: string, | 12 | xpath?: string, | ... | ... |
-
Please register or sign in to post a comment