Skip to content

Commit

Permalink
feat: allow toml/json/jsonc as config file (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
lino-levan authored Aug 23, 2023
1 parent d547e8e commit b083f99
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 31 deletions.
3 changes: 2 additions & 1 deletion deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@

// test building websites
"test:deno_json": "cd tests/end_to_end/deno_json && pyro build && cd ../../..",
"test:json_config": "cd tests/end_to_end/json_config && pyro build && cd ../../..",
"test:unocss": "cd tests/end_to_end/unocss && pyro build && cd ../../..",
"test:production": "cd www && pyro build && cd ..",
"test": "deno task test:production && deno task test:deno_json && deno task test:unocss"
"test": "deno task test:production && deno task test:json_config && deno task test:deno_json && deno task test:unocss"
},
"compilerOptions": {
"jsx": "react-jsx",
Expand Down
22 changes: 12 additions & 10 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type { JSX } from "https://esm.sh/[email protected].0";
export type { JSX } from "https://esm.sh/[email protected].1";
export { renderToString } from "https://esm.sh/[email protected]";

export {
Expand All @@ -8,19 +8,21 @@ export {
resolve,
toFileUrl,
win32,
} from "https://deno.land/[email protected]/path/mod.ts";
export { walkSync } from "https://deno.land/[email protected]/fs/walk.ts";
export { parse } from "https://deno.land/[email protected]/yaml/mod.ts";
export { copySync } from "https://deno.land/[email protected]/fs/copy.ts";
export { serveDir } from "https://deno.land/[email protected]/http/file_server.ts";
export { extract } from "https://deno.land/[email protected]/front_matter/any.ts";
export { existsSync } from "https://deno.land/[email protected]/fs/exists.ts";
} from "https://deno.land/[email protected]/path/mod.ts";
export { walkSync } from "https://deno.land/[email protected]/fs/walk.ts";
export { parse as parseYaml } from "https://deno.land/[email protected]/yaml/mod.ts";
export { parse as parseJsonc } from "https://deno.land/[email protected]/jsonc/mod.ts";
export { parse as parseToml } from "https://deno.land/[email protected]/toml/mod.ts";
export { copySync } from "https://deno.land/[email protected]/fs/copy.ts";
export { serveDir } from "https://deno.land/[email protected]/http/file_server.ts";
export { extract } from "https://deno.land/[email protected]/front_matter/any.ts";
export { existsSync } from "https://deno.land/[email protected]/fs/exists.ts";

export * as esbuild from "https://deno.land/x/[email protected]/mod.js";
export { denoPlugins } from "https://deno.land/x/[email protected]/mod.ts";

export { createGenerator } from "https://esm.sh/@unocss/[email protected].1";
export { presetUno } from "https://esm.sh/@unocss/[email protected].1";
export { createGenerator } from "https://esm.sh/@unocss/[email protected].2";
export { presetUno } from "https://esm.sh/@unocss/[email protected].2";

export { decode } from "https://deno.land/x/[email protected]/mod.ts";

Expand Down
15 changes: 3 additions & 12 deletions src/build.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
import {
copySync,
esbuild,
parse,
posix,
resolve,
walkSync,
win32,
} from "../deps.ts";
import { copySync, esbuild, posix, resolve, walkSync, win32 } from "../deps.ts";
import { render } from "./lib/render.ts";
import { getMagic } from "./lib/magic.ts";
import { CSS } from "./lib/css.ts";
import { Config } from "./lib/types.ts";
import { loadPlugins } from "./utils.tsx";
import { loadPlugins, readConfig } from "./utils.tsx";

export async function build() {
try {
Expand All @@ -27,7 +18,7 @@ export async function build() {
Deno.mkdirSync("./build/_pyro");
Deno.writeTextFileSync("./build/_pyro/bundle.css", CSS);

const config = parse(Deno.readTextFileSync("pyro.yml")) as Config;
const config = readConfig();
const magic = getMagic();
const plugins = config.plugins ? await loadPlugins(config.plugins) : [];

Expand Down
7 changes: 3 additions & 4 deletions src/dev.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { parse, resolve, serveDir } from "../deps.ts";
import { resolve, serveDir } from "../deps.ts";
import { render } from "./lib/render.ts";
import { getMagic } from "./lib/magic.ts";
import { CSS } from "./lib/css.ts";
import { Config } from "./lib/types.ts";
import { loadPlugins } from "./utils.tsx";
import { loadPlugins, readConfig } from "./utils.tsx";

export async function dev(hostname = "0.0.0.0", port = 8000) {
const config = parse(Deno.readTextFileSync("pyro.yml")) as Config;
let BUILD_ID = Math.random().toString();

Deno.serve({
Expand All @@ -16,6 +14,7 @@ export async function dev(hostname = "0.0.0.0", port = 8000) {
const url = new URL(req.url);
const pathname = url.pathname;

const config = readConfig();
const plugins = config.plugins ? await loadPlugins(config.plugins) : [];

for (const plugin of plugins) {
Expand Down
25 changes: 25 additions & 0 deletions src/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import {
denoPlugins,
esbuild,
existsSync,
parseJsonc,
parseToml,
parseYaml,
rehypeStringify,
remarkGfm,
remarkParse,
Expand Down Expand Up @@ -40,6 +43,28 @@ export function readFileSync(...options: string[]): [FileTypes, string] {
}
}

export function readConfig(): Config {
let extension;
if (existsSync("pyro.yml")) extension = "yml";
if (existsSync("pyro.yaml")) extension = "yaml";
if (existsSync("pyro.json")) extension = "json";
if (existsSync("pyro.jsonc")) extension = "jsonc";
if (existsSync("pyro.toml")) extension = "toml";

const file = Deno.readTextFileSync(`pyro.${extension}`);

if (extension === "yml" || extension === "yaml") {
return parseYaml(file) as Config;
} else if (extension === "json") {
return JSON.parse(file);
} else if (extension === "jsonc") {
return parseJsonc(file) as unknown as Config;
} else if (extension === "toml") {
return parseToml(file) as unknown as Config;
}
throw new Error("No Pyro configuration file found. Try making a `pyro.yml`");
}

function removeFrontmatter(markdown: string) {
return markdown.replace(/^---.+?---/s, "");
}
Expand Down
5 changes: 5 additions & 0 deletions tests/end_to_end/json_config/pages/getting-started/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: Getting Started
description: My first Pyro directory
index: 1
---
17 changes: 17 additions & 0 deletions tests/end_to_end/json_config/pages/getting-started/submenu.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
title: Submenu
description: My first Pyro directory
index: 0
---

This is the first subpage in the docs. We have access to all of markdown and
html here.

| We have | Tables |
| ------- | ------ |
| 1, 1 | 1, 2 |
| 2, 1 | 2, 2 |

We even support Github extensions to the spec like math blocks.

$$ x^2 + y^2 = z^2 $$
7 changes: 7 additions & 0 deletions tests/end_to_end/json_config/pages/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
title: Hello World
description: My first Pyro page
index: 0
---

How are you doing today?
4 changes: 4 additions & 0 deletions tests/end_to_end/json_config/pyro.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"title": "Pyro Site",
"github": "https://github.com/lino-levan/pyro"
}
Binary file added tests/end_to_end/json_config/static/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 6 additions & 4 deletions www/pages/getting-started/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ index: 1
---

Pyro has a very simplistic view of configurations, there is only one file you
need to worry about (being `pyro.yml`).
need to worry about (being `pyro.yml`/`pyro.toml`/`pyro.json`/`pyro.jsonc`).

## What goes into a `pyro.yml`?
## What goes into a Pyro configuration file?

This is just a standard yaml configuration file. Inside you will define all
This is just a standard configuration file. Inside you will define all
properties related to pyro and plugins you may install.

This is an example of a `pyro.yml` configuration file:

```yaml
# The title of the site
title: Pyro
Expand Down Expand Up @@ -55,7 +57,7 @@ plugin:
## How do I configure individual pages?
You can configure individual page metadata using markdown frontmatter. We
support yaml/toml/json.
support yaml/toml/json/jsonc.
Here is an example in YAML:
Expand Down

0 comments on commit b083f99

Please sign in to comment.