Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F996033
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
3 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/src/index.ts b/src/index.ts
index bdd4ecf..2a5f963 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -1,132 +1,132 @@
import { tempstream } from "tempstream";
import { JDDContext } from "./jdd-context";
import { Registry } from "./registry";
import { EarlyAsset } from "./component";
import { hasField } from "@sealcode/ts-predicates";
export * from "./component";
export * from "./component-arguments/component-argument";
export * as ComponentArguments from "./component-arguments/component-arguments";
export * from "./component-arguments/component-arguments"; // exporting this also as root elements to make it easier to auto-import those
export * from "./jdd-context";
export * from "./registry";
export { insert_nbsp } from "./utils/insert_nbsp";
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=""`) ||
+ `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=""`) ||
+ `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/x-diff
Expires
Tue, Dec 24, 07:44 (23 h, 33 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
557044
Default Alt Text
(3 KB)
Attached To
Mode
R130 jdd
Attached
Detach File
Event Timeline
Log In to Comment