Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: pluginify blog #660

Merged
merged 14 commits into from
Mar 3, 2024
6 changes: 2 additions & 4 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
"compilerOptions": { "jsx": "react-jsx", "jsxImportSource": "preact" },
"imports": {
"@/": "./",
"$fresh/": "https://deno.land/x/[email protected]/",
"$gfm": "https://deno.land/x/[email protected]/mod.ts",
"$fresh/": "https://raw.githubusercontent.com/deer/fresh/2159_plugin_routes_tailwind/",
"preact": "https://esm.sh/[email protected]",
"preact/": "https://esm.sh/[email protected]/",
"preact-render-to-string": "https://esm.sh/*[email protected]",
Expand All @@ -30,9 +29,8 @@
"tailwindcss": "npm:[email protected]",
"tailwindcss/": "npm:/[email protected]/",
"tailwindcss/plugin": "npm:/[email protected]/plugin.js",
"std/": "https://deno.land/[email protected]/",
"$std/": "https://deno.land/[email protected]/",
"stripe": "npm:/[email protected]",
"feed": "npm:/[email protected]",
"kv_oauth/": "https://deno.land/x/[email protected]/",
"tabler_icons_tsx/": "https://deno.land/x/[email protected]/tsx/",
"fresh_charts/": "https://deno.land/x/[email protected]/"
Expand Down
6 changes: 3 additions & 3 deletions e2e_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import {
assertNotEquals,
assertObjectMatch,
assertStringIncludes,
} from "std/assert/mod.ts";
import { isRedirectStatus, STATUS_CODE } from "std/http/status.ts";
import { resolvesNext, returnsNext, stub } from "std/testing/mock.ts";
} from "$std/assert/mod.ts";
import { isRedirectStatus, STATUS_CODE } from "$std/http/status.ts";
import { resolvesNext, returnsNext, stub } from "$std/testing/mock.ts";
import Stripe from "stripe";
import options from "./fresh.config.ts";
import { _internals } from "./plugins/kv_oauth.ts";
Expand Down
2 changes: 2 additions & 0 deletions fresh.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import securityHeaders from "./plugins/security_headers.ts";
import welcomePlugin from "./plugins/welcome.ts";
import type { FreshConfig } from "$fresh/server.ts";
import { ga4Plugin } from "https://deno.land/x/[email protected]/mod.ts";
import { blog } from "./plugins/blog/mod.ts";

export default {
plugins: [
Expand All @@ -17,5 +18,6 @@ export default {
tailwind(),
errorHandling,
securityHeaders,
blog(),
],
} satisfies FreshConfig;
6 changes: 0 additions & 6 deletions fresh.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@ import * as $api_users_login_index from "./routes/api/users/[login]/index.ts";
import * as $api_users_login_items from "./routes/api/users/[login]/items.ts";
import * as $api_users_index from "./routes/api/users/index.ts";
import * as $api_vote from "./routes/api/vote.ts";
import * as $blog_slug_ from "./routes/blog/[slug].tsx";
import * as $blog_index from "./routes/blog/index.tsx";
import * as $dashboard_index from "./routes/dashboard/index.tsx";
import * as $dashboard_stats from "./routes/dashboard/stats.tsx";
import * as $dashboard_users from "./routes/dashboard/users.tsx";
import * as $feed from "./routes/feed.ts";
import * as $index from "./routes/index.tsx";
import * as $pricing from "./routes/pricing.tsx";
import * as $submit from "./routes/submit.tsx";
Expand All @@ -48,12 +45,9 @@ const manifest = {
"./routes/api/users/[login]/items.ts": $api_users_login_items,
"./routes/api/users/index.ts": $api_users_index,
"./routes/api/vote.ts": $api_vote,
"./routes/blog/[slug].tsx": $blog_slug_,
"./routes/blog/index.tsx": $blog_index,
"./routes/dashboard/index.tsx": $dashboard_index,
"./routes/dashboard/stats.tsx": $dashboard_stats,
"./routes/dashboard/users.tsx": $dashboard_users,
"./routes/feed.ts": $feed,
"./routes/index.tsx": $index,
"./routes/pricing.tsx": $pricing,
"./routes/submit.tsx": $submit,
Expand Down
2 changes: 1 addition & 1 deletion islands/ItemsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useEffect } from "preact/hooks";
import { type Item } from "@/utils/db.ts";
import IconInfo from "tabler_icons_tsx/info-circle.tsx";
import { fetchValues } from "@/utils/http.ts";
import { decodeTime } from "std/ulid/mod.ts";
import { decodeTime } from "$std/ulid/mod.ts";
import { timeAgo } from "@/utils/display.ts";
import GitHubAvatarImg from "@/components/GitHubAvatarImg.tsx";

Expand Down
File renamed without changes.
24 changes: 24 additions & 0 deletions plugins/blog/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2023-2024 the Deno authors. All rights reserved. MIT license.
import type { Plugin } from "$fresh/server.ts";
import BlogIndex from "./routes/blog/index.tsx";
import BlogSlug from "./routes/blog/[slug].tsx";
import Feed from "./routes/feed.ts";
import { toFileUrl } from "$std/path/to_file_url.ts";

export function blog(): Plugin {
return {
name: "blog",
routes: [{
path: "/blog",
component: BlogIndex,
}, {
path: "/blog/[slug]",
component: BlogSlug,
}, {
path: "/feed",
component: Feed,
}],
location: import.meta.url,
projectLocation: toFileUrl(Deno.cwd()).href,
Copy link
Contributor

@iuioiua iuioiua Jan 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a blocker, but I observed that Tailwind styling appeared to break when I used projectLocation: Deno.cwd(). It must have something to do with the omission of file:// at the start of the string.

};
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright 2023-2024 the Deno authors. All rights reserved. MIT license.
import { defineRoute } from "$fresh/server.ts";
import { CSS, render } from "$gfm";
import { getPost } from "@/utils/posts.ts";
import { CSS, render } from "https://deno.land/x/gfm@0.2.5/mod.ts";
import { getPost } from "../../utils/posts.ts";
import Head from "@/components/Head.tsx";
import Share from "@/components/Share.tsx";
import Share from "../../components/Share.tsx";

export default defineRoute(async (_req, ctx) => {
const post = await getPost(ctx.params.slug);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2023-2024 the Deno authors. All rights reserved. MIT license.
import { defineRoute } from "$fresh/server.ts";
import { getPosts, type Post } from "@/utils/posts.ts";
import { getPosts, type Post } from "../../utils/posts.ts";
import Head from "@/components/Head.tsx";

function PostCard(props: Post) {
Expand Down
4 changes: 2 additions & 2 deletions routes/feed.ts → plugins/blog/routes/feed.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2023-2024 the Deno authors. All rights reserved. MIT license.
import { Feed } from "feed";
import { getPosts } from "@/utils/posts.ts";
import { Feed } from "npm:feed@4.2.2";
import { getPosts } from "../utils/posts.ts";
import { SITE_NAME } from "@/utils/constants.ts";
import { defineRoute } from "$fresh/server.ts";

Expand Down
4 changes: 2 additions & 2 deletions utils/posts.ts → plugins/blog/utils/posts.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2023-2024 the Deno authors. All rights reserved. MIT license.
import { extract } from "std/front_matter/yaml.ts";
import { join } from "std/path/join.ts";
import { extract } from "$std/front_matter/yaml.ts";
import { join } from "$std/path/join.ts";

/**
* This code is based on the
Expand Down
2 changes: 1 addition & 1 deletion utils/posts_test.ts → plugins/blog/utils/posts_test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2023-2024 the Deno authors. All rights reserved. MIT license.
import { getPost, getPosts } from "./posts.ts";

import { assert, assertEquals } from "std/assert/mod.ts";
import { assert, assertEquals } from "$std/assert/mod.ts";

Deno.test("[blog] getPost()", async () => {
const post = await getPost("first-post");
Expand Down
2 changes: 1 addition & 1 deletion plugins/error_handling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import type { Plugin } from "$fresh/server.ts";
import type { State } from "@/plugins/session.ts";
import { BadRequestError, redirect, UnauthorizedError } from "@/utils/http.ts";
import { STATUS_CODE, STATUS_TEXT } from "std/http/status.ts";
import { STATUS_CODE, STATUS_TEXT } from "$std/http/status.ts";

/**
* Returns the HTTP status code corresponding to a given runtime error. By
Expand Down
2 changes: 1 addition & 1 deletion routes/api/stripe-webhooks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2023-2024 the Deno authors. All rights reserved. MIT license.
import { type Handlers } from "$fresh/server.ts";
import { STATUS_CODE } from "std/http/status.ts";
import { STATUS_CODE } from "$std/http/status.ts";
import { isStripeEnabled, stripe } from "@/utils/stripe.ts";
import Stripe from "stripe";
import { getUserByStripeCustomer, updateUser } from "@/utils/db.ts";
Expand Down
2 changes: 1 addition & 1 deletion routes/api/vote.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2023-2024 the Deno authors. All rights reserved. MIT license.
import { type Handlers } from "$fresh/server.ts";
import { STATUS_CODE } from "std/http/status.ts";
import { STATUS_CODE } from "$std/http/status.ts";
import type { SignedInState } from "@/plugins/session.ts";
import { createVote } from "@/utils/db.ts";
import { BadRequestError } from "@/utils/http.ts";
Expand Down
2 changes: 1 addition & 1 deletion routes/submit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
type SignedInState,
State,
} from "@/plugins/session.ts";
import { ulid } from "std/ulid/mod.ts";
import { ulid } from "$std/ulid/mod.ts";
import IconInfo from "tabler_icons_tsx/info-circle.tsx";

const SUBMIT_STYLES =
Expand Down
4 changes: 2 additions & 2 deletions tasks/check_license.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright 2023-2024 the Deno authors. All rights reserved. MIT license.
// Copied from std/_tools/check_license.ts

import { walk } from "std/fs/walk.ts";
import { globToRegExp } from "std/path/glob_to_regexp.ts";
import { walk } from "$std/fs/walk.ts";
import { globToRegExp } from "$std/path/glob_to_regexp.ts";

const EXTENSIONS = [".ts", ".tsx"];
const EXCLUDED_DIRS = [
Expand Down
2 changes: 1 addition & 1 deletion tasks/db_seed.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2023-2024 the Deno authors. All rights reserved. MIT license.
// Description: Seeds the kv db with Hacker News stories
import { createItem, createUser } from "@/utils/db.ts";
import { ulid } from "std/ulid/mod.ts";
import { ulid } from "$std/ulid/mod.ts";

// Reference: https://github.com/HackerNews/API
const API_BASE_URL = `https://hacker-news.firebaseio.com/v0`;
Expand Down
4 changes: 2 additions & 2 deletions utils/db.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2023-2024 the Deno authors. All rights reserved. MIT license.
import { ulid } from "std/ulid/mod.ts";
import { ulid } from "$std/ulid/mod.ts";

const DENO_KV_PATH_KEY = "DENO_KV_PATH";
let path = undefined;
Expand Down Expand Up @@ -59,7 +59,7 @@ export function randomItem(): Item {
* @example
* ```ts
* import { createItem } from "@/utils/db.ts";
* import { ulid } from "std/ulid/mod.ts";
* import { ulid } from "$std/ulid/mod.ts";
*
* await createItem({
* id: ulid(),
Expand Down
4 changes: 2 additions & 2 deletions utils/db_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2023-2024 the Deno authors. All rights reserved. MIT license.
import { assertEquals, assertRejects } from "std/assert/mod.ts";
import { ulid } from "std/ulid/mod.ts";
import { assertEquals, assertRejects } from "$std/assert/mod.ts";
import { ulid } from "$std/ulid/mod.ts";
import {
collectValues,
createItem,
Expand Down
4 changes: 2 additions & 2 deletions utils/display.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2023-2024 the Deno authors. All rights reserved. MIT license.
import { difference } from "std/datetime/difference.ts";
import { difference } from "$std/datetime/difference.ts";

/**
* Returns a pluralized string for the given amount and unit.
Expand All @@ -22,7 +22,7 @@ export function pluralize(amount: number, unit: string) {
* @example
* ```ts
* import { timeAgo } from "@/utils/display.ts";
* import { SECOND, MINUTE, HOUR } from "std/datetime/constants.ts";
* import { SECOND, MINUTE, HOUR } from "$std/datetime/constants.ts";
*
* timeAgo(new Date()); // Returns "just now"
* timeAgo(new Date(Date.now() - 3 * HOUR)); // Returns "3 hours ago"
Expand Down
4 changes: 2 additions & 2 deletions utils/display_test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2023-2024 the Deno authors. All rights reserved. MIT license.
import { formatCurrency, pluralize, timeAgo } from "./display.ts";
import { DAY, HOUR, MINUTE, SECOND } from "std/datetime/constants.ts";
import { assertEquals, assertThrows } from "std/assert/mod.ts";
import { DAY, HOUR, MINUTE, SECOND } from "$std/datetime/constants.ts";
import { assertEquals, assertThrows } from "$std/assert/mod.ts";

Deno.test("[display] pluralize()", () => {
assertEquals(pluralize(0, "item"), "0 items");
Expand Down
8 changes: 4 additions & 4 deletions utils/github_test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright 2023-2024 the Deno authors. All rights reserved. MIT license.
import { assertRejects } from "std/assert/assert_rejects.ts";
import { assertRejects } from "$std/assert/assert_rejects.ts";
import { getGitHubUser } from "./github.ts";
import { returnsNext, stub } from "std/testing/mock.ts";
import { assertEquals } from "std/assert/assert_equals.ts";
import { STATUS_CODE } from "std/http/status.ts";
import { returnsNext, stub } from "$std/testing/mock.ts";
import { assertEquals } from "$std/assert/assert_equals.ts";
import { STATUS_CODE } from "$std/http/status.ts";
import { BadRequestError } from "@/utils/http.ts";

Deno.test("[plugins] getGitHubUser()", async (test) => {
Expand Down
2 changes: 1 addition & 1 deletion utils/http.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2023-2024 the Deno authors. All rights reserved. MIT license.
import { RedirectStatus, STATUS_CODE } from "std/http/status.ts";
import { RedirectStatus, STATUS_CODE } from "$std/http/status.ts";

/**
* Returns a response that redirects the client to the given location (URL).
Expand Down
6 changes: 3 additions & 3 deletions utils/http_test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright 2023-2024 the Deno authors. All rights reserved. MIT license.
import { returnsNext, stub } from "std/testing/mock.ts";
import { returnsNext, stub } from "$std/testing/mock.ts";
import { fetchValues, getCursor, redirect } from "./http.ts";
import { assert, assertEquals, assertRejects } from "std/assert/mod.ts";
import { STATUS_CODE } from "std/http/status.ts";
import { assert, assertEquals, assertRejects } from "$std/assert/mod.ts";
import { STATUS_CODE } from "$std/http/status.ts";
import { Item, randomItem } from "@/utils/db.ts";

Deno.test("[http] redirect() defaults", () => {
Expand Down
2 changes: 1 addition & 1 deletion utils/stripe.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2023-2024 the Deno authors. All rights reserved. MIT license.
import Stripe from "stripe";
import { AssertionError } from "std/assert/assertion_error.ts";
import { AssertionError } from "$std/assert/assertion_error.ts";

const STRIPE_SECRET_KEY = Deno.env.get("STRIPE_SECRET_KEY");

Expand Down
2 changes: 1 addition & 1 deletion utils/stripe_test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2023-2024 the Deno authors. All rights reserved. MIT license.
import { AssertionError, assertThrows } from "std/assert/mod.ts";
import { AssertionError, assertThrows } from "$std/assert/mod.ts";
import { assertIsPrice } from "./stripe.ts";

Deno.test("[stripe] assertIsPrice()", () => {
Expand Down
Loading