Replace now just contains replacements/slotHandler, and defers html
parsing to External.
Showing
3 changed files
with
29 additions
and
19 deletions
... | @@ -3,8 +3,9 @@ import Custom from './custom.astro' | ... | @@ -3,8 +3,9 @@ import Custom from './custom.astro' |
3 | import Match from './match.astro' | 3 | import Match from './match.astro' |
4 | import Node from './node.astro' | 4 | import Node from './node.astro' |
5 | import Replace from './replace.astro' | 5 | import Replace from './replace.astro' |
6 | import External from './external.astro' | ||
6 | 7 | ||
7 | export { Children, Custom, Match, Node, Replace } | 8 | export { Children, Custom, Match, Node, Replace, External } |
8 | 9 | ||
9 | import type { NodeType } from 'ultrahtml' | 10 | import type { NodeType } from 'ultrahtml' |
10 | type SlotHandler = (string, NodeType) => Promise<Any> | 11 | type SlotHandler = (string, NodeType) => Promise<Any> | ... | ... |
lib/external.astro
0 → 100644
1 | --- | ||
2 | import { parseHtml, createMatcher, findNode } from './html.ts' | ||
3 | import Match from './match.astro' | ||
4 | |||
5 | const { props } = Astro | ||
6 | const { debug, replacers, slotHandler } = props | ||
7 | |||
8 | const adjustmentsCompiled = Object.entries(adjustments).map(([ selector, handler ]) => [ createMatcher(selector), handler ]) | ||
9 | const adjuster = (node, parent, index) => { | ||
10 | for (const [ matcher, handler ] of adjustmentsCompiled) { | ||
11 | if (matcher(node, parent, index, false)) { | ||
12 | node = handler(node) | ||
13 | } | ||
14 | } | ||
15 | return node | ||
16 | } | ||
17 | |||
18 | const doc = props.node ? props.node : parseHtml(props.html, { adjuster }) | ||
19 | const node = xpath ? findNode(doc, xpath) : doc | ||
20 | |||
21 | const nextDebug = debug ? debug - 1 : 0 | ||
22 | --- | ||
23 | <Match node={node} debug={nextDebug} replacers={replacers} slotHandler={slotHandler} adjuster={adjuster}/> |
1 | --- | 1 | --- |
2 | import { walkSync } from 'ultrahtml' | 2 | import { createMatcher } from './html.ts' |
3 | import { parseHtml, createMatcher, findNode } from './html.ts' | 3 | import External from './external.astro' |
4 | import Match from './match.astro' | ||
5 | import type { SlotHandler, Replacements } from './astro.ts' | 4 | import type { SlotHandler, Replacements } from './astro.ts' |
6 | 5 | ||
7 | interface Props { | 6 | interface Props { |
... | @@ -13,26 +12,13 @@ interface Props { | ... | @@ -13,26 +12,13 @@ interface Props { |
13 | } | 12 | } |
14 | 13 | ||
15 | const { props } = Astro | 14 | const { props } = Astro |
16 | const { html, debug = 0, xpath, replacements = {}, adjustments = {} } = props | 15 | const { debug = 0, replacements = {}, ...rest } = props |
17 | 16 | ||
18 | const replacers = Object.entries(replacements).map(([ selector, handler ]) => [ createMatcher(selector), handler ]) | 17 | const replacers = Object.entries(replacements).map(([ selector, handler ]) => [ createMatcher(selector), handler ]) |
19 | 18 | ||
20 | const adjustmentsCompiled = Object.entries(adjustments).map(([ selector, handler ]) => [ createMatcher(selector), handler ]) | ||
21 | const adjuster = (node, parent, index) => { | ||
22 | for (const [ matcher, handler ] of adjustmentsCompiled) { | ||
23 | if (matcher(node, parent, index, false)) { | ||
24 | node = handler(node) | ||
25 | } | ||
26 | } | ||
27 | return node | ||
28 | } | ||
29 | |||
30 | const doc = props.node ? props.node : parseHtml(props.html, { adjuster }) | ||
31 | const node = xpath ? findNode(doc, xpath) : doc | ||
32 | |||
33 | const slotHandler: SlotHandler = (slotName, node) => { | 19 | const slotHandler: SlotHandler = (slotName, node) => { |
34 | return Astro.slots.render(slotName, [ node, { slotHandler, replacers } ] ) | 20 | return Astro.slots.render(slotName, [ node, { slotHandler, replacers } ] ) |
35 | } | 21 | } |
36 | const nextDebug = debug ? debug - 1 : 0 | 22 | const nextDebug = debug ? debug - 1 : 0 |
37 | --- | 23 | --- |
38 | <Match node={node} debug={nextDebug} replacers={replacers} slotHandler={slotHandler}/> | 24 | <External {...rest} debug={nextDebug} replacers={replacers} slotHandler={slotHandler}/> | ... | ... |
-
Please register or sign in to post a comment