From 683e74640a1c545492830e3b9810cf48b924b6e2 Mon Sep 17 00:00:00 2001 From: Reed von Redwitz Date: Tue, 15 Aug 2023 15:30:44 +0200 Subject: [PATCH] feat: ability to have a user defined index, with blogindex available at /blog --- src/plugin/blog.ts | 3 +- tests/separate_index_fixture/deno.json | 30 +++++++++++ tests/separate_index_fixture/dev.ts | 5 ++ tests/separate_index_fixture/fresh.gen.ts | 15 ++++++ tests/separate_index_fixture/local.config.ts | 12 +++++ tests/separate_index_fixture/main.ts | 20 ++++++++ tests/separate_index_fixture/options.ts | 11 ++++ .../posts/boring-post.md | 11 ++++ tests/separate_index_fixture/routes/index.tsx | 12 +++++ tests/separate_index_fixture/twind.config.ts | 5 ++ tests/separate_index_test.ts | 50 +++++++++++++++++++ 11 files changed, 173 insertions(+), 1 deletion(-) create mode 100644 tests/separate_index_fixture/deno.json create mode 100755 tests/separate_index_fixture/dev.ts create mode 100644 tests/separate_index_fixture/fresh.gen.ts create mode 100644 tests/separate_index_fixture/local.config.ts create mode 100644 tests/separate_index_fixture/main.ts create mode 100644 tests/separate_index_fixture/options.ts create mode 100644 tests/separate_index_fixture/posts/boring-post.md create mode 100644 tests/separate_index_fixture/routes/index.tsx create mode 100644 tests/separate_index_fixture/twind.config.ts create mode 100644 tests/separate_index_test.ts diff --git a/src/plugin/blog.ts b/src/plugin/blog.ts index d562091..72e1b65 100644 --- a/src/plugin/blog.ts +++ b/src/plugin/blog.ts @@ -18,6 +18,7 @@ interface BlogOptions { rootPath: string; postsPerPage?: number; sources?: Source[]; + useSeparateIndex?: boolean; } export type Source = "local" | "notion"; @@ -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), }, { diff --git a/tests/separate_index_fixture/deno.json b/tests/separate_index_fixture/deno.json new file mode 100644 index 0000000..c934957 --- /dev/null +++ b/tests/separate_index_fixture/deno.json @@ -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" + ] + } + } +} diff --git a/tests/separate_index_fixture/dev.ts b/tests/separate_index_fixture/dev.ts new file mode 100755 index 0000000..2d85d6c --- /dev/null +++ b/tests/separate_index_fixture/dev.ts @@ -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"); diff --git a/tests/separate_index_fixture/fresh.gen.ts b/tests/separate_index_fixture/fresh.gen.ts new file mode 100644 index 0000000..799a93f --- /dev/null +++ b/tests/separate_index_fixture/fresh.gen.ts @@ -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; diff --git a/tests/separate_index_fixture/local.config.ts b/tests/separate_index_fixture/local.config.ts new file mode 100644 index 0000000..e472683 --- /dev/null +++ b/tests/separate_index_fixture/local.config.ts @@ -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; diff --git a/tests/separate_index_fixture/main.ts b/tests/separate_index_fixture/main.ts new file mode 100644 index 0000000..5ff1531 --- /dev/null +++ b/tests/separate_index_fixture/main.ts @@ -0,0 +1,20 @@ +/// +/// +/// +/// +/// + +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)], +}); diff --git a/tests/separate_index_fixture/options.ts b/tests/separate_index_fixture/options.ts new file mode 100644 index 0000000..8d1b398 --- /dev/null +++ b/tests/separate_index_fixture/options.ts @@ -0,0 +1,11 @@ +import { FreshOptions } from "$fresh/server.ts"; + +export default { + async render(_ctx, render) { + await new Promise((r) => r()); + const body = render(); + if (typeof body !== "string") { + throw new Error("body is missing"); + } + }, +} as FreshOptions; diff --git a/tests/separate_index_fixture/posts/boring-post.md b/tests/separate_index_fixture/posts/boring-post.md new file mode 100644 index 0000000..2b66b9d --- /dev/null +++ b/tests/separate_index_fixture/posts/boring-post.md @@ -0,0 +1,11 @@ +--- +title: "Single Tag" +date: 2023-8-15 +author: Some Author +--- + +The blog isn't the primary purpose of this site. + + + +Some extra content, just in case. diff --git a/tests/separate_index_fixture/routes/index.tsx b/tests/separate_index_fixture/routes/index.tsx new file mode 100644 index 0000000..e93050d --- /dev/null +++ b/tests/separate_index_fixture/routes/index.tsx @@ -0,0 +1,12 @@ +import { PageProps, RouteContext } from "$fresh/server.ts"; + +export default function Home(_req: Request, _ctx: RouteContext) { + return ( +
+

+ Welcome to the site! The blog isn't the main purpose here, so we're + using the
useSeparateIndex
option. +

+
+ ); +} diff --git a/tests/separate_index_fixture/twind.config.ts b/tests/separate_index_fixture/twind.config.ts new file mode 100644 index 0000000..2a7ac27 --- /dev/null +++ b/tests/separate_index_fixture/twind.config.ts @@ -0,0 +1,5 @@ +import { Options } from "$fresh/plugins/twind.ts"; + +export default { + selfURL: import.meta.url, +} as Options; diff --git a/tests/separate_index_test.ts b/tests/separate_index_test.ts new file mode 100644 index 0000000..2766466 --- /dev/null +++ b/tests/separate_index_test.ts @@ -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.", + ); +});