Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ jobs:
uses: actions/checkout@v2

- name: Install Deno
uses: denoland/setup-deno@v1
uses: denoland/setup-deno@v2
with:
deno-version: v2.x

- name: Check formatting
run: deno fmt --check
Expand Down
57 changes: 21 additions & 36 deletions blog.tsx
Original file line number Diff line number Diff line change
@@ -1,43 +1,28 @@
// Copyright 2022 the Deno authors. All rights reserved. MIT license.

/** @jsx h */
/// <reference no-default-lib="true"/>
/// <reference lib="dom" />
/// <reference lib="dom.asynciterable" />
/// <reference lib="deno.ns" />

import {
callsites,
ColorScheme,
createReporter,
dirname,
Feed,
Fragment,
fromFileUrl,
frontMatter,
gfm,
h,
html,
HtmlOptions,
join,
relative,
removeMarkdown,
serve,
serveDir,
UnoCSS,
walk,
} from "./deps.ts";
import { pooledMap } from "https://deno.land/[email protected]/async/pool.ts";

import * as gfm from "@deno/gfm";
import { pooledMap } from "@std/async";
import { extractYaml as frontMatter } from "@std/front-matter";
import { walk, WalkEntry } from "@std/fs";
import { serveDir } from "@std/http";
import { dirname, fromFileUrl, join, relative } from "@std/path";
import callsites from "callsites";
import { Feed, type Item as FeedItem } from "feed";
import { createReporter } from "ga";
import html, { Fragment, h, HtmlOptions } from "htm/html.tsx";
import ColorScheme from "htm/plugins/color-scheme.ts";
import UnoCSS from "htm/plugins/unocss.ts";
import removeMarkdown from "remove-markdown";
import { Index, PostPage } from "./components.tsx";
import type { ConnInfo, FeedItem } from "./deps.ts";
import type {
BlogContext,
BlogMiddleware,
BlogSettings,
BlogState,
Post,
} from "./types.d.ts";
import { WalkEntry } from "https://deno.land/[email protected]/fs/walk.ts";

export { Fragment, h };

Expand Down Expand Up @@ -110,17 +95,17 @@ export default async function blog(settings?: BlogSettings) {
const blogState = await configureBlog(url, IS_DEV, settings);

const blogHandler = createBlogHandler(blogState);
serve(blogHandler, {
Deno.serve({
port: blogState.port,
hostname: blogState.hostname,
onError: errorHandler,
});
}, blogHandler);
}

export function createBlogHandler(state: BlogState) {
const inner = handler;
const withMiddlewares = composeMiddlewares(state);
return function handler(req: Request, connInfo: ConnInfo) {
return function handler(req: Request, connInfo: Deno.ServeHandlerInfo) {
// Redirect requests that end with a trailing slash
// to their non-trailing slash counterpart.
// Ex: /about/ -> /about
Expand All @@ -136,7 +121,7 @@ export function createBlogHandler(state: BlogState) {
function composeMiddlewares(state: BlogState) {
return (
req: Request,
connInfo: ConnInfo,
connInfo: Deno.ServeHandlerInfo,
inner: (req: Request, ctx: BlogContext) => Promise<Response>,
) => {
const mws = state.middlewares?.slice().reverse();
Expand Down Expand Up @@ -232,7 +217,7 @@ async function watchForChanges(postsDirectory: string) {
socket.send("refresh");
});
} catch (err) {
console.error(`loadPost ${path} error:`, err.message);
console.error(`loadPost ${path} error:`, (err as Error).message);
}
}
}
Expand Down Expand Up @@ -436,7 +421,7 @@ export async function handler(
} catch (e) {
if (!(e instanceof Deno.errors.NotFound)) {
console.error(e);
return new Response(e.message, { status: 500 });
return new Response((e as Error).message, { status: 500 });
}
}

Expand Down Expand Up @@ -553,7 +538,7 @@ export function redirects(redirectMap: Record<string, string>): BlogMiddleware {
return await ctx.next();
} catch (e) {
console.error(e);
return new Response(`Internal server error: ${e.message}`, {
return new Response(`Internal server error: ${(e as Error).message}`, {
status: 500,
});
}
Expand Down
18 changes: 5 additions & 13 deletions blog_test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
// Copyright 2022 the Deno authors. All rights reserved. MIT license.

import { configureBlog, createBlogHandler, redirects } from "./blog.tsx";
import {
assert,
assertEquals,
assertStringIncludes,
} from "https://deno.land/[email protected]/testing/asserts.ts";
import { fromFileUrl, join } from "https://deno.land/[email protected]/path/mod.ts";
import { assert, assertEquals, assertStringIncludes } from "@std/assert";
import { fromFileUrl, join } from "@std/path";

const BLOG_URL = new URL("./testdata/main.js", import.meta.url).href;
const TESTDATA_PATH = fromFileUrl(new URL("./testdata/", import.meta.url));
Expand All @@ -26,17 +22,13 @@ const BLOG_SETTINGS = await configureBlog(BLOG_URL, false, {
readtime: true,
});
const CONN_INFO = {
localAddr: {
transport: "tcp" as const,
hostname: "0.0.0.0",
port: 8000,
},
remoteAddr: {
transport: "tcp" as const,
hostname: "0.0.0.0",
port: 8001,
port: 8000,
},
};
completed: Promise.resolve(),
} satisfies Deno.ServeHandlerInfo;

const blogHandler = createBlogHandler(BLOG_SETTINGS);
const testHandler = (req: Request): Response | Promise<Response> => {
Expand Down
8 changes: 2 additions & 6 deletions components.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
// Copyright 2022 the Deno authors. All rights reserved. MIT license.

/** @jsx h */
/** @jsxFrag Fragment */
/// <reference no-default-lib="true"/>
/// <reference lib="dom" />
/// <reference lib="dom.asynciterable" />
/// <reference lib="deno.ns" />

import { gfm, h } from "./deps.ts";
import * as gfm from "@deno/gfm";
import { h } from "htm/html.tsx";
import type { BlogState, DateFormat, Post } from "./types.d.ts";

const socialAppIcons = new Map([
Expand Down
16 changes: 15 additions & 1 deletion deno.jsonc
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
{
"lock": false,
"tasks": {
"demo": "deno run --allow-net --allow-read --allow-env=NODE_DEBUG --watch --no-check testdata/my_blog.ts --dev",
"demo": "deno run --allow-net --allow-read --allow-env --watch --no-check testdata/my_blog.ts --dev",
"test": "deno test --no-check=remote --allow-read"
},
"imports": {
"@deno/gfm": "jsr:@deno/gfm@^0.10.0",
"@std/assert": "jsr:@std/assert@^1.0.11",
"@std/async": "jsr:@std/async@^1.0.10",
"@std/front-matter": "jsr:@std/front-matter@^1.0.5",
"@std/fs": "jsr:@std/fs@^1.0.11",
"@std/http": "jsr:@std/http@^1.0.13",
"@std/path": "jsr:@std/path@^1.0.8",
"callsites": "npm:callsites@^4.2.0",
"feed": "npm:feed@^4.2.2",
"ga": "https://deno.land/x/[email protected]/mod.ts",
"htm/": "https://deno.land/x/[email protected]/",
"remove-markdown": "npm:remove-markdown@^0.6.0"
}
}
42 changes: 0 additions & 42 deletions deps.ts

This file was deleted.

2 changes: 1 addition & 1 deletion init.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2022 the Deno authors. All rights reserved. MIT license.

import { join, resolve } from "https://deno.land/[email protected]/path/mod.ts";
import { join, resolve } from "@std/path";

const HELP = `deno_blog

Expand Down
10 changes: 8 additions & 2 deletions types.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Copyright 2022 the Deno authors. All rights reserved. MIT license.

import type { ConnInfo, UnoConfig, VNode } from "./deps.ts";
import { type VNode } from "htm/html.tsx";
import UnoCSS from "htm/plugins/unocss.ts";

export interface BlogContext {
state: BlogState;
connInfo: ConnInfo;
connInfo: Deno.ServeHandlerInfo;
next: () => Promise<Response>;
}

Expand Down Expand Up @@ -103,3 +104,8 @@ export interface Post {
readTime: number;
renderMath?: boolean;
}

export type UnoConfig = typeof UnoCSS extends (
arg: infer P | undefined,
) => unknown ? P
: never;