https://github.com/maxchang3/newmd
A CLI tool that creates markdown files with frontmatter using a Zod schema.
https://github.com/maxchang3/newmd
astro cli-tool frontmatter markdown zod
Last synced: about 1 year ago
JSON representation
A CLI tool that creates markdown files with frontmatter using a Zod schema.
- Host: GitHub
- URL: https://github.com/maxchang3/newmd
- Owner: maxchang3
- License: mit
- Created: 2024-08-22T12:49:11.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2025-04-29T14:14:27.000Z (about 1 year ago)
- Last Synced: 2025-04-29T14:16:59.338Z (about 1 year ago)
- Topics: astro, cli-tool, frontmatter, markdown, zod
- Language: TypeScript
- Homepage:
- Size: 401 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
➕ newmd
A CLI tool that creates markdown files with frontmatter using a Zod schema.
npx newmd blog "Hello World"
\* `blog` is the default schema with some fields. You can define your own schema in the config file.
[](https://www.npmjs.com/package/newmd)
[](https://github.com/maxchang3/newmd/actions)
[](https://biomejs.dev)
[](LICENSE)
## Usage
## CLI
Run the following command in your terminal:
```sh
npx newmd blog "Hello World"
```
or using `pnpx`(`pnpm dlx`):
```sh
pnpx newmd blog "Hello World"
```
Will create a markdown file with the following content:
```md
---
title: Hello World
description: ''
pubDate: 2025-03-09T01:57:00.000Z
---
```
\* The `pubDate` field will be filled with `new Date()`.
You can install it globally for convenience.
### Options
See `newmd --help` for more details, following is a brief description.
```sh
newmd
```
- `--content ` Set the content of the markdown file
- `--path ` Set the output directory
- `--slug ` Set the slug for the filename, if not provided, it will be generated from the slugified title.
- `--cwd ` Set the current working directory
- `--toml` Whether to use TOML format for frontmatter, default is `false`
- `--overwrite` Whether to overwrite the existing file, default is `false`
## Config file
You need to create a config file to define the schemas for the frontmatter.
The config structure and default values are as follows:
```ts
// newmd.config.[js,mjs,ts]
import { defineConfig, z } from 'newmd'
export default defineConfig({
// The format of the frontmatter.
format: 'yaml',
// Root path for the markdown file.
path: '.',
// Schemas for the frontmatter.
schemas: {
blog: z.object({
title: z.string(),
description: z.string().optional(),
pubDate: z.coerce.date(),
updatedDate: z.coerce.date().optional(),
}),
},
})
```
## Integration
### With [Astro](https://astro.build/)
Say you have this content config file:
```ts
// src/content.config.ts
import { glob } from 'astro/loaders'
import { defineCollection, z } from 'astro:content'
const blog = defineCollection({
loader: glob({ pattern: '**/*.md', base: './src/data/blog' }),
schema: z.object({
title: z.string(),
permalink: z.string().optional(),
}),
})
export const collections = { blog }
```
You can create a newmd config file like this:
```ts
// newmd.config.ts
import { defineConfig, z } from 'newmd'
export default defineConfig({
// Corresponding to the `base` option in the content config.
path: './src/data/blog',
schemas: { // Copy the schema from the content config.
blog: z.object({
title: z.string(),
description: z.string().optional(),
pubDate: z.coerce.date(),
updatedDate: z.coerce.date().optional(),
}),
},
})
```
Now you can use the same schema to create markdown files with frontmatter by running `npx newmd blog "Hello World"`.
## Credits
This project won't be possible without [@toiroakr](https://github.com/toiroakr)'s [zod-empty](https://github.com/toiroakr/zod-empty/) and other open-source projects.
## License
[MIT](./LICENSE) License © 2024-PRESENT [Max Chang](https://github.com/maxchang3)