Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F4637545
index.ts
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
3 KB
Referenced Files
None
Subscribers
None
index.ts
View Options
import
{
tempstream
}
from
"tempstream"
;
import
{
JDDContext
}
from
"./jdd-context.js"
;
import
{
Registry
}
from
"./registry.js"
;
import
{
EarlyAsset
}
from
"./component.js"
;
import
{
hasField
}
from
"@sealcode/ts-predicates"
;
export
*
from
"./component.js"
;
export
*
from
"./component-arguments/component-argument.js"
;
export
*
as
ComponentArguments
from
"./component-arguments/component-arguments.js"
;
export
*
from
"./component-arguments/component-arguments.js"
;
// exporting this also as root elements to make it easier to auto-import those
export
*
from
"./jdd-context.js"
;
export
*
from
"./registry.js"
;
export
{
insert_nbsp
}
from
"./utils/insert_nbsp.js"
;
export
type
JDDocument
=
Array
<
{
component_name
:
string
;
args
:
Record
<
string
,
unknown
>
;
}
>
;
export
function
countWords
(
registry
:
Registry
,
document
:
JDDocument
)
:
number
{
return
document
.
reduce
((
acc
,
{
component_name
,
args
})
=>
{
const
component
=
registry
.
get
(
component_name
);
if
(
!
component
)
{
console
.
warn
(
"Component not found in the registry: "
+
component_name
);
return
acc
+
0
;
}
return
acc
+
component
.
countWords
(
args
);
},
0
);
}
export
async
function
renderEarlyAssets
(
registry
:
Registry
,
document
:
JDDocument
,
context
:
JDDContext
)
{
const
early_assets
=
(
await
Promise
.
all
(
document
.
map
(
async
({
component_name
,
args
})
=>
{
const
component
=
registry
.
get
(
component_name
);
if
(
!
component
)
{
console
.
warn
(
"Component not found in the registry: "
+
component_name
);
return
[];
}
for
(
const
arg_name
in
component
?
.
getArguments
())
{
if
(
!
Object
.
prototype
.
hasOwnProperty
.
call
(
args
,
arg_name
))
{
args
[
arg_name
]
=
component
?
.
getArguments
()
[
arg_name
]
?
.
getEmptyValue
();
}
}
return
await
component
.
getEarlyAssets
(
args
,
context
);
})
)
).
flat
();
const
deduplicated_assets
:
Record
<
string
,
EarlyAsset
>
=
{};
for
(
const
asset
of
early_assets
)
{
deduplicated_assets
[
asset
.
identity
]
=
asset
;
}
return
Object
.
values
(
deduplicated_assets
)
.
map
((
asset
)
=>
{
if
(
asset
.
type
==
"script"
)
{
if
(
hasField
(
"url"
,
asset
))
{
return
/* HTML */
`<script
async
src="
${
asset
.
url
}
"
onLoad="document.dispatchEvent(new Event('loaded-
${
asset
.
identity
}
'))"
${
(
asset
.
integrity
&&
`integrity="
${
asset
.
integrity
}
" crossorigin="anonymous"`
)
||
""
}
></script>`
;
}
else
{
return
/* HTML */
`<script><
${
asset
.
content
}
/script>`
;
}
}
else
if
(
asset
.
type
==
"style"
)
{
if
(
hasField
(
"url"
,
asset
))
{
const
integrity
=
(
asset
.
integrity
&&
`integrity="
${
asset
.
integrity
}
" crossorigin="anonymous"`
)
||
""
;
// see https://web.dev/articles/defer-non-critical-css
return
/* HTML */
`<link
rel="preload"
href="
${
asset
.
url
}
"
as="style"
onload="this.onload=null;this.rel='stylesheet'"
${
integrity
}
/>
<noscript
><link
rel="stylesheet"
href="
${
asset
.
url
}
"
${
integrity
}
/></noscript>`
;
}
else
{
return
/* HTML */
`<style>
${
asset
.
content
}
</style>`
;
}
}
})
.
join
(
" "
);
}
export
function
render
(
registry
:
Registry
,
document
:
JDDocument
,
context
:
JDDContext
)
{
return
tempstream
`
${
document
.
map
(({
component_name
,
args
}
) => {
const component = registry.get(component_name);
if (!component) {
console.warn(
"Component not found in the registry: " + component_name
);
return "";
}
for (const arg_name in component?.getArguments()) {
if (!Object.prototype.hasOwnProperty.call(args, arg_name)) {
args[arg_name] = component
?.getArguments()
[arg_name]?.getEmptyValue();
}
}
return component.toHTML(args, context);
})}`
;
}
File Metadata
Details
Attached
Mime Type
text/html
Expires
Tue, May 27, 23:48 (1 d, 20 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
625007
Default Alt Text
index.ts (3 KB)
Attached To
Mode
rJDD jdd
Attached
Detach File
Event Timeline
Log In to Comment