0
Fork 0

dynamic blog post list

This commit is contained in:
Pavocracy 2024-01-29 22:42:50 +10:00
parent 76dcb0a09d
commit 18faa41e4c
Signed by: Pavocracy
GPG Key ID: 4669764A8BBA94F9
14 changed files with 3994 additions and 38 deletions

View File

@ -1,3 +1,9 @@
# www
My personal website built with htmx and tailwind, running with bun!
My personal website built with htmx and tailwind, running with bun!
# Usage
To setup run the command `bun run setup`
Then run the command `bun run dev` and visit `http://localhost:3000` in a browser

View File

@ -1,30 +0,0 @@
#!/usr/bin/env bun
// Copyright (c) 2024 Pavocracy <pavocracy@pm.me>
// This file is released under MIT License
// Find this project at https://code.pavocracy.dev/personal-projects/www
import { $ } from "bun"
import { readdir } from "node:fs/promises"
console.log("Starting build process...")
const src_dir = `${import.meta.dir}/src`
const dist_dir = `${import.meta.dir}/dist`
await Bun.build({
root: src_dir,
entrypoints: [`${src_dir}/server.ts`],
outdir: dist_dir,
minify: true,
target: "bun",
})
await $`bun x tailwindcss -o ${dist_dir}/css/styles.css --minify`
for (const dir of ["pages", "assets"]) {
const files = await readdir(`${src_dir}/${dir}`)
for (const curr_file of files) {
const write_file = Bun.file(`${src_dir}/${dir}/${curr_file}`)
await Bun.write(`${dist_dir}/${dir}/${curr_file}`, write_file)
}
}

View File

@ -0,0 +1,2 @@
# Another blog
more ranting

View File

@ -0,0 +1,2 @@
# Blog post
this is a blog post

View File

@ -0,0 +1,2 @@
# OMG
something amazing happened

View File

@ -1,7 +1,11 @@
{
"name": "www",
"module": "src/server.ts",
"module": "server.ts",
"type": "module",
"scripts": {
"setup": "bun run setup.ts",
"dev": "bun --hot run server.ts"
},
"devDependencies": {
"@types/bun": "latest"
},

View File

@ -8,12 +8,17 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="styles.css" rel="stylesheet">
<script src="htmx.js"></script>
</head>
<body>
<h1 class="text-3xl font-bold underline">
Hello world!
</h1>
<p>
Blog posts testing
</p>
<ul hx-get="/blog" hx-trigger="load"></ul>
</body>
</html>

18
routes/blog.ts Normal file
View File

@ -0,0 +1,18 @@
// Copyright (c) 2024 Pavocracy <pavocracy@pm.me>
// This file is released under MIT License
// Find this project at https://code.pavocracy.dev/personal-projects/www
import { readdirSync } from "node:fs"
import { run_dir } from "../server"
export function getBlogPosts(): string {
var blog_posts = ""
const files = readdirSync(`${run_dir}/content`)
files.sort((a, b) => a > b ? -1 : 1)
for (const curr_file of files) {
blog_posts += `<li>${curr_file.split(".", 1)}</li>`
}
return blog_posts
}

View File

@ -4,29 +4,49 @@
// Find this project at https://code.pavocracy.dev/personal-projects/www
import fs from "fs"
import { getBlogPosts } from "./routes/blog"
console.log("Listening on port 3000")
const run_dir = import.meta.dir
console.log(`Running from ${run_dir}`)
const date_fmt = new Intl.DateTimeFormat("en-AU", {
year: "2-digit",
month: "2-digit",
day: "2-digit",
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
timeZoneName: "short",
hour12: true,
timeZone: "Australia/Brisbane",
})
Bun.serve({
fetch(req, server) {
const client = server.requestIP(req)
const url = new URL(req.url)
console.log(`[${Date()}] ${client?.address}:${client?.port} requested ${url}`)
console.log(`[${date_fmt.format(new Date())}] ${client?.address}:${client?.port} ${req.method} ${url}`)
if (url.pathname === "/") url.pathname = "/index.html"
let requested_file = `${run_dir}/`
if (url.pathname.endsWith(".html")) requested_file += `pages/${url.pathname}`
if (url.pathname.endsWith(".css")) requested_file += `css/${url.pathname}`
if (url.pathname.endsWith(".css") || url.pathname.endsWith(".js")) requested_file += `static/${url.pathname}`
if (url.pathname.endsWith(".png") || url.pathname.endsWith(".ico")) requested_file += `assets/${url.pathname}`
if (url.pathname === "/blog") {
return new Response(getBlogPosts())
}
if (fs.existsSync(requested_file)) {
return new Response(Bun.file(requested_file))
} else {
return Response.json("Not Found", 404)
}
return Response.json("Not Found", 404)
},
})
})
export { run_dir }

22
setup.ts Executable file
View File

@ -0,0 +1,22 @@
#!/usr/bin/env bun
// Copyright (c) 2024 Pavocracy <pavocracy@pm.me>
// This file is released under MIT License
// Find this project at https://code.pavocracy.dev/personal-projects/www
import { $ } from "bun"
const src_dir = import.meta.dir
console.log("Starting setup process...")
console.log("Installing dependencies")
await $`bun install`
console.log("Building tailwindcss")
await $`bun x tailwindcss -o ${src_dir}/static/styles.css`
console.log("Copying htmx.js from node_modules")
const htmx = Bun.file(`${src_dir}/node_modules/htmx.org/dist/htmx.js`)
await Bun.write(`${src_dir}/static/htmx.js`, htmx)
console.log("Build finished!")

3905
static/htmx.js Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,7 +1,7 @@
import type { Config } from 'tailwindcss'
export default {
content: ["./src/pages/*.html"],
content: ["pages/*.html"],
theme: {
extend: {},
},