feat: delete server:script

This commit is contained in:
Ayo 2023-10-10 22:46:29 +02:00
parent 3b246cb711
commit 69499d61ab
2 changed files with 17 additions and 3 deletions

View file

@ -1,4 +1,4 @@
import { ELEMENT_NODE, parse, walkSync } from "ultrahtml";
import { ELEMENT_NODE, parse, renderSync, walkSync } from "ultrahtml";
import { parseScript } from "esprima";
export default eventHandler(async (event) => {
@ -14,7 +14,7 @@ export default eventHandler(async (event) => {
if (!html) html = await useStorage().getItem(getPath("/404.html"));
// transforms
const transforms = [insertRegistry, doSetUp];
const transforms = [insertRegistry, doSetUp, deleteServerScripts];
if (html) {
for (const transform of transforms) {
html = transform(html.toString());
@ -83,3 +83,17 @@ function doSetUp(html: string) {
return html;
}
function deleteServerScripts(html: string): string {
const ast = parse(html);
walkSync(ast, (node) => {
const { attributes } = node;
const attributeKeys = Object.keys(attributes ?? {});
const isServerScript = attributeKeys.some((key) => key.includes("server:"));
if (isServerScript) {
node.parent.children.splice(node.parent.children.indexOf(node), 1);
}
});
return renderSync(ast);
}

View file

@ -17,7 +17,7 @@
<body>
<a href="/about">{{ count }}</a>
<div>
<hello-world name="{{name}}"></hello-world>
<hello-world name="{{name }}"></hello-world>
</div>
<p>some text: {{greeting}}</p>
<clickable-text></clickable-text>