Skip to content

Commit

Permalink
BREAKING: use --stylesheet instead of --css (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuapp authored Dec 1, 2024
1 parent b8766cc commit a172a86
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Usage: md2pdf [OPTION]... [FILE]...
Options:
-w, --watch Watch for file changes.
-h, --help Print help.
--css Set CSS file used for rendering.
--stylesheet Set CSS file path used for rendering.
```

## Front matter (Experimental)
Expand Down
4 changes: 2 additions & 2 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
"tasks": {
"cli": "deno run --allow-read --allow-write --allow-env --allow-net=0.0.0.0,127.0.0.1 --allow-run ./src/cli.ts --",
"dev": "deno run --allow-read --allow-write --allow-env --allow-net=0.0.0.0,127.0.0.1 --allow-run ./src/cli.ts -- README.md",
"dev:all": "deno run --allow-read --allow-write --allow-env --allow-net=0.0.0.0,127.0.0.1 --allow-run ./src/cli.ts -w --css=src/testdata/bluecode.css -- README.md",
"dev:all": "deno run --allow-read --allow-write --allow-env --allow-net=0.0.0.0,127.0.0.1 --allow-run ./src/cli.ts -w --stylesheet=src/testdata/bluecode.css -- README.md",
"dev:help": "deno run --allow-read --allow-write --allow-env --allow-net=0.0.0.0,127.0.0.1 --allow-run ./src/cli.ts --help",
"dev:css": "deno run --allow-read --allow-write --allow-env --allow-net=0.0.0.0,127.0.0.1 --allow-run ./src/cli.ts -- ./src/testdata/bluecode.md",
"dev:invalidcss": "deno run --allow-read --allow-write --allow-env --allow-net=0.0.0.0,127.0.0.1 --allow-run ./src/cli.ts --css=src/testdata/redcode.css -- README.md",
"dev:invalidcss": "deno run --allow-read --allow-write --allow-env --allow-net=0.0.0.0,127.0.0.1 --allow-run ./src/cli.ts --stylesheet=src/testdata/redcode.css -- README.md",
"dev:watch": "deno run --allow-read --allow-write --allow-env --allow-net=0.0.0.0,127.0.0.1 --allow-run ./src/cli.ts --watch -- README.md"
},
"imports": {
Expand Down
26 changes: 3 additions & 23 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,6 @@ import { mdToPdf } from "./md-to-pdf.ts";
import { getFilename } from "./utils/filename.ts";
import type { MdToPdfOptions } from "./types.ts";

async function validateArgs(
args: {
css?: string;
},
): Promise<boolean> {
if (args.css) {
try {
await Deno.lstat(args.css);
} catch (_e) {
console.error(
`${brightRed("error")}: Set CSS file is not found: ${args.css}`,
);
return false;
}
}
return true;
}

function printHelp(): void {
const help = `md2pdf: ${
green("A simple CLI tool for converting markdown to PDF.")
Expand All @@ -53,7 +35,7 @@ ${gray("Usage:")} ${green("md2pdf [OPTION]... [FILE]...")}
${yellow("Options:")}
${green("-w, --watch")} Watch for file changes.
${green("-h, --help")} Print help.
${green("--css")} Set CSS file used for rendering.`;
${green("--stylesheet")} Set CSS file path used for rendering.`;
console.log(help);
}

Expand All @@ -78,12 +60,10 @@ async function generatePdfFromMarkdown(path: string, options?: MdToPdfOptions) {

const args = await parseArgs(Deno.args, {
boolean: ["w", "watch", "h", "help"],
string: ["css"],
string: ["stylesheet"],
});

if (!(await validateArgs(args))) {
Deno.exit(1);
} else if (args.h || args.help) {
if (args.h || args.help) {
printHelp();
Deno.exit(0);
}
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export type MdToPdfOptions = {
css?: string;
stylesheet?: string;
};
2 changes: 1 addition & 1 deletion src/utils/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function launchHttpServer(
} catch (_e) {
markdown = fileContent;
}
stylesheet = options?.css ? options?.css : stylesheet;
stylesheet = options?.stylesheet ?? stylesheet;

const content = mdToHtml(markdown);
const title = getFilename(path.split("/").at(-1) || "") || "Untitled";
Expand Down

0 comments on commit a172a86

Please sign in to comment.