-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use endpoints instead of pulling file metadata into browser memory
- Loading branch information
Showing
5 changed files
with
48 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1 @@ | ||
const slugRegex = /([\w-]+)\.(svelte\.md|md|svx)/i; | ||
|
||
|
||
|
||
export async function getAllPosts(globArr) { | ||
const postPromises = []; | ||
|
||
for (let [path, resolver] of Object.entries(globArr)) { | ||
const match = path.match(slugRegex); | ||
|
||
if (!match || !match[1]) continue; | ||
|
||
const slug = match[1]; | ||
const promise = resolver().then((post) => [ | ||
slug, | ||
{ | ||
...post.metadata, | ||
date: new Date(post.metadata.date) | ||
} | ||
]); | ||
postPromises.push(promise); | ||
} | ||
|
||
return Promise.all(postPromises); | ||
} | ||
|
||
export function fromEntries(entries) { | ||
return entries.reduce((acc, [key, val]) => { | ||
acc[key] = val; | ||
return acc; | ||
}, {}); | ||
} | ||
export const slugFromPath = (path) => path.match(/([\w-]+)\.(svelte\.md|md|svx)/i)?.[1] ?? null; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { slugFromPath } from '$lib/util'; | ||
|
||
/** | ||
* @type {import('@sveltejs/kit').RequestHandler} | ||
*/ | ||
export async function get({ params }) { | ||
const modules = import.meta.glob(`./*.{md,svx,svelte.md}`); | ||
|
||
let match; | ||
for (const [path, resolver] of Object.entries(modules)) { | ||
if (slugFromPath(path) === params.slug) { | ||
match = [path, resolver]; | ||
break; | ||
} | ||
} | ||
|
||
if (!match) { | ||
return { | ||
status: 404 | ||
}; | ||
} | ||
|
||
const post = await match[1](); | ||
|
||
return { | ||
body: post.metadata | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,24 @@ | ||
const slug = path => path.match(/([\w-]+)\.(svelte\.md|md|svx)/i)?.[1] ?? null; | ||
import { slugFromPath } from '$lib/util'; | ||
|
||
export async function getAllPosts(globArr) { | ||
const postPromises = []; | ||
/** @type {import('@sveltejs/kit').RequestHandler} */ | ||
export async function get() { | ||
const modules = import.meta.glob('./*.{md,svx,svelte.md}'); | ||
|
||
for (let [path, resolver] of Object.entries(globArr)) { | ||
const match = path.match(slugRegex); | ||
const postPromises = []; | ||
|
||
if (!match || !match[1]) continue; | ||
for (let [path, resolver] of Object.entries(modules)) { | ||
const slug = slugFromPath(path); | ||
const promise = resolver().then((post) => [slug, post.metadata]); | ||
|
||
const slug = match[1]; | ||
const promise = resolver().then((post) => [ | ||
slug, | ||
{ | ||
...post.metadata, | ||
date: new Date(post.metadata.date) | ||
} | ||
]); | ||
postPromises.push(promise); | ||
} | ||
|
||
return Promise.all(postPromises); | ||
} | ||
const posts = await Promise.all(postPromises); | ||
const publishedPosts = posts.filter(([slug, metadata]) => metadata.published); | ||
|
||
publishedPosts.sort(([_slugA, a], [_slugB, b]) => (new Date(a.date) > new Date(b.date) ? -1 : 1)); | ||
|
||
/** @type {import('@sveltejs/kit').RequestHandler} */ | ||
export async function get(req) { | ||
const files = import.meta.glob('./posts/*.{md,svx,svelte.md}'); | ||
|
||
|
||
|
||
console.log(req); | ||
|
||
} | ||
return { | ||
body: publishedPosts | ||
}; | ||
} |