@mdit-vue/plugin-frontmatter 0.6.0 → 0.8.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/README.md CHANGED
@@ -7,6 +7,7 @@ A [markdown-it](https://github.com/markdown-it/markdown-it) plugin to get markdo
7
7
 
8
8
  - Extract frontmatter into markdown-it `env.frontmatter`.
9
9
  - Allow providing default frontmatter via markdown-it `env.frontmatter`.
10
+ - Extract markdown content without frontmatter into markdown-it `env.content`.
10
11
  - Support extracting rendered excerpt into markdown-it `env.excerpt`.
11
12
 
12
13
  ## Install
@@ -20,6 +21,7 @@ npm i @mdit-vue/plugin-frontmatter
20
21
  ```ts
21
22
  import MarkdownIt from 'markdown-it';
22
23
  import { frontmatterPlugin } from '@mdit-vue/plugin-frontmatter';
24
+ import type { MarkdownItEnv } from '@mdit-vue/types';
23
25
 
24
26
  const md = MarkdownIt({ html: true }).use(frontmatterPlugin, {
25
27
  // options
package/dist/index.cjs CHANGED
@@ -12,6 +12,7 @@ const frontmatterPlugin = (md, { grayMatterOptions, renderExcerpt = true } = {})
12
12
  const render = md.render.bind(md);
13
13
  md.render = (src, env = {}) => {
14
14
  const { data, content, excerpt = "" } = grayMatter__default(src, grayMatterOptions);
15
+ env.content = content;
15
16
  env.frontmatter = {
16
17
  ...env.frontmatter,
17
18
  ...data
package/dist/index.d.ts CHANGED
@@ -14,12 +14,27 @@ interface FrontmatterPluginOptions {
14
14
  grayMatterOptions?: GrayMatterOptions;
15
15
  /**
16
16
  * Render the excerpt or not
17
+ *
18
+ * @default true
17
19
  */
18
20
  renderExcerpt?: boolean;
19
21
  }
20
22
  declare module '@mdit-vue/types' {
21
23
  interface MarkdownItEnv {
24
+ /**
25
+ * The raw Markdown content without frontmatter
26
+ */
27
+ content?: string;
28
+ /**
29
+ * The excerpt that extracted by `@mdit-vue/plugin-frontmatter`
30
+ *
31
+ * - Would be the rendered HTML when `renderExcerpt` is enabled
32
+ * - Would be the raw Markdown when `renderExcerpt` is disabled
33
+ */
22
34
  excerpt?: string;
35
+ /**
36
+ * The frontmatter that extracted by `@mdit-vue/plugin-frontmatter`
37
+ */
23
38
  frontmatter?: Record<string, unknown>;
24
39
  }
25
40
  }
package/dist/index.mjs CHANGED
@@ -4,6 +4,7 @@ const frontmatterPlugin = (md, { grayMatterOptions, renderExcerpt = true } = {})
4
4
  const render = md.render.bind(md);
5
5
  md.render = (src, env = {}) => {
6
6
  const { data, content, excerpt = "" } = grayMatter(src, grayMatterOptions);
7
+ env.content = content;
7
8
  env.frontmatter = {
8
9
  ...env.frontmatter,
9
10
  ...data
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mdit-vue/plugin-frontmatter",
3
- "version": "0.6.0",
3
+ "version": "0.8.0",
4
4
  "description": "A markdown-it plugin to get markdown frontmatter",
5
5
  "keywords": [
6
6
  "markdown-it",
@@ -19,6 +19,7 @@
19
19
  },
20
20
  "license": "MIT",
21
21
  "author": "meteorlxy <meteor.lxy@foxmail.com>",
22
+ "type": "module",
22
23
  "exports": {
23
24
  ".": {
24
25
  "types": "./dist/index.d.ts",
@@ -29,23 +30,26 @@
29
30
  "main": "./dist/index.cjs",
30
31
  "module": "./dist/index.mjs",
31
32
  "types": "./dist/index.d.ts",
33
+ "files": [
34
+ "./dist"
35
+ ],
32
36
  "scripts": {
33
37
  "build": "unbuild",
34
38
  "test": "vitest"
35
39
  },
36
40
  "dependencies": {
37
- "@mdit-vue/types": "0.6.0",
41
+ "@mdit-vue/types": "0.8.0",
38
42
  "@types/markdown-it": "^12.2.3",
39
43
  "gray-matter": "^4.0.3",
40
44
  "markdown-it": "^13.0.1"
41
45
  },
42
46
  "devDependencies": {
43
- "c8": "^7.11.3",
44
- "unbuild": "^0.7.4",
45
- "vitest": "^0.18.0"
47
+ "c8": "^7.12.0",
48
+ "unbuild": "^0.7.6",
49
+ "vitest": "^0.18.1"
46
50
  },
47
51
  "publishConfig": {
48
52
  "access": "public"
49
53
  },
50
- "gitHead": "654a3a4d52f4fb9711bf5f7ba3674ff58ec663bf"
54
+ "gitHead": "7fd357a18b8bcc347cf17e2e78da3b3ed4242e58"
51
55
  }
package/CHANGELOG.md DELETED
@@ -1,16 +0,0 @@
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.6.0](https://github.com/mdit-vue/mdit-vue/compare/v0.5.0...v0.6.0) (2022-07-11)
7
-
8
- ### Features
9
-
10
- - **types:** extract types from shared package ([5fc56ce](https://github.com/mdit-vue/mdit-vue/commit/5fc56ce6439159584e09e13ca9a6e87abe8f4389))
11
-
12
- # [0.5.0](https://github.com/mdit-vue/mdit-vue/compare/v0.4.0...v0.5.0) (2022-07-06)
13
-
14
- ### Features
15
-
16
- - **plugin-frontmatter:** add frontmatter plugin ([bcffc7a](https://github.com/mdit-vue/mdit-vue/commit/bcffc7a7fef0c48a869d703f0358d168524ac19e))
package/build.config.ts DELETED
@@ -1,11 +0,0 @@
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
- });
@@ -1,38 +0,0 @@
1
- import type { MarkdownItEnv } from '@mdit-vue/types';
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 DELETED
@@ -1,2 +0,0 @@
1
- export * from './frontmatter-plugin';
2
- export * from './types';
package/src/types.ts DELETED
@@ -1,27 +0,0 @@
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/types' {
23
- interface MarkdownItEnv {
24
- excerpt?: string;
25
- frontmatter?: Record<string, unknown>;
26
- }
27
- }
@@ -1,143 +0,0 @@
1
- import type { MarkdownItEnv } from '@mdit-vue/types';
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
- });