feat: nested sub pages

This commit is contained in:
Ayo 2023-10-07 22:45:45 +02:00
parent 6d9a3f2847
commit 77a93f8507
4 changed files with 50 additions and 2 deletions

11
pages/about.html Normal file
View file

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>top about</title>
</head>
<body>
<h1>Top Level About</h1>
</body>
</html>

11
pages/about/index.html Normal file
View file

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>About Me</title>
</head>
<body>
<h1>About Me</h1>
</body>
</html>

11
pages/about/me.html Normal file
View file

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>About Meeeee</title>
</head>
<body>
<h1>About Meeeeeezzz</h1>
</body>
</html>

View file

@ -1,7 +1,22 @@
import * as fs from "fs";
export default eventHandler((event) => {
const filename = event.path === "/" ? "/index.html" : `${event.path}.html`;
const path = `./src/pages${filename}`;
const html = fs.readFileSync(path, "utf8");
const fallback = getPath(event.path + "/index.html");
const path = getPath(filename);
let html = "";
try {
html = fs.readFileSync(path, "utf8");
} catch (error) {
try {
html = fs.readFileSync(fallback, "utf8");
} catch {
html = `cannot find ${path} or ${fallback}`;
}
}
return html;
});
function getPath(filename: string) {
return `./pages${filename}`;
}