59c77f5f by Adam Heath

Replace now just contains replacements/slotHandler, and defers html

parsing to External.
1 parent 754d6092
......@@ -3,8 +3,9 @@ import Custom from './custom.astro'
import Match from './match.astro'
import Node from './node.astro'
import Replace from './replace.astro'
import External from './external.astro'
export { Children, Custom, Match, Node, Replace }
export { Children, Custom, Match, Node, Replace, External }
import type { NodeType } from 'ultrahtml'
type SlotHandler = (string, NodeType) => Promise<Any>
......
---
import { parseHtml, createMatcher, findNode } from './html.ts'
import Match from './match.astro'
const { props } = Astro
const { debug, replacers, slotHandler } = props
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
}
const doc = props.node ? props.node : parseHtml(props.html, { adjuster })
const node = xpath ? findNode(doc, xpath) : doc
const nextDebug = debug ? debug - 1 : 0
---
<Match node={node} debug={nextDebug} replacers={replacers} slotHandler={slotHandler} adjuster={adjuster}/>
---
import { walkSync } from 'ultrahtml'
import { parseHtml, createMatcher, findNode } from './html.ts'
import Match from './match.astro'
import { createMatcher } from './html.ts'
import External from './external.astro'
import type { SlotHandler, Replacements } from './astro.ts'
interface Props {
......@@ -13,26 +12,13 @@ interface Props {
}
const { props } = Astro
const { html, debug = 0, xpath, replacements = {}, adjustments = {} } = props
const { debug = 0, replacements = {}, ...rest } = props
const replacers = Object.entries(replacements).map(([ selector, handler ]) => [ createMatcher(selector), handler ])
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
}
const doc = props.node ? props.node : parseHtml(props.html, { adjuster })
const node = xpath ? findNode(doc, xpath) : doc
const slotHandler: SlotHandler = (slotName, node) => {
return Astro.slots.render(slotName, [ node, { slotHandler, replacers } ] )
}
const nextDebug = debug ? debug - 1 : 0
---
<Match node={node} debug={nextDebug} replacers={replacers} slotHandler={slotHandler}/>
<External {...rest} debug={nextDebug} replacers={replacers} slotHandler={slotHandler}/>
......