Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
brainfood
/
astro-wt
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
754d6092
authored
2024-11-22 09:44:18 -0600
by
Adam Heath
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
Adjustments are now done centrally, inside the node proxy, instead of
ad-hoc in Match.
1 parent
20070e45
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
24 additions
and
22 deletions
lib/children.astro
lib/custom.astro
lib/html.ts
lib/match.astro
lib/node.astro
lib/replace.astro
lib/children.astro
View file @
754d609
---
import Match from './match.astro'
const { props: { parent, children, debug = 0, replacers, slotHandler
, adjuster
} } = Astro
const { props: { parent, children, debug = 0, replacers, slotHandler } } = Astro
//console.log('Children:render', { parent, children, replacers })
if (debug) {
...
...
@@ -11,7 +11,7 @@ const nextDebug = debug ? debug - 1 : 0
---
{
Array.isArray(children) ?
children.map((child, index) => <Match parent={parent} node={child} index={index} debug={nextDebug} replacers={replacers} slotHandler={slotHandler}
adjuster={adjuster}
/>)
children.map((child, index) => <Match parent={parent} node={child} index={index} debug={nextDebug} replacers={replacers} slotHandler={slotHandler}/>)
: !children ? ''
: <Match parent={parent} node={children} index={0} debug={nextDebug} replacers={replacers} slotHandler={slotHandler}
adjuster={adjuster}
/>
: <Match parent={parent} node={children} index={0} debug={nextDebug} replacers={replacers} slotHandler={slotHandler}/>
}
...
...
lib/custom.astro
View file @
754d609
...
...
@@ -2,7 +2,7 @@
import Children from './children.astro'
const { props: { wrap = false, wrapAttributes = {}, node, replacers, slotHandler
, adjuster
} } = Astro
const { props: { wrap = false, wrapAttributes = {}, node, replacers, slotHandler } } = Astro
const { name: Name, attributes, children } = node
const CustomName = `custom-${Name}`
...
...
@@ -15,14 +15,14 @@ const CustomName = `custom-${Name}`
{
node.isSelfClosingTag ? <Name {...attributes}/>
: <Name {...attributes}>
<Children parent={node} children={children} replacers={replacers} slotHandler={slotHandler}
adjuster={adjuster}
/>
<Children parent={node} children={children} replacers={replacers} slotHandler={slotHandler}/>
</Name>
}
</CustomName>
) : (
node.isSelfClosingTag ? <CustomName {...attributes}/>
: <CustomName {...attributes}>
<Children parent={node} children={children} replacers={replacers} slotHandler={slotHandler}
adjuster={adjuster}
/>
<Children parent={node} children={children} replacers={replacers} slotHandler={slotHandler}/>
</CustomName>
)
}
...
...
lib/html.ts
View file @
754d609
...
...
@@ -344,7 +344,7 @@ class NodeProxyHandler {
#
options
#
cache
#
parent
constructor
(
options
=
{}
,
parent
=
null
)
{
constructor
(
options
,
parent
=
null
)
{
this
.
#
options
=
options
this
.
#
cache
=
{}
this
.
#
parent
=
parent
...
...
@@ -369,7 +369,7 @@ class NodeProxyHandler {
case
'parent'
:
return
this
.
#
parent
case
'children'
:
return
this
.
#
cache
[
prop
]
=
origValue
.
map
((
child
)
=>
proxyNode
(
child
,
this
.
#
options
,
this
))
return
this
.
#
cache
[
prop
]
=
origValue
.
map
((
child
,
index
)
=>
proxyNode
(
child
,
this
.
#
options
,
this
,
index
))
default
:
if
(
typeof
origValue
===
'function'
)
{
return
this
.
#
cache
[
prop
]
=
(...
args
)
=>
{
...
...
@@ -381,8 +381,10 @@ class NodeProxyHandler {
}
}
const
proxyNode
=
(
node
,
options
,
parent
)
=>
{
return
new
Proxy
(
node
,
new
NodeProxyHandler
(
options
,
parent
))
const
proxyNode
=
(
node
,
options
=
{},
parent
=
null
,
index
=
0
)
=>
{
const
{
adjuster
=
(
node
,
parent
,
index
)
=>
node
}
=
options
const
adjusted
=
adjuster
(
node
,
parent
,
index
)
return
new
Proxy
(
adjusted
,
new
NodeProxyHandler
(
options
,
parent
))
}
export
const
parseHtml
=
(
html
:
string
,
options
):
NodeType
=>
{
...
...
lib/match.astro
View file @
754d609
...
...
@@ -2,17 +2,16 @@
import { ELEMENT_NODE, TEXT_NODE } from 'ultrahtml'
import Node from './node.astro'
const { props: { parent = null, node, index = 0, debug = 0, replacers, slotHandler
, adjuster
} } = Astro
const { props: { parent = null, node, index = 0, debug = 0, replacers, slotHandler } } = Astro
const { name: Name, attributes } = node
const fixedNode = adjuster(node, parent, index)
if (debug) {
console.log('trying to match against', {
fixedN
ode})
console.log('trying to match against', {
n
ode})
}
let slotName
for (const [ matcher, handler ] of replacers) {
if (debug) console.log('attempting matcher', matcher.toString())
if (matcher(
fixedN
ode, parent, index, false)) {
if (matcher(
n
ode, parent, index, false)) {
if (debug) console.log('matched')
slotName = handler
}
...
...
@@ -21,6 +20,6 @@ for (const [ matcher, handler ] of replacers) {
const nextDebug = debug ? debug - 1 : 0
---
{
slotName ? slotHandler(slotName,
fixedN
ode)
: <Node parent={parent} node={
fixedNode} index={index} debug={nextDebug} replacers={replacers} slotHandler={slotHandler} adjuster={adjust
er}/>
slotName ? slotHandler(slotName,
n
ode)
: <Node parent={parent} node={
node} index={index} debug={nextDebug} replacers={replacers} slotHandler={slotHandl
er}/>
}
...
...
lib/node.astro
View file @
754d609
...
...
@@ -16,7 +16,7 @@ interface Props {
}
const { props: { parent = null, node, index = 0, debug = 0, replacers, slotHandler
, adjuster
} } = Astro
const { props: { parent = null, node, index = 0, debug = 0, replacers, slotHandler } } = Astro
const { name: Name, attributes } = node
if (debug) {
...
...
@@ -32,7 +32,7 @@ const nextDebug = debug ? debug - 1 : 0
: node.type === ELEMENT_NODE ? (
node.isSelfClosingTag ? <Name {...attributes}/>
: <Name {...attributes}>
<Children parent={node} children={node.children} debug={nextDebug} replacers={replacers} slotHandler={slotHandler}
adjuster={adjuster}
/>
<Children parent={node} children={node.children} debug={nextDebug} replacers={replacers} slotHandler={slotHandler}/>
</Name>
) : ''
}
...
...
lib/replace.astro
View file @
754d609
...
...
@@ -14,9 +14,7 @@ interface Props {
const { props } = Astro
const { html, debug = 0, xpath, replacements = {}, adjustments = {} } = props
const doc = props.node ? props.node : parseHtml(props.html)
const node = xpath ? findNode(doc, xpath) : doc
const replacers = Object.entries(replacements).map(([ selector, handler ]) => [ createMatcher(selector), handler ])
const adjustmentsCompiled = Object.entries(adjustments).map(([ selector, handler ]) => [ createMatcher(selector), handler ])
...
...
@@ -29,9 +27,12 @@ const adjuster = (node, parent, index) => {
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
, adjuster
} ] )
return Astro.slots.render(slotName, [ node, { slotHandler, replacers } ] )
}
const nextDebug = debug ? debug - 1 : 0
---
<Match node={node} debug={nextDebug} replacers={replacers} slotHandler={slotHandler}
adjuster={adjuster}
/>
<Match node={node} debug={nextDebug} replacers={replacers} slotHandler={slotHandler}/>
...
...
Please
register
or
sign in
to post a comment