diff --git a/package-lock.json b/package-lock.json index 77e30e8..be212f9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,11 +8,13 @@ "name": "bgenc.net-new", "version": "0.0.1", "dependencies": { - "@sveltejs/adapter-static": "^2.0.3" + "@sveltejs/adapter-static": "^2.0.3", + "reading-time-estimator": "^1.11.0" }, "devDependencies": { "@sveltejs/kit": "^1.27.6", "@types/lodash": "^4.14.201", + "@types/node": "^20.12.8", "@typescript-eslint/eslint-plugin": "^6.11.0", "@typescript-eslint/parser": "^6.11.0", "date-fns": "^2.30.0", @@ -360,6 +362,15 @@ "integrity": "sha512-y9euML0cim1JrykNxADLfaG0FgD1g/yTHwUs/Jg9ZIU7WKj2/4IW9Lbb1WZbvck78W/lfGXFfe+u2EGfIJXdLQ==", "dev": true }, + "node_modules/@types/node": { + "version": "20.12.8", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.12.8.tgz", + "integrity": "sha512-NU0rJLJnshZWdE/097cdCBbyW1h4hEg0xpovcoAQYHl8dnEyp/NAOiE45pvc+Bd1Dt+2r94v2eGFpQJ4R7g+2w==", + "devOptional": true, + "dependencies": { + "undici-types": "~5.26.4" + } + }, "node_modules/@types/pug": { "version": "2.0.9", "dev": true, @@ -2356,6 +2367,11 @@ "node": ">=8.10.0" } }, + "node_modules/reading-time-estimator": { + "version": "1.11.0", + "resolved": "https://registry.npmjs.org/reading-time-estimator/-/reading-time-estimator-1.11.0.tgz", + "integrity": "sha512-2813WXSxCPsDvgrYq+cPBI4gGqgmtKlo62rpI0mwnhrMfZJIqbDfOeidr9Y36OruDesQGmYA9DkVRmvsBbv2DA==" + }, "node_modules/regenerator-runtime": { "version": "0.14.0", "resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.14.0.tgz", @@ -2966,6 +2982,12 @@ "node": ">=14.0" } }, + "node_modules/undici-types": { + "version": "5.26.5", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz", + "integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==", + "devOptional": true + }, "node_modules/unist-util-stringify-position": { "version": "2.0.3", "dev": true, diff --git a/package.json b/package.json index a42770b..cfb5a19 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "devDependencies": { "@sveltejs/kit": "^1.27.6", "@types/lodash": "^4.14.201", + "@types/node": "^20.12.8", "@typescript-eslint/eslint-plugin": "^6.11.0", "@typescript-eslint/parser": "^6.11.0", "date-fns": "^2.30.0", @@ -34,6 +35,7 @@ }, "type": "module", "dependencies": { - "@sveltejs/adapter-static": "^2.0.3" + "@sveltejs/adapter-static": "^2.0.3", + "reading-time-estimator": "^1.11.0" } } diff --git a/src/lib/Button.svelte b/src/lib/Button.svelte index 719e783..2918f58 100644 --- a/src/lib/Button.svelte +++ b/src/lib/Button.svelte @@ -14,7 +14,9 @@ diff --git a/src/routes/posts/[[page=integer]]/+page.ts b/src/routes/posts/[[page=integer]]/+page.server.ts similarity index 59% rename from src/routes/posts/[[page=integer]]/+page.ts rename to src/routes/posts/[[page=integer]]/+page.server.ts index 8ca2550..a71cf9c 100644 --- a/src/routes/posts/[[page=integer]]/+page.ts +++ b/src/routes/posts/[[page=integer]]/+page.server.ts @@ -1,4 +1,10 @@ import _ from 'lodash'; +import { readingTime } from 'reading-time-estimator'; +import path from 'path'; +import { fileURLToPath } from 'url'; +import { readFile } from 'fs/promises'; + +const __dirname = path.dirname(fileURLToPath(import.meta.url)); const PAGE_SIZE = 10; @@ -7,15 +13,18 @@ export async function load({ params }) { const iterablePostFiles = Object.entries(allPostFiles); const posts = await Promise.all( - iterablePostFiles.map(async ([path, resolver]) => { + iterablePostFiles.map(async ([filePath, resolver]) => { const { metadata } = await resolver(); - const slug = /[/]([^/]*)[.]md$/.exec(path)?.[1]; - if (!slug) throw new Error(`Could not parse slug from path: ${path}`); + const slug = /[/]([^/]*)[.]md$/.exec(filePath)?.[1]; + if (!slug) throw new Error(`Could not parse slug from path: ${filePath}`); + + const contents = await readFile(path.join(__dirname, '..', `${slug}.md`), 'utf8'); return { meta: metadata, path: slug, + readingTime: readingTime(contents), }; }), ); @@ -33,3 +42,4 @@ export async function load({ params }) { pageCount: Math.ceil(posts.length / PAGE_SIZE), }; } +export type Post = Awaited>['posts'][number]; diff --git a/src/routes/posts/[[page=integer]]/+page.svelte b/src/routes/posts/[[page=integer]]/+page.svelte index e23ca00..e5ceb66 100644 --- a/src/routes/posts/[[page=integer]]/+page.svelte +++ b/src/routes/posts/[[page=integer]]/+page.svelte @@ -1,11 +1,13 @@