@mdit-vue/plugin-frontmatter 0.5.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/CHANGELOG.md ADDED
@@ -0,0 +1,10 @@
1
+ # Change Log
2
+
3
+ All notable changes to this project will be documented in this file.
4
+ See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
+
6
+ # [0.5.0](https://github.com/mdit-vue/mdit-vue/compare/v0.4.0...v0.5.0) (2022-07-06)
7
+
8
+ ### Features
9
+
10
+ - **plugin-frontmatter:** add frontmatter plugin ([bcffc7a](https://github.com/mdit-vue/mdit-vue/commit/bcffc7a7fef0c48a869d703f0358d168524ac19e))
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022-present, mdit-vue Contributors
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,66 @@
1
+ # @mdit-vue/plugin-frontmatter
2
+
3
+ [![npm](https://badgen.net/npm/v/@mdit-vue/plugin-frontmatter)](https://www.npmjs.com/package/@mdit-vue/plugin-frontmatter)
4
+ [![license](https://badgen.net/github/license/mdit-vue/mdit-vue)](https://github.com/mdit-vue/mdit-vue/blob/main/LICENSE)
5
+
6
+ A [markdown-it](https://github.com/markdown-it/markdown-it) plugin to get markdown frontmatter with [gray-matter](https://github.com/jonschlinkert/gray-matter).
7
+
8
+ - Extract frontmatter into markdown-it `env.frontmatter`.
9
+ - Allow providing default frontmatter via markdown-it `env.frontmatter`.
10
+ - Support extracting rendered excerpt into markdown-it `env.excerpt`.
11
+
12
+ ## Install
13
+
14
+ ```sh
15
+ npm i @mdit-vue/plugin-frontmatter
16
+ ```
17
+
18
+ ## Usage
19
+
20
+ ```ts
21
+ import MarkdownIt from 'markdown-it';
22
+ import { frontmatterPlugin } from '@mdit-vue/plugin-frontmatter';
23
+
24
+ const md = MarkdownIt({ html: true }).use(frontmatterPlugin, {
25
+ // options
26
+ grayMatterOptions: {
27
+ excerpt: true,
28
+ excerpt_separator: '<!-- more -->',
29
+ },
30
+ });
31
+ const env: MarkdownItEnv = {};
32
+
33
+ const rendered = md.render(
34
+ `\
35
+ ---
36
+ title: foobar
37
+ ---
38
+ Everything above <!-- more --> will be extracted as excerpt.
39
+ <!-- more -->
40
+ `,
41
+ env,
42
+ );
43
+
44
+ console.log(env.frontmatter);
45
+ console.log(env.excerpt);
46
+ ```
47
+
48
+ ## Options
49
+
50
+ ### grayMatterOptions
51
+
52
+ - Type: `GrayMatterOptions`
53
+
54
+ - Details:
55
+
56
+ Options of [gray-matter](https://github.com/jonschlinkert/gray-matter).
57
+
58
+ ### renderExcerpt
59
+
60
+ - Type: `boolean`
61
+
62
+ - Default: `true`
63
+
64
+ Render the [excerpt](https://github.com/jonschlinkert/gray-matter#optionsexcerpt) with markdown-it or not.
65
+
66
+ This option will only take effects when `grayMatterOptions.excerpt` is enabled (disabled by default) or `grayMatterOptions.excerpt_separator` is set (undefined by default).
@@ -0,0 +1,11 @@
1
+ import { defineBuildConfig } from 'unbuild';
2
+
3
+ export default defineBuildConfig({
4
+ clean: true,
5
+ declaration: true,
6
+ entries: ['src/index'],
7
+ rollup: {
8
+ emitCJS: true,
9
+ inlineDependencies: false,
10
+ },
11
+ });
package/dist/index.cjs ADDED
@@ -0,0 +1,24 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ const grayMatter = require('gray-matter');
6
+
7
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e["default"] : e; }
8
+
9
+ const grayMatter__default = /*#__PURE__*/_interopDefaultLegacy(grayMatter);
10
+
11
+ const frontmatterPlugin = (md, { grayMatterOptions, renderExcerpt = true } = {}) => {
12
+ const render = md.render.bind(md);
13
+ md.render = (src, env = {}) => {
14
+ const { data, content, excerpt = "" } = grayMatter__default(src, grayMatterOptions);
15
+ env.frontmatter = {
16
+ ...env.frontmatter,
17
+ ...data
18
+ };
19
+ env.excerpt = renderExcerpt && excerpt ? render(excerpt, { ...env }) : excerpt;
20
+ return render(content, env);
21
+ };
22
+ };
23
+
24
+ exports.frontmatterPlugin = frontmatterPlugin;
@@ -0,0 +1,34 @@
1
+ import { PluginWithOptions } from 'markdown-it';
2
+ import { GrayMatterOption } from 'gray-matter';
3
+
4
+ declare type GrayMatterOptions = GrayMatterOption<string, GrayMatterOptions>;
5
+ /**
6
+ * Options of @mdit-vue/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
+ renderExcerpt?: boolean;
19
+ }
20
+ declare module '@mdit-vue/shared' {
21
+ interface MarkdownItEnv {
22
+ excerpt?: string;
23
+ frontmatter?: Record<string, unknown>;
24
+ }
25
+ }
26
+
27
+ /**
28
+ * Get markdown frontmatter and excerpt
29
+ *
30
+ * Extract them to env
31
+ */
32
+ declare const frontmatterPlugin: PluginWithOptions<FrontmatterPluginOptions>;
33
+
34
+ export { FrontmatterPluginOptions, frontmatterPlugin };
package/dist/index.mjs ADDED
@@ -0,0 +1,16 @@
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
+ const { data, content, excerpt = "" } = grayMatter(src, grayMatterOptions);
7
+ env.frontmatter = {
8
+ ...env.frontmatter,
9
+ ...data
10
+ };
11
+ env.excerpt = renderExcerpt && excerpt ? render(excerpt, { ...env }) : excerpt;
12
+ return render(content, env);
13
+ };
14
+ };
15
+
16
+ export { frontmatterPlugin };
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "@mdit-vue/plugin-frontmatter",
3
+ "version": "0.5.0",
4
+ "description": "A markdown-it plugin to get markdown frontmatter",
5
+ "keywords": [
6
+ "markdown-it",
7
+ "markdown-it-plugin",
8
+ "vue",
9
+ "frontmatter",
10
+ "excerpt"
11
+ ],
12
+ "homepage": "https://github.com/mdit-vue",
13
+ "bugs": {
14
+ "url": "https://github.com/mdit-vue/mdit-vue/issues"
15
+ },
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/mdit-vue/mdit-vue.git"
19
+ },
20
+ "license": "MIT",
21
+ "author": "meteorlxy <meteor.lxy@foxmail.com>",
22
+ "exports": {
23
+ ".": {
24
+ "types": "./dist/index.d.ts",
25
+ "import": "./dist/index.mjs",
26
+ "require": "./dist/index.cjs"
27
+ }
28
+ },
29
+ "main": "./dist/index.cjs",
30
+ "module": "./dist/index.mjs",
31
+ "types": "./dist/index.d.ts",
32
+ "scripts": {
33
+ "build": "unbuild",
34
+ "test": "vitest"
35
+ },
36
+ "dependencies": {
37
+ "@mdit-vue/shared": "0.5.0",
38
+ "@types/markdown-it": "^12.2.3",
39
+ "gray-matter": "^4.0.3",
40
+ "markdown-it": "^13.0.1"
41
+ },
42
+ "devDependencies": {
43
+ "c8": "^7.11.3",
44
+ "unbuild": "^0.7.4",
45
+ "vitest": "^0.17.0"
46
+ },
47
+ "publishConfig": {
48
+ "access": "public"
49
+ },
50
+ "gitHead": "33671b4a15611ea35b065269526449129eef99d6"
51
+ }
@@ -0,0 +1,38 @@
1
+ import type { MarkdownItEnv } from '@mdit-vue/shared';
2
+ import grayMatter from 'gray-matter';
3
+ import type { PluginWithOptions } from 'markdown-it';
4
+ import type { FrontmatterPluginOptions } from './types';
5
+
6
+ /**
7
+ * Get markdown frontmatter and excerpt
8
+ *
9
+ * Extract them to env
10
+ */
11
+ export const frontmatterPlugin: PluginWithOptions<FrontmatterPluginOptions> = (
12
+ md,
13
+ { grayMatterOptions, renderExcerpt = true } = {},
14
+ ): void => {
15
+ const render = md.render.bind(md);
16
+ md.render = (src, env: MarkdownItEnv = {}) => {
17
+ const { data, content, excerpt = '' } = grayMatter(src, grayMatterOptions);
18
+
19
+ // extract frontmatter
20
+ env.frontmatter = {
21
+ // allow providing default value
22
+ ...env.frontmatter,
23
+ ...data,
24
+ };
25
+
26
+ // render and extract excerpt
27
+ env.excerpt =
28
+ renderExcerpt && excerpt
29
+ ? // render the excerpt with original markdown-it render method.
30
+ // here we spread `env` to avoid mutating the original object.
31
+ // using deep clone might be a safer choice.
32
+ render(excerpt, { ...env })
33
+ : // use the raw excerpt directly
34
+ excerpt;
35
+
36
+ return render(content, env);
37
+ };
38
+ };
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './frontmatter-plugin';
2
+ export * from './types';
package/src/types.ts ADDED
@@ -0,0 +1,27 @@
1
+ import type { GrayMatterOption } from 'gray-matter';
2
+
3
+ type GrayMatterOptions = GrayMatterOption<string, GrayMatterOptions>;
4
+
5
+ /**
6
+ * Options of @mdit-vue/plugin-frontmatter
7
+ */
8
+ export interface FrontmatterPluginOptions {
9
+ /**
10
+ * Options of gray-matter
11
+ *
12
+ * @see https://github.com/jonschlinkert/gray-matter#options
13
+ */
14
+ grayMatterOptions?: GrayMatterOptions;
15
+
16
+ /**
17
+ * Render the excerpt or not
18
+ */
19
+ renderExcerpt?: boolean;
20
+ }
21
+
22
+ declare module '@mdit-vue/shared' {
23
+ interface MarkdownItEnv {
24
+ excerpt?: string;
25
+ frontmatter?: Record<string, unknown>;
26
+ }
27
+ }
@@ -0,0 +1,143 @@
1
+ import type { MarkdownItEnv } from '@mdit-vue/shared';
2
+ import MarkdownIt from 'markdown-it';
3
+ import { describe, expect, it } from 'vitest';
4
+ import { frontmatterPlugin } from '../src';
5
+ import type { FrontmatterPluginOptions } from '../src';
6
+
7
+ const testCases: {
8
+ source: string;
9
+ options: FrontmatterPluginOptions;
10
+ env: MarkdownItEnv;
11
+ frontmatter: MarkdownItEnv['frontmatter'];
12
+ excerpt: MarkdownItEnv['excerpt'];
13
+ }[] = [
14
+ {
15
+ source: `\
16
+ ---
17
+ str: string
18
+ num: 123
19
+ date: 2022-07-06
20
+ ---
21
+ `,
22
+ options: {},
23
+ env: {},
24
+ frontmatter: {
25
+ str: 'string',
26
+ num: 123,
27
+ date: new Date('2022-07-06T00:00:00Z'),
28
+ },
29
+ excerpt: '',
30
+ },
31
+ {
32
+ source: `\
33
+ ---
34
+ foo: foo
35
+ bar: bar
36
+ ---
37
+ `,
38
+ options: {},
39
+ env: {
40
+ frontmatter: {
41
+ foo: 'default-foo',
42
+ baz: 'default-baz',
43
+ },
44
+ },
45
+ frontmatter: {
46
+ foo: 'foo',
47
+ bar: 'bar',
48
+ baz: 'default-baz',
49
+ },
50
+ excerpt: '',
51
+ },
52
+ {
53
+ source: `\
54
+ # Hello, world
55
+ ---
56
+ `,
57
+ options: {},
58
+ env: {},
59
+ frontmatter: {},
60
+ excerpt: '',
61
+ },
62
+ {
63
+ source: `\
64
+ ---
65
+ foo: foo
66
+ bar: bar
67
+ ---
68
+ # Hello, world
69
+ ---
70
+ `,
71
+ options: {
72
+ grayMatterOptions: {
73
+ excerpt: true,
74
+ },
75
+ renderExcerpt: true,
76
+ },
77
+ env: {},
78
+ frontmatter: {
79
+ foo: 'foo',
80
+ bar: 'bar',
81
+ },
82
+ excerpt: '<h1>Hello, world</h1>\n',
83
+ },
84
+ {
85
+ source: `\
86
+ ---
87
+ foo: foo
88
+ bar: bar
89
+ ---
90
+ # Hello, world
91
+ <!-- more -->
92
+ `,
93
+ options: {
94
+ grayMatterOptions: {
95
+ excerpt: true,
96
+ excerpt_separator: '<!-- more -->',
97
+ },
98
+ renderExcerpt: true,
99
+ },
100
+ env: {},
101
+ frontmatter: {
102
+ foo: 'foo',
103
+ bar: 'bar',
104
+ },
105
+ excerpt: '<h1>Hello, world</h1>\n',
106
+ },
107
+ {
108
+ source: `\
109
+ ---
110
+ foo: foo
111
+ bar: bar
112
+ ---
113
+ # Hello, world
114
+ <!-- more -->
115
+ `,
116
+ options: {
117
+ grayMatterOptions: {
118
+ excerpt: true,
119
+ excerpt_separator: '<!-- more -->',
120
+ },
121
+ renderExcerpt: false,
122
+ },
123
+ env: {},
124
+ frontmatter: {
125
+ foo: 'foo',
126
+ bar: 'bar',
127
+ },
128
+ excerpt: '# Hello, world\n',
129
+ },
130
+ ];
131
+
132
+ describe('@mdit-vue/plugin-frontmatter > frontmatter-plugin', () => {
133
+ testCases.forEach(
134
+ ({ source, options, env: rawEnv, frontmatter, excerpt }, i) =>
135
+ it(`case ${i}`, () => {
136
+ const md = MarkdownIt().use(frontmatterPlugin, options);
137
+ const env: MarkdownItEnv = { ...rawEnv };
138
+ md.render(source, env);
139
+ expect(env.frontmatter).toEqual(frontmatter);
140
+ expect(env.excerpt).toEqual(excerpt);
141
+ }),
142
+ );
143
+ });