Skip to content

Commit

Permalink
feat: experimental front matter support (#7)
Browse files Browse the repository at this point in the history
* feat: experimental front matter support

* docs: about front matter

* chore: rename keyword

* fix: change to load stylesheet
  • Loading branch information
ryuapp authored Dec 1, 2024
1 parent 29cbdfc commit b8766cc
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
README.pdf
*.pdf
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ Options:
--css Set CSS file used for rendering.
```

## Front matter (Experimental)

We can specify CSS file used in the front matter of markdown.

```md
---
stylesheet: ./github.css
---

# Hello World
```

## Related

- [md-to-pdf](https://github.com/simonhaenisch/md-to-pdf) - Markdown to PDF CLI
Expand Down
5 changes: 4 additions & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"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: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 --css=src/testdata/bluecode.css -- README.md",
"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: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"
},
Expand All @@ -19,6 +19,9 @@
"@std/assert": "jsr:@std/assert@^1.0.2",
"@std/cli": "jsr:@std/[email protected]",
"@std/fmt": "jsr:@std/fmt@^1",
"@std/front-matter": "jsr:@std/front-matter@^1.0.5",
"@std/path": "jsr:@std/path@^1.0.8",
"@std/yaml": "jsr:@std/yaml@^1.0.5",
"md4w": "npm:md4w@^0.2.6"
},
"exclude": [".gitattributes", ".github", "src/testdata", "**/*/*_test.ts"]
Expand Down
30 changes: 29 additions & 1 deletion deno.lock

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

8 changes: 8 additions & 0 deletions src/testdata/bluecode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
stylesheet: bluecode.css
---

# Blue Code
```js
console.log("Hello, World!");
```
31 changes: 24 additions & 7 deletions src/utils/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { init as initMd4w, mdToHtml } from "md4w";
import type { MdToPdfOptions } from "../types.ts";
import { getFilename } from "./filename.ts";
import { extract } from "@std/front-matter/yaml";
import { parse } from "@std/yaml/parse";
import { join } from "@std/path";

export const DEFAULT_PORT = 33433;

Expand All @@ -20,18 +23,32 @@ export function launchHttpServer(

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 fileContent = decoder.decode(await Deno.readFile(path));

let markdown = "";
let stylesheet = "";
// Front matter
try {
const file = extract(fileContent);
const frontMatter = parse(file.frontMatter) as {
stylesheet?: string;
};
markdown = file.body;
if (frontMatter && frontMatter.stylesheet) {
stylesheet = join(path, "..", frontMatter.stylesheet);
}
} catch (_e) {
markdown = fileContent;
}
stylesheet = options?.css ? options?.css : stylesheet;

const content = mdToHtml(markdown);
const title = getFilename(path.split("/").at(-1) || "") || "Untitled";
return new Response(
`<html>
<head>
<title>${title}</title>
<style>${css}</style>
${stylesheet && `<link rel="stylesheet" href="${stylesheet}" />`}
</head>
<body>
${content}
Expand Down

0 comments on commit b8766cc

Please sign in to comment.