diff --git a/.gitignore b/.gitignore index 147414a..dbaf881 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ node_modules/ dist/ +.wrangler/ .cache/ .env diff --git a/config/11ty/filters.js b/config/11ty/filters.js index e4709d0..c7f8305 100644 --- a/config/11ty/filters.js +++ b/config/11ty/filters.js @@ -1,5 +1,6 @@ import { DateTime } from 'luxon'; import urlize from '../urlize.js'; +import stringify from '../json-pretty-stringify.js'; const toShortDate = (dateObject) => { // 10/14/1983 @@ -51,5 +52,8 @@ export const filters = (eleventyConfig) => { eleventyConfig.addFilter('capitalize', (string) => { return string.charAt(0).toUpperCase() + string.slice(1); }); + eleventyConfig.addFilter('stringify', (value) => { + return stringify(value, '\t'); + }); eleventyConfig.addFilter('url', urlize); }; diff --git a/config/json-pretty-stringify.js b/config/json-pretty-stringify.js new file mode 100644 index 0000000..d665e23 --- /dev/null +++ b/config/json-pretty-stringify.js @@ -0,0 +1,28 @@ +export default function (input, space) { + return JSON.stringify( + input, + function (k, v) { + if (Array.isArray(v)) { + let children = ''; + + for (let index = 0, l = v.length; index < l; index++) { + const value = v[index]; + const child = JSON.stringify(value); + + if (child === undefined) continue; + + children += children ? `, ${child}` : child; + } + + return `[${children}]`; + } + return v; + }, + space, + ) + .replaceAll('\\', '') + .replaceAll('"[', '[') + .replaceAll(']"', ']') + .replaceAll('"{', '{') + .replaceAll('}"', '}'); +} diff --git a/functions/index.ts b/functions/index.ts new file mode 100644 index 0000000..27fc55d --- /dev/null +++ b/functions/index.ts @@ -0,0 +1,22 @@ +import stringify from '../config/json-pretty-stringify.js'; +import whoami from '../src/_data/whoami.js'; + +// @ts-expect-error +export async function onRequest(context: EventContext): PagesFunction { + const { next, request } = context as { + next: ( + input?: Request | string, + init?: RequestInit, + ) => Promise; + request: Request; + }; + + if ( + new URL(request.url).pathname === '/' && + request.headers.get('user-agent')?.startsWith('curl/') + ) { + return new Response(stringify(whoami, 2)); + } + + return await next(); +} diff --git a/functions/package.json b/functions/package.json new file mode 100644 index 0000000..9c3a622 --- /dev/null +++ b/functions/package.json @@ -0,0 +1,7 @@ +{ + "name": "functions", + "version": "0.1.0", + "devDependencies": { + "@cloudflare/workers-types": "^4.20240208.0" + } +} diff --git a/functions/pnpm-lock.yaml b/functions/pnpm-lock.yaml new file mode 100644 index 0000000..e25c7d2 --- /dev/null +++ b/functions/pnpm-lock.yaml @@ -0,0 +1,16 @@ +lockfileVersion: '6.0' + +settings: + autoInstallPeers: true + excludeLinksFromLockfile: false + +devDependencies: + '@cloudflare/workers-types': + specifier: ^4.20240208.0 + version: 4.20240208.0 + +packages: + + /@cloudflare/workers-types@4.20240208.0: + resolution: {integrity: sha512-MVGTTjZpJu4kJONvai5SdJzWIhOJbuweVZ3goI7FNyG+JdoQH41OoB+nMhLsX626vPLZVWGPIWsiSo/WZHzgQw==} + dev: true diff --git a/functions/tsconfig.json b/functions/tsconfig.json new file mode 100644 index 0000000..d9d3a20 --- /dev/null +++ b/functions/tsconfig.json @@ -0,0 +1,17 @@ +{ + "compilerOptions": { + "noEmit": true, + "module": "esnext", + "target": "esnext", + "lib": ["esnext"], + "strict": true, + "alwaysStrict": true, + "preserveConstEnums": true, + "moduleResolution": "node", + "resolveJsonModule": true, + "sourceMap": true, + "types": ["@cloudflare/workers-types"] + }, + "include": ["src"], + "exclude": ["node_modules", "dist", "test"] +} diff --git a/package.json b/package.json index 06b8e2b..ad5d258 100644 --- a/package.json +++ b/package.json @@ -58,5 +58,5 @@ "prettier": "^3.2.5", "rimraf": "^5.0.5" }, - "packageManager": "pnpm@8.9.0" + "packageManager": "pnpm@8.15.1" } diff --git a/src/_data/whoami.js b/src/_data/whoami.js new file mode 100644 index 0000000..de2a459 --- /dev/null +++ b/src/_data/whoami.js @@ -0,0 +1,12 @@ +import site from '../../site.config.js'; + +export default { + name: site.author.name, + website: site.url, + occupation: 'Student', + location: 'United States', + languages: ['JavaScript', 'TypeScript', 'Rust', 'Python', 'HTML', 'CSS'], + frameworks: ['SolidJS', 'Eleventy', 'TailwindCSS'], + interests: ['Web Development', 'Software Development', 'Open Source'], + learning: ['Rust', 'Vue', 'Fortran', 'Swift'], +}; diff --git a/src/index.njk b/src/index.njk index 427cd0e..262b964 100644 --- a/src/index.njk +++ b/src/index.njk @@ -11,16 +11,7 @@ templateEngineOverride: njk, md
```js -const whoami = { - name: "{{ site.author.name }}", - website: "{{ site.url }}", - occupation: "Student", - location: "United States", - languages: ["JavaScript", "TypeScript", "Rust", "Python", "HTML", "CSS"], - frameworks: ["SolidJS", "Eleventy", "TailwindCSS"], - interests: ["Web Development", "Software Development", "Open Source"], - learning: ["Rust", "Vue", "Fortran", "Swift"], -}; +const whoami = {{ whoami | stringify | safe }}; ```