Skip to content

Commit

Permalink
feat: Add custom root directory (denoland#134) (denoland#135)
Browse files Browse the repository at this point in the history
Co-authored-by: Roj <[email protected]>
  • Loading branch information
j-tag and rojvv authored Dec 23, 2023
1 parent 66977aa commit 571a875
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 0 deletions.
3 changes: 3 additions & 0 deletions blog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ export async function configureBlog(
throw new Error("Cannot run blog from a remote URL.");
}

// Override blog directory, if `rootDirectory` is provided
directory = settings?.rootDirectory ?? directory;

const state: BlogState = {
directory,
...settings,
Expand Down
37 changes: 37 additions & 0 deletions blog_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,3 +364,40 @@ Deno.test("Plaintext response", async () => {
const body = await resp.text();
assert(body.startsWith("It was popularised in the 1960s"));
});

Deno.test(
"custom root directory",
async () => {
const blogState = await configureBlog(BLOG_URL, false, {
author: "The author",
title: "Test blog",
description: "This is some description.",
lang: "en-GB",
rootDirectory: join(TESTDATA_PATH, "./customRootDir"),
});
const customRootDirectoryBlogHandler = createBlogHandler(blogState);
const customRootDirectoryTestHandler = (req: Request) => {
return customRootDirectoryBlogHandler(req, CONN_INFO);
};
const resp = await customRootDirectoryTestHandler(
new Request("https://blog.deno.dev/custom"),
);
assert(resp);
assertEquals(resp.status, 200);
assertEquals(resp.headers.get("content-type"), "text/html; charset=utf-8");
const body = await resp.text();
assertStringIncludes(body, `Custom post`);
const respStaticFile = await customRootDirectoryTestHandler(
new Request("https://blog.deno.dev/cat_custom_path.png"),
);
assertEquals(respStaticFile.status, 200);
assertEquals(respStaticFile.headers.get("content-type"), "image/png");
const bytes = new Uint8Array(await respStaticFile.arrayBuffer());
assertEquals(
bytes,
await Deno.readFile(
join(TESTDATA_PATH, "./customRootDir/cat_custom_path.png"),
),
);
},
);
Binary file added testdata/customRootDir/cat_custom_path.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions testdata/customRootDir/posts/custom.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
title: Custom post
publish_date: 2022-03-20
abstract: This is the first post with a custom root directory.
---

It was popularised in the 1960s with the release of Letraset sheets containing
Lorem Ipsum passages, and more recently with desktop publishing software like
Aldus PageMaker including versions of Lorem Ipsum.
2 changes: 2 additions & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ export interface BlogSettings {
hostname?: string;
/** Whether to display readtime or not */
readtime?: boolean;
/** The root directory of the blog contents */
rootDirectory?: string;
}

export interface BlogState extends BlogSettings {
Expand Down

0 comments on commit 571a875

Please sign in to comment.