Skip to content

Commit

Permalink
refactor files, fix up types, move over to jsr where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
thisisjofrank committed Nov 4, 2024
1 parent d251507 commit 26d2d91
Show file tree
Hide file tree
Showing 23 changed files with 504 additions and 93 deletions.
File renamed without changes
2 changes: 1 addition & 1 deletion testdata/my_blog.ts → content/main.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import blog from "../blog.tsx";
import blog from "../src/blog.tsx";

blog({
author: "Dino",
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
26 changes: 26 additions & 0 deletions deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"tasks": {
"dev": "deno run --allow-net --allow-read --allow-env=NODE_DEBUG,NO_COLOR,FORCE_COLOR,TERM --watch --no-check ./content/main.tsx --dev",
"test": "deno test --allow-net --allow-read --allow-env=NODE_DEBUG,NO_COLOR,FORCE_COLOR,TERM --no-check=remote",
"serve": "deno run --allow-net --allow-read --allow-env --no-check ./content/main.tsx"
},
"imports": {
"@deno/gfm": "jsr:@deno/gfm@^0.10.0",
"@std/testing": "jsr:@std/testing@^1.0.4",
"@std/assert": "jsr:@std/assert@^1.0.7",
"@std/async": "jsr:@std/async@^1.0.8",
"@std/front-matter": "jsr:@std/front-matter@^1.0.5",
"@std/fs": "jsr:@std/fs@^1.0.5",
"@std/http": "jsr:@std/http@^1.0.9",
"@std/path": "jsr:@std/path@^1.0.8",
"@denoland/htm": "https://deno.land/x/[email protected]/mod.ts",
"@denoland/htm/plugins/color-scheme": "https://deno.land/x/[email protected]/plugins/color-scheme.ts",
"@denoland/htm/plugins/unocss": "https://deno.land/x/[email protected]/plugins/unocss.ts",
"@denoland/g_a": "https://deno.land/x/[email protected]/mod.ts",
"@githubcontent/kt3k/callsites": "https://raw.githubusercontent.com/kt3k/callsites/v1.0.0/mod.ts",
"@esm/feed": "https://esm.sh/[email protected]",
"@esm/remove-markdown": "https://esm.sh/[email protected]",
"@esm/prism-c": "https://esm.sh/[email protected]/components/prism-c?no-check",
"@outdated/std-http": "https://deno.land/[email protected]/http/mod.ts"
}
}
7 changes: 0 additions & 7 deletions deno.jsonc

This file was deleted.

402 changes: 402 additions & 0 deletions deno.lock

Large diffs are not rendered by default.

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
20 changes: 20 additions & 0 deletions mod.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { assert } from "@std/assert/assert";
import * as module from "./mod.ts";

Deno.test("module exports expected dependencies", () => {
assert(module, "module is not defined");
assert(module.types, "module.types is not defined");
assert(module.blog, "module.blog is not defined");
assert(module.blog.configureBlog, "module.blog.configureBlog is not defined");
assert(
module.blog.createBlogHandler,
"module.blog.createBlogHandler is not defined",
);
assert(module.blog.default, "module.blog.default is not defined");
assert(module.components, "module.components is not defined");
assert(module.components.Index, "module.components.Index is not defined");
assert(
module.components.PostPage,
"module.components.PostPage is not defined",
);
});
5 changes: 5 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as types from "./types.d.ts";
import * as blog from "./src/blog.tsx";
import * as components from "./src/components.tsx";

export { blog, components, types };
20 changes: 9 additions & 11 deletions blog_test.ts → src/blog.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
// 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));
const BLOG_URL = new URL("../content/main.js", import.meta.url).href;
const TESTDATA_PATH = fromFileUrl(new URL("../testdata/", import.meta.url));
const CONTENT_PATH = fromFileUrl(new URL("../content/", import.meta.url));
const BLOG_SETTINGS = await configureBlog(BLOG_URL, false, {
author: "The author",
title: "Test blog",
Expand All @@ -25,6 +22,7 @@ const BLOG_SETTINGS = await configureBlog(BLOG_URL, false, {
],
readtime: true,
});

const CONN_INFO = {
localAddr: {
transport: "tcp" as const,
Expand Down Expand Up @@ -268,7 +266,7 @@ Deno.test("static files in posts/ directory", async () => {
const bytes = new Uint8Array(await resp.arrayBuffer());
assertEquals(
bytes,
await Deno.readFile(join(TESTDATA_PATH, "./posts/first/hello.png")),
await Deno.readFile(join(CONTENT_PATH, "./posts/first/hello.png")),
);
}
{
Expand All @@ -281,7 +279,7 @@ Deno.test("static files in posts/ directory", async () => {
const bytes = new Uint8Array(await resp.arrayBuffer());
assertEquals(
bytes,
await Deno.readFile(join(TESTDATA_PATH, "./posts/second/hello2.png")),
await Deno.readFile(join(CONTENT_PATH, "./posts/second/hello2.png")),
);
}
});
Expand All @@ -292,7 +290,7 @@ Deno.test("static files in root directory", async () => {
assertEquals(resp.status, 200);
assertEquals(resp.headers.get("content-type"), "image/png");
const bytes = new Uint8Array(await resp.arrayBuffer());
assertEquals(bytes, await Deno.readFile(join(TESTDATA_PATH, "./cat.png")));
assertEquals(bytes, await Deno.readFile(join(CONTENT_PATH, "./cat.png")));
});

Deno.test("RSS feed", async () => {
Expand Down
58 changes: 30 additions & 28 deletions blog.tsx → src/blog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,37 @@
/// <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 { serveDir } from "@std/http/file-server";
import { walk } from "@std/fs";
import { dirname, fromFileUrl, join, relative } from "@std/path";
import { type ConnInfo, serve } from "@outdated/std-http";
import { extract as frontMatter } from "@std/front-matter/any";

import * as gfm from "@deno/gfm";
import { Fragment, h } from "@denoland/htm";
import { default as html, type HtmlOptions } from "@denoland/htm";

import { createReporter } from "@denoland/g_a";
import { default as callsites } from "@githubcontent/kt3k/callsites";
import { Feed, type Item as FeedItem } from "@esm/feed";
import { default as removeMarkdown } from "@esm/remove-markdown";

import UnoCSS from "@denoland/htm/plugins/unocss";
import ColorScheme from "@denoland/htm/plugins/color-scheme";

import { pooledMap } from "@std/async";
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";
} from "../types.d.ts";
import { WalkEntry } from "@std/fs";

// Add syntax highlighting support for C by default
import "@esm/prism-c";

export { Fragment, h };

Expand Down Expand Up @@ -231,7 +230,8 @@ async function watchForChanges(postsDirectory: string) {
HMR_SOCKETS.forEach((socket) => {
socket.send("refresh");
});
} catch (err) {
// deno-lint-ignore no-explicit-any
} catch (err: any) {
console.error(`loadPost ${path} error:`, err.message);
}
}
Expand Down Expand Up @@ -433,7 +433,8 @@ export async function handler(
try {
await Deno.lstat(join(blogState.directory, "./posts", pathname));
fsRoot = join(blogState.directory, "./posts");
} catch (e) {
// deno-lint-ignore no-explicit-any
} catch (e: any) {
if (!(e instanceof Deno.errors.NotFound)) {
console.error(e);
return new Response(e.message, { status: 500 });
Expand Down Expand Up @@ -551,7 +552,8 @@ export function redirects(redirectMap: Record<string, string>): BlogMiddleware {
}
try {
return await ctx.next();
} catch (e) {
// deno-lint-ignore no-explicit-any
} catch (e: any) {
console.error(e);
return new Response(`Internal server error: ${e.message}`, {
status: 500,
Expand Down
5 changes: 3 additions & 2 deletions components.tsx → src/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
/// <reference lib="dom.asynciterable" />
/// <reference lib="deno.ns" />

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

const socialAppIcons = new Map([
["github.com", IconGithub],
Expand Down
8 changes: 7 additions & 1 deletion types.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
// Copyright 2022 the Deno authors. All rights reserved. MIT license.
import UnoCSS from "@denoland/htm/plugins/unocss";
import { VNode } from "@denoland/htm";
import type { ConnInfo } from "@outdated/std-http";

import type { ConnInfo, UnoConfig, VNode } from "./deps.ts";
type UnoConfig = typeof UnoCSS extends (
arg: infer P | undefined,
) => unknown ? P
: never;

export interface BlogContext {
state: BlogState;
Expand Down

0 comments on commit 26d2d91

Please sign in to comment.