Move createAdjuster from external to html.
Showing
2 changed files
with
14 additions
and
14 deletions
1 | --- | 1 | --- |
2 | import { parseHtml, createMatcher, findNode } from './html.ts' | 2 | import { parseHtml, createAdjuster, createMatcher, findNode } from './html.ts' |
3 | import { getUrl, getSitePage } from './remote-content.ts' | 3 | import { getUrl, getSitePage } from './remote-content.ts' |
4 | import Match from './match.astro' | 4 | import Match from './match.astro' |
5 | 5 | ||
... | @@ -24,19 +24,6 @@ export const getNodesFromProps = async (props, options) => { | ... | @@ -24,19 +24,6 @@ export const getNodesFromProps = async (props, options) => { |
24 | return xpath ? findNode(doc, xpath, { single: false }) : [ doc ] | 24 | return xpath ? findNode(doc, xpath, { single: false }) : [ doc ] |
25 | } | 25 | } |
26 | 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 | } | ||
34 | } | ||
35 | return node | ||
36 | } | ||
37 | return adjuster | ||
38 | } | ||
39 | |||
40 | const { props } = Astro | 27 | const { props } = Astro |
41 | const { debug, replacers, slotHandler } = props | 28 | const { debug, replacers, slotHandler } = props |
42 | const nodes = await getNodesFromProps(props) | 29 | const nodes = await getNodesFromProps(props) | ... | ... |
... | @@ -343,6 +343,19 @@ export const createMatcher = (selector: string): Matcher => { | ... | @@ -343,6 +343,19 @@ export const createMatcher = (selector: string): Matcher => { |
343 | return newMatcherCreater() | 343 | return newMatcherCreater() |
344 | } | 344 | } |
345 | 345 | ||
346 | export const createAdjuster = (adjustments = {}) => { | ||
347 | const adjustmentsCompiled = Object.entries(adjustments).map(([ selector, handler ]) => [ createMatcher(selector), handler ]) | ||
348 | const adjuster = (node, parent, index) => { | ||
349 | for (const [ matcher, handler ] of adjustmentsCompiled) { | ||
350 | if (matcher(node, parent, index, false)) { | ||
351 | node = handler(node) | ||
352 | } | ||
353 | } | ||
354 | return node | ||
355 | } | ||
356 | return adjuster | ||
357 | } | ||
358 | |||
346 | const reactAttributeMap = { | 359 | const reactAttributeMap = { |
347 | 'class': 'className', | 360 | 'class': 'className', |
348 | 'srcset': 'srcSet', | 361 | 'srcset': 'srcSet', | ... | ... |
-
Please register or sign in to post a comment