@holdenmatt/md-schema 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +75 -0
- package/dist/index.d.ts +39 -0
- package/dist/index.js +37 -0
- package/package.json +44 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Matt Holden
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# md-schema
|
|
2
|
+
|
|
3
|
+
Typed frontmatter schemas for markdown documents.
|
|
4
|
+
|
|
5
|
+
`@holdenmatt/md-schema` parses YAML frontmatter with `gray-matter` and validates it with a Zod object schema. The markdown body is returned as trimmed markdown text.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm add @holdenmatt/md-schema zod
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { z } from "zod";
|
|
17
|
+
import { markdownSchema } from "@holdenmatt/md-schema";
|
|
18
|
+
|
|
19
|
+
const postSchema = markdownSchema(
|
|
20
|
+
z.object({
|
|
21
|
+
title: z.string(),
|
|
22
|
+
draft: z.boolean().default(false),
|
|
23
|
+
slug: z.string().transform((value) => value.toLowerCase()),
|
|
24
|
+
}),
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
const result = postSchema.parse(`---
|
|
28
|
+
title: Hello
|
|
29
|
+
slug: Hello-World
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
# Hello
|
|
33
|
+
`);
|
|
34
|
+
|
|
35
|
+
if (result.success) {
|
|
36
|
+
result.data.frontmatter.title;
|
|
37
|
+
result.data.body;
|
|
38
|
+
result.data.markdown;
|
|
39
|
+
}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## API
|
|
43
|
+
|
|
44
|
+
### `markdownSchema(frontmatterSchema)`
|
|
45
|
+
|
|
46
|
+
Creates a parser for markdown documents with YAML frontmatter.
|
|
47
|
+
|
|
48
|
+
- `frontmatterSchema`: a Zod object schema used to validate parsed frontmatter.
|
|
49
|
+
- Returns `{ schema, parse }`.
|
|
50
|
+
- `schema` is the original Zod schema.
|
|
51
|
+
- `parse(markdown)` returns a safe result object and does not throw for YAML parse or Zod validation failures.
|
|
52
|
+
|
|
53
|
+
Success:
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
{
|
|
57
|
+
success: true,
|
|
58
|
+
data: {
|
|
59
|
+
frontmatter,
|
|
60
|
+
body,
|
|
61
|
+
markdown,
|
|
62
|
+
},
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Failure:
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
{
|
|
70
|
+
success: false,
|
|
71
|
+
error,
|
|
72
|
+
}
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Only frontmatter is validated. The body is not parsed and is returned as trimmed markdown text.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type * as z from "zod";
|
|
2
|
+
/** A Zod object schema used to validate parsed YAML frontmatter. */
|
|
3
|
+
export type FrontmatterSchema = z.ZodObject;
|
|
4
|
+
/**
|
|
5
|
+
* Parsed markdown data returned after frontmatter validation succeeds.
|
|
6
|
+
*/
|
|
7
|
+
export type MarkdownSchemaData<Schema extends FrontmatterSchema> = {
|
|
8
|
+
/** Frontmatter after Zod validation. */
|
|
9
|
+
frontmatter: z.output<Schema>;
|
|
10
|
+
/** Markdown content after frontmatter, trimmed at both ends. */
|
|
11
|
+
body: string;
|
|
12
|
+
/** Original markdown input string, preserved exactly. */
|
|
13
|
+
markdown: string;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Safe parse result for a markdown document with typed frontmatter.
|
|
17
|
+
*/
|
|
18
|
+
export type MarkdownSchemaResult<Schema extends FrontmatterSchema> = {
|
|
19
|
+
success: true;
|
|
20
|
+
data: MarkdownSchemaData<Schema>;
|
|
21
|
+
} | {
|
|
22
|
+
success: false;
|
|
23
|
+
error: unknown;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* A reusable parser for markdown documents with a specific frontmatter schema.
|
|
27
|
+
*/
|
|
28
|
+
export type MarkdownSchema<Schema extends FrontmatterSchema> = {
|
|
29
|
+
/** Frontmatter schema used by this parser. */
|
|
30
|
+
schema: Schema;
|
|
31
|
+
/** Parse markdown text into typed frontmatter and a trimmed body. */
|
|
32
|
+
parse(markdown: string): MarkdownSchemaResult<Schema>;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Create a parser for markdown documents with YAML frontmatter validated by a Zod object schema.
|
|
36
|
+
*
|
|
37
|
+
* YAML parse failures and Zod validation failures are returned as safe `{ success: false }` results.
|
|
38
|
+
*/
|
|
39
|
+
export declare function markdownSchema<Schema extends FrontmatterSchema>(frontmatterSchema: Schema): MarkdownSchema<Schema>;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import matter from "gray-matter";
|
|
2
|
+
/**
|
|
3
|
+
* Create a parser for markdown documents with YAML frontmatter validated by a Zod object schema.
|
|
4
|
+
*
|
|
5
|
+
* YAML parse failures and Zod validation failures are returned as safe `{ success: false }` results.
|
|
6
|
+
*/
|
|
7
|
+
export function markdownSchema(frontmatterSchema) {
|
|
8
|
+
return {
|
|
9
|
+
schema: frontmatterSchema,
|
|
10
|
+
parse(markdown) {
|
|
11
|
+
let file;
|
|
12
|
+
try {
|
|
13
|
+
file = matter(markdown);
|
|
14
|
+
}
|
|
15
|
+
catch (error) {
|
|
16
|
+
return { success: false, error };
|
|
17
|
+
}
|
|
18
|
+
try {
|
|
19
|
+
const result = frontmatterSchema.safeParse(file.data);
|
|
20
|
+
if (!result.success) {
|
|
21
|
+
return { success: false, error: result.error };
|
|
22
|
+
}
|
|
23
|
+
return {
|
|
24
|
+
success: true,
|
|
25
|
+
data: {
|
|
26
|
+
frontmatter: result.data,
|
|
27
|
+
body: file.content.trim(),
|
|
28
|
+
markdown,
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
return { success: false, error };
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@holdenmatt/md-schema",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Typed frontmatter schemas for markdown documents",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/holdenmatt/md-schema.git"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"dist"
|
|
12
|
+
],
|
|
13
|
+
"type": "module",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"import": "./dist/index.js"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "tsc",
|
|
22
|
+
"prepare": "tsc",
|
|
23
|
+
"test": "vitest run",
|
|
24
|
+
"lint": "oxlint",
|
|
25
|
+
"format": "oxfmt --write .",
|
|
26
|
+
"format:check": "oxfmt --check .",
|
|
27
|
+
"check": "pnpm format:check && pnpm lint && pnpm test && pnpm build"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"gray-matter": "^4.0.3"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@types/node": "^26.0.1",
|
|
34
|
+
"oxfmt": "^0.56.0",
|
|
35
|
+
"oxlint": "^1.71.0",
|
|
36
|
+
"typescript": "^6.0.3",
|
|
37
|
+
"vitest": "^4.1.9",
|
|
38
|
+
"zod": "^4.4.3"
|
|
39
|
+
},
|
|
40
|
+
"peerDependencies": {
|
|
41
|
+
"zod": "^4.0.0"
|
|
42
|
+
},
|
|
43
|
+
"packageManager": "pnpm@10.33.0"
|
|
44
|
+
}
|