Skip to content

Commit 571a875

Browse files
j-tagrojvv
andauthored
feat: Add custom root directory (#134) (#135)
Co-authored-by: Roj <[email protected]>
1 parent 66977aa commit 571a875

File tree

5 files changed

+51
-0
lines changed

5 files changed

+51
-0
lines changed

blog.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,9 @@ export async function configureBlog(
180180
throw new Error("Cannot run blog from a remote URL.");
181181
}
182182

183+
// Override blog directory, if `rootDirectory` is provided
184+
directory = settings?.rootDirectory ?? directory;
185+
183186
const state: BlogState = {
184187
directory,
185188
...settings,

blog_test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,3 +364,40 @@ Deno.test("Plaintext response", async () => {
364364
const body = await resp.text();
365365
assert(body.startsWith("It was popularised in the 1960s"));
366366
});
367+
368+
Deno.test(
369+
"custom root directory",
370+
async () => {
371+
const blogState = await configureBlog(BLOG_URL, false, {
372+
author: "The author",
373+
title: "Test blog",
374+
description: "This is some description.",
375+
lang: "en-GB",
376+
rootDirectory: join(TESTDATA_PATH, "./customRootDir"),
377+
});
378+
const customRootDirectoryBlogHandler = createBlogHandler(blogState);
379+
const customRootDirectoryTestHandler = (req: Request) => {
380+
return customRootDirectoryBlogHandler(req, CONN_INFO);
381+
};
382+
const resp = await customRootDirectoryTestHandler(
383+
new Request("https://blog.deno.dev/custom"),
384+
);
385+
assert(resp);
386+
assertEquals(resp.status, 200);
387+
assertEquals(resp.headers.get("content-type"), "text/html; charset=utf-8");
388+
const body = await resp.text();
389+
assertStringIncludes(body, `Custom post`);
390+
const respStaticFile = await customRootDirectoryTestHandler(
391+
new Request("https://blog.deno.dev/cat_custom_path.png"),
392+
);
393+
assertEquals(respStaticFile.status, 200);
394+
assertEquals(respStaticFile.headers.get("content-type"), "image/png");
395+
const bytes = new Uint8Array(await respStaticFile.arrayBuffer());
396+
assertEquals(
397+
bytes,
398+
await Deno.readFile(
399+
join(TESTDATA_PATH, "./customRootDir/cat_custom_path.png"),
400+
),
401+
);
402+
},
403+
);
225 KB
Loading
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
title: Custom post
3+
publish_date: 2022-03-20
4+
abstract: This is the first post with a custom root directory.
5+
---
6+
7+
It was popularised in the 1960s with the release of Letraset sheets containing
8+
Lorem Ipsum passages, and more recently with desktop publishing software like
9+
Aldus PageMaker including versions of Lorem Ipsum.

types.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ export interface BlogSettings {
7878
hostname?: string;
7979
/** Whether to display readtime or not */
8080
readtime?: boolean;
81+
/** The root directory of the blog contents */
82+
rootDirectory?: string;
8183
}
8284

8385
export interface BlogState extends BlogSettings {

0 commit comments

Comments
 (0)