-
Notifications
You must be signed in to change notification settings - Fork 14
/
buildPage.js
61 lines (59 loc) · 1.74 KB
/
buildPage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
export default function (
containerSelector,
scrollIntoView,
scrollIntoViewOptions,
localLinkDetector,
scanOnMount,
analytics,
defaultAnimation
) {
return `${
containerSelector
? `const newContent = doc.querySelector("${containerSelector}");
${
scrollIntoView
? `const container = document.querySelector("${containerSelector}");
container.replaceWith(newContent);
newContent.scrollIntoView(${JSON.stringify(scrollIntoViewOptions)});`
: ""
}
document.head.replaceWith(doc.head);
${localLinkDetector ? "styleLocalLink();" : ""}`
: `document.documentElement.replaceWith(doc.documentElement);` +
(scrollIntoView
? `document.documentElement.scrollIntoView(${JSON.stringify(
scrollIntoViewOptions
)});`
: "")
}
[
...document.${
containerSelector
? `querySelectorAll("head script, ${containerSelector} script")`
: "scripts"
}
].forEach((script) => {
const newScript = document.createElement("script");
newScript.text = script.text;
for (const attr of script.attributes) {
newScript.setAttribute(attr.name, attr.value);
}
script.replaceWith(newScript);
});
${scanOnMount ? "AstroSpa.scan();" : ""}
${analytics ? "AstroSpa.track();" : ""}
window.dispatchEvent(new Event("mount"));
window.onMount && window.onMount();
${
defaultAnimation
? `${
containerSelector ? "newContent" : "document.documentElement"
}.animate(
{
opacity: [0, 1],
},
1000
);`
: ""
}`;
}