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
3497e932
authored
2024-07-12 16:23:40 -0500
by
Adam Heath
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
Add options when parsing html, and if for react use, map certain
attributes.
1 parent
3f061832
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
22 additions
and
9 deletions
lib/html.ts
lib/html.ts
View file @
3497e93
...
...
@@ -307,9 +307,17 @@ export const createMatcher = (selector: string): Matcher => {
return
newMatcherCreater
()
}
const
reactAttributeMap
=
{
'class'
:
'className'
,
'srcset'
:
'srcSet'
,
'maxlength'
:
'maxLength'
,
}
class
NodeProxyHandler
{
#
options
#
cache
constructor
()
{
constructor
(
options
=
{})
{
this
.
#
options
=
options
this
.
#
cache
=
{}
}
...
...
@@ -321,13 +329,18 @@ class NodeProxyHandler {
switch
(
prop
)
{
case
'attributes'
:
return
this
.
#
cache
[
prop
]
=
(
origValue
?
Object
.
fromEntries
(
Object
.
entries
(
origValue
).
map
(([
key
,
value
])
=>
{
return
[
key
,
decode
(
value
)
]
const
decoded
=
decode
(
value
)
if
(
this
.
#
options
.
react
)
{
const
{
[
key
]:
newKey
}
=
reactAttributeMap
if
(
newKey
)
return
[
newKey
,
decoded
]
}
return
[
key
,
decoded
]
}))
:
origValue
)
return
newValue
case
'parent'
:
return
this
.
#
cache
[
prop
]
=
proxyNode
(
origValue
)
return
this
.
#
cache
[
prop
]
=
proxyNode
(
origValue
,
this
.
#
options
)
case
'children'
:
return
this
.
#
cache
[
prop
]
=
origValue
.
map
((
child
)
=>
proxyNode
(
child
))
return
this
.
#
cache
[
prop
]
=
origValue
.
map
((
child
)
=>
proxyNode
(
child
,
this
.
#
options
))
default
:
if
(
typeof
origValue
===
'function'
)
{
return
this
.
#
cache
[
prop
]
=
(...
args
)
=>
{
...
...
@@ -339,16 +352,16 @@ class NodeProxyHandler {
}
}
const
proxyNode
=
(
node
)
=>
{
return
new
Proxy
(
node
,
new
NodeProxyHandler
())
const
proxyNode
=
(
node
,
options
)
=>
{
return
new
Proxy
(
node
,
new
NodeProxyHandler
(
options
))
}
export
const
parseHtml
=
(
html
:
string
):
NodeType
=>
{
export
const
parseHtml
=
(
html
:
string
,
options
):
NodeType
=>
{
const
cached
=
parsedHtmlCache
.
get
(
html
)
if
(
cached
)
return
proxyNode
(
cached
)
if
(
cached
)
return
proxyNode
(
cached
,
options
)
const
doc
=
ultraParse
(
html
)
parsedHtmlCache
.
set
(
html
,
doc
)
return
proxyNode
(
doc
)
return
proxyNode
(
doc
,
options
)
}
export
const
findNode
=
(
doc
:
NodeType
,
selector
:
string
):
NodeType
=>
{
...
...
Please
register
or
sign in
to post a comment