Skip to content

Commit

Permalink
refactor: reduce dependencies (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuapp authored Nov 30, 2024
1 parent d2fab8e commit f32e296
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 105 deletions.
7 changes: 2 additions & 5 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@
},
"imports": {
"@astral/astral": "jsr:@astral/astral@^0.4.5",
"@hono/hono": "jsr:@hono/hono@^4.5.5",
"@std/assert": "jsr:@std/assert@^1.0.2",
"@std/cli": "jsr:@std/[email protected]",
"@std/fmt": "jsr:@std/fmt@^1.0.0",
"@std/fs": "jsr:@std/fs@^1.0.1",
"@std/http": "jsr:@std/http@^1.0.11",
"@std/cli": "jsr:@std/[email protected]",
"@std/fmt": "jsr:@std/fmt@^1",
"md4w": "npm:md4w@^0.2.6"
},
"exclude": [".gitattributes", ".github", "src/testdata", "**/*/*_test.ts"]
Expand Down
62 changes: 7 additions & 55 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 10 additions & 10 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
underline,
yellow,
} from "@std/fmt/colors";
import { exists } from "@std/fs/exists";
import { mdToPdf } from "./md-to-pdf.ts";
import { getFilename } from "./utils/filename.ts";
import type { MdToPdfOptions } from "./types.ts";
Expand All @@ -31,8 +30,10 @@ async function validateArgs(
css?: string;
},
): Promise<boolean> {
if (typeof args.css === "string") {
if (!(await exists(args.css, { isFile: true, isReadable: true }))) {
if (args.css) {
try {
await Deno.lstat(args.css);
} catch (_e) {
console.error(
`${brightRed("error")}: Set CSS file is not found: ${args.css}`,
);
Expand Down Expand Up @@ -97,14 +98,13 @@ const paths: Array<string> = [];
if (args._) {
for await (const path of args._) {
if (typeof path !== "string") continue;
if (await exists(path, { isFile: true })) {
try {
await Deno.lstat(path);
paths.push(path);
} else {
if (await exists(path)) {
console.error("md2pdf: " + path + ": Is not a file");
} else {
console.error("md2pdf: " + path + ": Not found");
}
} catch (e) {
console.error(
`${brightRed("error")}: ${e}`,
);
}
}
}
Expand Down
81 changes: 46 additions & 35 deletions src/utils/server.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { Hono } from "@hono/hono/tiny";
import { LinearRouter } from "@hono/hono/router/linear-router";
import { init as initMd4w, mdToHtml } from "md4w";
import type { MdToPdfOptions } from "../types.ts";
import { serveFile } from "@std/http/file-server";
import { getFilename } from "./filename.ts";

export const DEFAULT_PORT = 33433;
Expand All @@ -18,38 +15,52 @@ export function launchHttpServer(
path: string,
options?: MdToPdfOptions,
): Deno.HttpServer<Deno.NetAddr> {
const decoder = new TextDecoder("utf-8");
const app = new Hono({ router: new LinearRouter() });
app.get("/", async (c) => {
const css = options?.css
? await decoder.decode(await Deno.readFile(options?.css))
: "";
const content = mdToHtml(
decoder.decode(await Deno.readFile(path)),
);
const title = getFilename(path.split("/").at(-1) || "") || "Untitled";
return c.html(
`<html>
<head>
<title>${title}</title>
<style>${css}</style>
</head>
<body>
${content}
</body>
</html>`,
);
});
app.get("*", async (c) => {
const filePath = "." + decodeURI(c.req.path);
try {
const fileInfo = await Deno.lstat(filePath);
if (fileInfo.isFile) {
return serveFile(c.req.raw, filePath);
const handler: Deno.ServeHandler = async (req) => {
const url = new URL(req.url);

if (url.pathname === "/") {
const decoder = new TextDecoder("utf-8");
const css = options?.css
? decoder.decode(await Deno.readFile(options?.css))
: "";
const content = mdToHtml(
decoder.decode(await Deno.readFile(path)),
);
const title = getFilename(path.split("/").at(-1) || "") || "Untitled";
return new Response(
`<html>
<head>
<title>${title}</title>
<style>${css}</style>
</head>
<body>
${content}
</body>
</html>`,
{ headers: { "Content-Type": "text/html" } },
);
} else {
const filePath = "." + decodeURI(url.pathname);
try {
const fileInfo = await Deno.lstat(filePath);
if (fileInfo.isFile) {
const file = await Deno.open(filePath);
return new Response(file.readable);
}
} catch (_e) {
return notFound();
}
} catch (_e) {
return c.notFound();
}
});
return Deno.serve({ onListen: () => "", port: DEFAULT_PORT }, app.fetch);
return notFound();
};

return Deno.serve({ onListen: () => "", port: DEFAULT_PORT }, handler);
}

/**
* A response for 404 Not Found.
* @internal
*/
function notFound(): Response {
return new Response("Not Found", { status: 404 });
}

0 comments on commit f32e296

Please sign in to comment.