@md-plugins/md-plugin-frontmatter 0.1.0-alpha.1
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.md +21 -0
- package/README.md +108 -0
- package/dist/index.d.mts +49 -0
- package/dist/index.d.ts +49 -0
- package/dist/index.mjs +32 -0
- package/package.json +49 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024-present, Jeff Galbraith
|
|
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
|
|
13
|
+
all 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
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# @md-plugins/md-plugin-frontmatter
|
|
2
|
+
|
|
3
|
+
A **Markdown-It** plugin that extracts and processes frontmatter from Markdown content. Frontmatter is commonly used for metadata such as titles, authors, and dates, making this plugin essential for static site generators, documentation tools, and content management systems.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- Extracts frontmatter from Markdown files.
|
|
8
|
+
- Supports rendering the frontmatter as raw Markdown or HTML.
|
|
9
|
+
- Compatible with various frontmatter syntaxes (YAML, JSON, TOML) via the `gray-matter` library.
|
|
10
|
+
- Stores extracted frontmatter in the `frontmatter` property of the Markdown-It environment (`env`).
|
|
11
|
+
- Optionally renders an excerpt from the Markdown content.
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
Install the plugin via your preferred package manager:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
# With npm:
|
|
19
|
+
npm install @md-plugins/md-plugin-frontmatter
|
|
20
|
+
# Or with Yarn:
|
|
21
|
+
yarn add @md-plugins/md-plugin-frontmatter
|
|
22
|
+
# Or with pnpm:
|
|
23
|
+
pnpm add @md-plugins/md-plugin-frontmatter
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Usage
|
|
27
|
+
|
|
28
|
+
### Basic Setup
|
|
29
|
+
|
|
30
|
+
```js
|
|
31
|
+
import MarkdownIt from 'markdown-it';
|
|
32
|
+
import { frontmatterPlugin } from '@md-plugins/md-plugin-frontmatter';
|
|
33
|
+
import type { MarkdownItEnv } from '@md-plugins/shared';
|
|
34
|
+
|
|
35
|
+
const md = new MarkdownIt();
|
|
36
|
+
md.use(frontmatterPlugin, {
|
|
37
|
+
renderExcerpt: true,
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const markdownContent = `
|
|
41
|
+
---
|
|
42
|
+
title: Frontmatter Example
|
|
43
|
+
author: Jane Doe
|
|
44
|
+
date: 2024-01-01
|
|
45
|
+
---
|
|
46
|
+
|
|
47
|
+
# Main Content
|
|
48
|
+
|
|
49
|
+
This is the main content of the Markdown file.
|
|
50
|
+
`;
|
|
51
|
+
|
|
52
|
+
const env: MarkdownItEnv = {};
|
|
53
|
+
const renderedOutput = md.render(markdownContent, env);
|
|
54
|
+
|
|
55
|
+
console.log('Rendered Output:', renderedOutput);
|
|
56
|
+
console.log('Extracted Frontmatter:', env.frontmatter);
|
|
57
|
+
console.log('Extracted Excerpt:', env.excerpt);
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Example Output
|
|
61
|
+
|
|
62
|
+
For the example above, the `env` will contain:
|
|
63
|
+
|
|
64
|
+
```json
|
|
65
|
+
{
|
|
66
|
+
"frontmatter": {
|
|
67
|
+
"title": "Frontmatter Example",
|
|
68
|
+
"author": "Jane Doe",
|
|
69
|
+
"date": "2024-01-01"
|
|
70
|
+
},
|
|
71
|
+
"excerpt": "<p>This is the main content of the Markdown file.</p>"
|
|
72
|
+
}
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Options
|
|
76
|
+
|
|
77
|
+
The `md-plugin-frontmatter` plugin supports the following options:
|
|
78
|
+
|
|
79
|
+
| Option | Type | Default | Description |
|
|
80
|
+
| ----------------- | ------- | ------- | ---------------------------------------------------------------------------------------------- |
|
|
81
|
+
| grayMatterOptions | object | {} | Options for the gray-matter library. Refer to the gray-matter documentation. |
|
|
82
|
+
| renderExcerpt | boolean | true | Whether to render the excerpt as HTML. If false, the raw Markdown is extracted as the excerpt. |
|
|
83
|
+
|
|
84
|
+
## Advanced Usage
|
|
85
|
+
|
|
86
|
+
### Customizing Frontmatter Parsing
|
|
87
|
+
|
|
88
|
+
You can customize the behavior of the `gray-matter` library by passing `grayMatterOptions`:
|
|
89
|
+
|
|
90
|
+
```js
|
|
91
|
+
md.use(frontmatterPlugin, {
|
|
92
|
+
grayMatterOptions: {
|
|
93
|
+
delimiters: '+++', // Use "+++" as the frontmatter delimiter
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## Testing
|
|
99
|
+
|
|
100
|
+
Run the unit tests with `Vitest` to ensure the plugin behaves as expected:
|
|
101
|
+
|
|
102
|
+
```bash
|
|
103
|
+
pnpm test
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
## License
|
|
107
|
+
|
|
108
|
+
This project is licensed under the MIT License. See the [LICENSE](LICENSE.md) file for details.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { PluginWithOptions } from 'markdown-it';
|
|
2
|
+
import matter from 'gray-matter';
|
|
3
|
+
|
|
4
|
+
type GrayMatterOptions = matter.GrayMatterOption<string, GrayMatterOptions>;
|
|
5
|
+
/**
|
|
6
|
+
* Options of md-plugin-frontmatter
|
|
7
|
+
*/
|
|
8
|
+
interface FrontmatterPluginOptions {
|
|
9
|
+
/**
|
|
10
|
+
* Options of gray-matter
|
|
11
|
+
*
|
|
12
|
+
* @see https://github.com/jonschlinkert/gray-matter#options
|
|
13
|
+
*/
|
|
14
|
+
grayMatterOptions?: GrayMatterOptions;
|
|
15
|
+
/**
|
|
16
|
+
* Render the excerpt or not
|
|
17
|
+
*
|
|
18
|
+
* @default true
|
|
19
|
+
*/
|
|
20
|
+
renderExcerpt?: boolean;
|
|
21
|
+
}
|
|
22
|
+
declare module '@md-plugins/shared' {
|
|
23
|
+
interface MarkdownItEnv {
|
|
24
|
+
/**
|
|
25
|
+
* The raw Markdown content without frontmatter
|
|
26
|
+
*/
|
|
27
|
+
content?: string;
|
|
28
|
+
/**
|
|
29
|
+
* The excerpt that extracted by `md-plugin-frontmatter`
|
|
30
|
+
*
|
|
31
|
+
* - Would be the rendered HTML when `renderExcerpt` is enabled
|
|
32
|
+
* - Would be the raw Markdown when `renderExcerpt` is disabled
|
|
33
|
+
*/
|
|
34
|
+
excerpt?: string;
|
|
35
|
+
/**
|
|
36
|
+
* The frontmatter that extracted by `md-plugin-frontmatter`
|
|
37
|
+
*/
|
|
38
|
+
frontmatter?: Record<string, unknown>;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Get markdown frontmatter and excerpt
|
|
44
|
+
*
|
|
45
|
+
* Extract them into env
|
|
46
|
+
*/
|
|
47
|
+
declare const frontmatterPlugin: PluginWithOptions<FrontmatterPluginOptions>;
|
|
48
|
+
|
|
49
|
+
export { type FrontmatterPluginOptions, frontmatterPlugin };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { PluginWithOptions } from 'markdown-it';
|
|
2
|
+
import matter from 'gray-matter';
|
|
3
|
+
|
|
4
|
+
type GrayMatterOptions = matter.GrayMatterOption<string, GrayMatterOptions>;
|
|
5
|
+
/**
|
|
6
|
+
* Options of md-plugin-frontmatter
|
|
7
|
+
*/
|
|
8
|
+
interface FrontmatterPluginOptions {
|
|
9
|
+
/**
|
|
10
|
+
* Options of gray-matter
|
|
11
|
+
*
|
|
12
|
+
* @see https://github.com/jonschlinkert/gray-matter#options
|
|
13
|
+
*/
|
|
14
|
+
grayMatterOptions?: GrayMatterOptions;
|
|
15
|
+
/**
|
|
16
|
+
* Render the excerpt or not
|
|
17
|
+
*
|
|
18
|
+
* @default true
|
|
19
|
+
*/
|
|
20
|
+
renderExcerpt?: boolean;
|
|
21
|
+
}
|
|
22
|
+
declare module '@md-plugins/shared' {
|
|
23
|
+
interface MarkdownItEnv {
|
|
24
|
+
/**
|
|
25
|
+
* The raw Markdown content without frontmatter
|
|
26
|
+
*/
|
|
27
|
+
content?: string;
|
|
28
|
+
/**
|
|
29
|
+
* The excerpt that extracted by `md-plugin-frontmatter`
|
|
30
|
+
*
|
|
31
|
+
* - Would be the rendered HTML when `renderExcerpt` is enabled
|
|
32
|
+
* - Would be the raw Markdown when `renderExcerpt` is disabled
|
|
33
|
+
*/
|
|
34
|
+
excerpt?: string;
|
|
35
|
+
/**
|
|
36
|
+
* The frontmatter that extracted by `md-plugin-frontmatter`
|
|
37
|
+
*/
|
|
38
|
+
frontmatter?: Record<string, unknown>;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Get markdown frontmatter and excerpt
|
|
44
|
+
*
|
|
45
|
+
* Extract them into env
|
|
46
|
+
*/
|
|
47
|
+
declare const frontmatterPlugin: PluginWithOptions<FrontmatterPluginOptions>;
|
|
48
|
+
|
|
49
|
+
export { type FrontmatterPluginOptions, frontmatterPlugin };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import grayMatter from 'gray-matter';
|
|
2
|
+
|
|
3
|
+
const frontmatterPlugin = (md, { grayMatterOptions, renderExcerpt = true } = {}) => {
|
|
4
|
+
const render = md.render.bind(md);
|
|
5
|
+
md.render = (src, env = {}) => {
|
|
6
|
+
let data, content, excerpt;
|
|
7
|
+
try {
|
|
8
|
+
({ data, content } = grayMatter(src, grayMatterOptions));
|
|
9
|
+
} catch (error) {
|
|
10
|
+
console.error("Failed to parse frontmatter:", error);
|
|
11
|
+
data = {};
|
|
12
|
+
content = src;
|
|
13
|
+
excerpt = void 0;
|
|
14
|
+
}
|
|
15
|
+
env.content = content;
|
|
16
|
+
env.frontmatter = {
|
|
17
|
+
// allow providing default value
|
|
18
|
+
...env.frontmatter,
|
|
19
|
+
...data
|
|
20
|
+
};
|
|
21
|
+
env.excerpt = renderExcerpt && data.excerpt ? (
|
|
22
|
+
// render the excerpt with original markdown-it render method.
|
|
23
|
+
render(data.excerpt, env)
|
|
24
|
+
) : (
|
|
25
|
+
// use the raw excerpt directly
|
|
26
|
+
excerpt
|
|
27
|
+
);
|
|
28
|
+
return render(content, env);
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export { frontmatterPlugin };
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@md-plugins/md-plugin-frontmatter",
|
|
3
|
+
"version": "0.1.0-alpha.1",
|
|
4
|
+
"description": "A markdown-it plugin for handling frontmatter in markdown files.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"markdown-it",
|
|
7
|
+
"quasarframework",
|
|
8
|
+
"vue",
|
|
9
|
+
"types"
|
|
10
|
+
],
|
|
11
|
+
"homepage": "https://github.com/md-plugins",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/md-plugins/md-plugins/issues"
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/md-plugins/md-plugins.git"
|
|
18
|
+
},
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"author": "hawkeye64 <galbraith64@gmail.com>",
|
|
21
|
+
"type": "module",
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"import": {
|
|
25
|
+
"types": "./dist/index.d.mts",
|
|
26
|
+
"default": "./dist/index.mjs"
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"module": "./dist/index.mjs",
|
|
31
|
+
"types": "./dist/index.d.ts",
|
|
32
|
+
"files": [
|
|
33
|
+
"./dist"
|
|
34
|
+
],
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@types/markdown-it": "^14.1.2",
|
|
37
|
+
"gray-matter": "^4.0.3",
|
|
38
|
+
"markdown-it": "^14.1.0",
|
|
39
|
+
"@md-plugins/shared": "0.1.0-alpha.1"
|
|
40
|
+
},
|
|
41
|
+
"publishConfig": {
|
|
42
|
+
"access": "public"
|
|
43
|
+
},
|
|
44
|
+
"scripts": {
|
|
45
|
+
"build": "unbuild",
|
|
46
|
+
"clean": "rm -rf dist",
|
|
47
|
+
"test": "vitest"
|
|
48
|
+
}
|
|
49
|
+
}
|