Skip to content

Commit

Permalink
feat: ability to have a user defined index, with blogindex available …
Browse files Browse the repository at this point in the history
…at /blog
  • Loading branch information
deer committed Aug 15, 2023
1 parent 281b540 commit 683e746
Show file tree
Hide file tree
Showing 11 changed files with 173 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/plugin/blog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface BlogOptions {
rootPath: string;
postsPerPage?: number;
sources?: Source[];
useSeparateIndex?: boolean;
}

export type Source = "local" | "notion";
Expand Down Expand Up @@ -46,7 +47,7 @@ export function blogPlugin(
path: "/_app",
component: AppBuilder(options),
}, {
path: "/index",
path: options.useSeparateIndex ? "/blog" : "/index",
component: buildBlogIndexPage(postsPerPage),
handler: buildIndexHandler(postsPerPage),
}, {
Expand Down
30 changes: 30 additions & 0 deletions tests/separate_index_fixture/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"lock": false,
"tasks": {
"start": "deno run -A --watch=static/,routes/ dev.ts",
"update": "deno run -A -r https://fresh.deno.dev/update ."
},
"imports": {
"$fresh/": "https://deno.land/x/fresh@1.3.1/",
"preact": "https://esm.sh/preact@10.15.1",
"preact/": "https://esm.sh/preact@10.15.1/",
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.2.0",
"@preact/signals": "https://esm.sh/*@preact/signals@1.1.3",
"@preact/signals-core": "https://esm.sh/*@preact/signals-core@1.2.3",
"twind": "https://esm.sh/twind@0.16.19",
"twind/": "https://esm.sh/twind@0.16.19/",
"$std/": "https://deno.land/std@0.193.0/"
},
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "preact"
},
"lint": {
"rules": {
"tags": [
"fresh",
"recommended"
]
}
}
}
5 changes: 5 additions & 0 deletions tests/separate_index_fixture/dev.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env -S deno run -A --watch=static/,routes/

import dev from "$fresh/dev.ts";

await dev(import.meta.url, "./main.ts");
15 changes: 15 additions & 0 deletions tests/separate_index_fixture/fresh.gen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// DO NOT EDIT. This file is generated by fresh.
// This file SHOULD be checked into source version control.
// This file is automatically updated during development when running `dev.ts`.

import * as $0 from "./routes/index.tsx";

const manifest = {
routes: {
"./routes/index.tsx": $0,
},
islands: {},
baseUrl: import.meta.url,
};

export default manifest;
12 changes: 12 additions & 0 deletions tests/separate_index_fixture/local.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { BlogOptions } from "../../mod.ts";

export default {
title: "Demo Blog",
navbarItems: {
Blog: "/blog",
Archive: "/archive",
},
rootPath: import.meta.url,
postsPerPage: 5,
useSeparateIndex: true,
} as BlogOptions;
20 changes: 20 additions & 0 deletions tests/separate_index_fixture/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/// <reference no-default-lib="true" />
/// <reference lib="dom" />
/// <reference lib="dom.iterable" />
/// <reference lib="dom.asynciterable" />
/// <reference lib="deno.ns" />

import "$std/dotenv/load.ts";

import { start } from "$fresh/server.ts";
import manifest from "./fresh.gen.ts";

import twindPlugin from "$fresh/plugins/twind.ts";
import twindConfig from "./twind.config.ts";

import { blogPlugin } from "../../src/plugin/blog.ts";
import localConfig from "./local.config.ts";

await start(manifest, {
plugins: [twindPlugin(twindConfig), blogPlugin(localConfig)],
});
11 changes: 11 additions & 0 deletions tests/separate_index_fixture/options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { FreshOptions } from "$fresh/server.ts";

export default {
async render(_ctx, render) {
await new Promise<void>((r) => r());
const body = render();
if (typeof body !== "string") {
throw new Error("body is missing");
}
},
} as FreshOptions;
11 changes: 11 additions & 0 deletions tests/separate_index_fixture/posts/boring-post.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
title: "Single Tag"
date: 2023-8-15
author: Some Author
---

The blog isn't the primary purpose of this site.

<!--more-->

Some extra content, just in case.
12 changes: 12 additions & 0 deletions tests/separate_index_fixture/routes/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { PageProps, RouteContext } from "$fresh/server.ts";

export default function Home(_req: Request, _ctx: RouteContext) {
return (
<div>
<h1>
Welcome to the site! The blog isn't the main purpose here, so we're
using the <pre>useSeparateIndex</pre> option.
</h1>
</div>
);
}
5 changes: 5 additions & 0 deletions tests/separate_index_fixture/twind.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Options } from "$fresh/plugins/twind.ts";

export default {
selfURL: import.meta.url,
} as Options;
50 changes: 50 additions & 0 deletions tests/separate_index_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import blogConfig from "./separate_index_fixture/local.config.ts";
import { createHandler } from "../deps.ts";
import { assertEquals, assertStringIncludes, DOMParser } from "./test_deps.ts";
import manifest from "./separate_index_fixture/fresh.gen.ts";
import { blogPlugin } from "../src/plugin/blog.ts";

Deno.test("index page isn't blog", async () => {
//@ts-ignore this will be fixed in fresh 1.4
const handler = await createHandler(manifest, {
plugins: [blogPlugin(blogConfig)],
});
const resp = await handler(
new Request("http://127.0.0.1/"),
);
const body = await resp.text();
assertStringIncludes(
body,
"Welcome to the site! The blog isn't the main purpose here",
);
});

Deno.test("blog page is blog", async () => {
//@ts-ignore this will be fixed in fresh 1.4
const handler = await createHandler(manifest, {
plugins: [blogPlugin(blogConfig)],
});
const resp = await handler(
new Request("http://127.0.0.1/blog"),
);
const body = await resp.text();
const doc = new DOMParser().parseFromString(body, "text/html")!;
const postElements = Array.from(doc.querySelectorAll('div[id^="post:"]'));

assertEquals(postElements.length, 1);
});

Deno.test("blog can view posts", async () => {
//@ts-ignore this will be fixed in fresh 1.4
const handler = await createHandler(manifest, {
plugins: [blogPlugin(blogConfig)],
});
const resp = await handler(
new Request("http://127.0.0.1/blog/boring-post"),
);
const body = await resp.text();
assertStringIncludes(
body,
"Some extra content, just in case.",
);
});

0 comments on commit 683e746

Please sign in to comment.