@mdit-vue/plugin-headers 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
@@ -18,6 +18,7 @@ npm i @mdit-vue/plugin-headers
18
18
  ```ts
19
19
  import MarkdownIt from 'markdown-it';
20
20
  import { headersPlugin } from '@mdit-vue/plugin-headers';
21
+ import type { MarkdownItEnv } from '@mdit-vue/types';
21
22
 
22
23
  const md = MarkdownIt({ html: true }).use(headersPlugin, {
23
24
  // options
package/dist/index.d.ts CHANGED
@@ -29,6 +29,9 @@ interface HeadersPluginOptions {
29
29
  }
30
30
  declare module '@mdit-vue/types' {
31
31
  interface MarkdownItEnv {
32
+ /**
33
+ * The headers that extracted by `@mdit-vue/plugin-headers`
34
+ */
32
35
  headers?: MarkdownItHeader[];
33
36
  }
34
37
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mdit-vue/plugin-headers",
3
- "version": "0.6.0",
3
+ "version": "0.8.0",
4
4
  "description": "A markdown-it plugin to get markdown headers",
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,24 +30,27 @@
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/shared": "0.6.0",
38
- "@mdit-vue/types": "0.6.0",
41
+ "@mdit-vue/shared": "0.8.0",
42
+ "@mdit-vue/types": "0.8.0",
39
43
  "@types/markdown-it": "^12.2.3",
40
44
  "markdown-it": "^13.0.1"
41
45
  },
42
46
  "devDependencies": {
43
- "c8": "^7.11.3",
47
+ "c8": "^7.12.0",
44
48
  "markdown-it-anchor": "^8.6.4",
45
- "unbuild": "^0.7.4",
46
- "vitest": "^0.18.0"
49
+ "unbuild": "^0.7.6",
50
+ "vitest": "^0.18.1"
47
51
  },
48
52
  "publishConfig": {
49
53
  "access": "public"
50
54
  },
51
- "gitHead": "654a3a4d52f4fb9711bf5f7ba3674ff58ec663bf"
55
+ "gitHead": "7fd357a18b8bcc347cf17e2e78da3b3ed4242e58"
52
56
  }
package/CHANGELOG.md DELETED
@@ -1,20 +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
- **Note:** Version bump only for package @mdit-vue/plugin-headers
15
-
16
- # [0.4.0](https://github.com/mdit-vue/mdit-vue/compare/v0.3.1...v0.4.0) (2022-07-02)
17
-
18
- ### Features
19
-
20
- - **plugin-headers:** add headers plugin ([636aa9b](https://github.com/mdit-vue/mdit-vue/commit/636aa9be80051922ee290d61a5074f7d90d7b793))
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,40 +0,0 @@
1
- import {
2
- slugify as defaultSlugify,
3
- resolveHeadersFromTokens,
4
- } from '@mdit-vue/shared';
5
- import type { MarkdownItEnv, MarkdownItHeader } from '@mdit-vue/types';
6
- import type { PluginWithOptions } from 'markdown-it';
7
- import type { HeadersPluginOptions } from './types';
8
-
9
- /**
10
- * Get markdown headers info
11
- *
12
- * Extract them to env
13
- */
14
- export const headersPlugin: PluginWithOptions<HeadersPluginOptions> = (
15
- md,
16
- { level = [2, 3], slugify = defaultSlugify, format } = {},
17
- ): void => {
18
- let headers: MarkdownItHeader[];
19
-
20
- // push the rule to the end of the chain
21
- // resolve headers from the parsed tokens
22
- md.core.ruler.push('resolveHeaders', (state) => {
23
- headers = resolveHeadersFromTokens(state.tokens, {
24
- level,
25
- shouldAllowHtml: false,
26
- shouldEscapeText: false,
27
- slugify,
28
- format,
29
- });
30
- return true;
31
- });
32
-
33
- // extract headers to env
34
- const render = md.render.bind(md);
35
- md.render = (src, env: MarkdownItEnv = {}) => {
36
- const result = render(src, env);
37
- env.headers = headers;
38
- return result;
39
- };
40
- };
package/src/index.ts DELETED
@@ -1,2 +0,0 @@
1
- export * from './headers-plugin';
2
- export * from './types';
package/src/types.ts DELETED
@@ -1,37 +0,0 @@
1
- import type { MarkdownItHeader } from '@mdit-vue/types';
2
-
3
- export type { MarkdownItHeader };
4
-
5
- /**
6
- * Options of @mdit-vue/plugin-headers
7
- */
8
- export interface HeadersPluginOptions {
9
- /**
10
- * A custom slugification function
11
- *
12
- * Should use the same slugify function with markdown-it-anchor
13
- * to ensure the link is matched
14
- */
15
- slugify?: (str: string) => string;
16
-
17
- /**
18
- * A function for formatting header title
19
- */
20
- format?: (str: string) => string;
21
-
22
- /**
23
- * Heading level that going to be extracted
24
- *
25
- * Should be a subset of markdown-it-anchor's `level` option
26
- * to ensure the slug is existed
27
- *
28
- * @default [2,3]
29
- */
30
- level?: number[];
31
- }
32
-
33
- declare module '@mdit-vue/types' {
34
- interface MarkdownItEnv {
35
- headers?: MarkdownItHeader[];
36
- }
37
- }
@@ -1,223 +0,0 @@
1
- // Vitest Snapshot v1
2
-
3
- exports[`@mdit-vue/plugin-headers > headers-plugin > should extract headers (h1, h2, h3, h4) > complexTree 1`] = `
4
- [
5
- {
6
- "children": [
7
- {
8
- "children": [
9
- {
10
- "children": [
11
- {
12
- "children": [],
13
- "level": 4,
14
- "slug": "s1-1-1-1",
15
- "title": "s1-1-1-1",
16
- },
17
- ],
18
- "level": 3,
19
- "slug": "s1-1-1",
20
- "title": "s1-1-1",
21
- },
22
- {
23
- "children": [],
24
- "level": 3,
25
- "slug": "s1-1-2",
26
- "title": "s1-1-2",
27
- },
28
- {
29
- "children": [
30
- {
31
- "children": [],
32
- "level": 4,
33
- "slug": "s1-1-3-1",
34
- "title": "s1-1-3-1",
35
- },
36
- ],
37
- "level": 3,
38
- "slug": "s1-1-3",
39
- "title": "s1-1-3",
40
- },
41
- ],
42
- "level": 2,
43
- "slug": "s1-1",
44
- "title": "s1-1",
45
- },
46
- {
47
- "children": [],
48
- "level": 2,
49
- "slug": "s1-2",
50
- "title": "s1-2",
51
- },
52
- {
53
- "children": [
54
- {
55
- "children": [
56
- {
57
- "children": [],
58
- "level": 4,
59
- "slug": "s1-3-2-1",
60
- "title": "s1-3-2-1",
61
- },
62
- ],
63
- "level": 3,
64
- "slug": "s1-3-2",
65
- "title": "s1-3-2",
66
- },
67
- ],
68
- "level": 2,
69
- "slug": "s1-3",
70
- "title": "s1-3",
71
- },
72
- ],
73
- "level": 1,
74
- "slug": "s1",
75
- "title": "s1",
76
- },
77
- ]
78
- `;
79
-
80
- exports[`@mdit-vue/plugin-headers > headers-plugin > should extract headers (h1, h2, h3, h4) > reversedTree 1`] = `
81
- [
82
- {
83
- "children": [],
84
- "level": 4,
85
- "slug": "h4",
86
- "title": "h4",
87
- },
88
- {
89
- "children": [],
90
- "level": 3,
91
- "slug": "h3",
92
- "title": "h3",
93
- },
94
- {
95
- "children": [],
96
- "level": 2,
97
- "slug": "h2",
98
- "title": "h2",
99
- },
100
- {
101
- "children": [],
102
- "level": 1,
103
- "slug": "h1",
104
- "title": "h1",
105
- },
106
- ]
107
- `;
108
-
109
- exports[`@mdit-vue/plugin-headers > headers-plugin > should extract headers (h1, h2, h3, h4) > simpleTree 1`] = `
110
- [
111
- {
112
- "children": [
113
- {
114
- "children": [
115
- {
116
- "children": [
117
- {
118
- "children": [],
119
- "level": 4,
120
- "slug": "h4",
121
- "title": "h4",
122
- },
123
- ],
124
- "level": 3,
125
- "slug": "h3",
126
- "title": "h3",
127
- },
128
- ],
129
- "level": 2,
130
- "slug": "h2",
131
- "title": "h2",
132
- },
133
- ],
134
- "level": 1,
135
- "slug": "h1",
136
- "title": "h1",
137
- },
138
- ]
139
- `;
140
-
141
- exports[`@mdit-vue/plugin-headers > headers-plugin > should extract headers with default option (h2, h3) > complexTree 1`] = `
142
- [
143
- {
144
- "children": [
145
- {
146
- "children": [],
147
- "level": 3,
148
- "slug": "s1-1-1",
149
- "title": "s1-1-1",
150
- },
151
- {
152
- "children": [],
153
- "level": 3,
154
- "slug": "s1-1-2",
155
- "title": "s1-1-2",
156
- },
157
- {
158
- "children": [],
159
- "level": 3,
160
- "slug": "s1-1-3",
161
- "title": "s1-1-3",
162
- },
163
- ],
164
- "level": 2,
165
- "slug": "s1-1",
166
- "title": "s1-1",
167
- },
168
- {
169
- "children": [],
170
- "level": 2,
171
- "slug": "s1-2",
172
- "title": "s1-2",
173
- },
174
- {
175
- "children": [
176
- {
177
- "children": [],
178
- "level": 3,
179
- "slug": "s1-3-2",
180
- "title": "s1-3-2",
181
- },
182
- ],
183
- "level": 2,
184
- "slug": "s1-3",
185
- "title": "s1-3",
186
- },
187
- ]
188
- `;
189
-
190
- exports[`@mdit-vue/plugin-headers > headers-plugin > should extract headers with default option (h2, h3) > reversedTree 1`] = `
191
- [
192
- {
193
- "children": [],
194
- "level": 3,
195
- "slug": "h3",
196
- "title": "h3",
197
- },
198
- {
199
- "children": [],
200
- "level": 2,
201
- "slug": "h2",
202
- "title": "h2",
203
- },
204
- ]
205
- `;
206
-
207
- exports[`@mdit-vue/plugin-headers > headers-plugin > should extract headers with default option (h2, h3) > simpleTree 1`] = `
208
- [
209
- {
210
- "children": [
211
- {
212
- "children": [],
213
- "level": 3,
214
- "slug": "h3",
215
- "title": "h3",
216
- },
217
- ],
218
- "level": 2,
219
- "slug": "h2",
220
- "title": "h2",
221
- },
222
- ]
223
- `;
@@ -1,149 +0,0 @@
1
- import { slugify } from '@mdit-vue/shared';
2
- import type { MarkdownItEnv, MarkdownItHeader } from '@mdit-vue/shared';
3
- import MarkdownIt from 'markdown-it';
4
- import anchorPlugin from 'markdown-it-anchor';
5
- import { describe, expect, it } from 'vitest';
6
- import { headersPlugin } from '../src';
7
-
8
- const fixtures = {
9
- simpleTree: `\
10
- # h1
11
- ## h2
12
- ### h3
13
- #### h4
14
- ##### h5
15
- ###### h6
16
- `,
17
- complexTree: `\
18
- # s1
19
- ## s1-1
20
- ### s1-1-1
21
- #### s1-1-1-1
22
- ### s1-1-2
23
- ### s1-1-3
24
- #### s1-1-3-1
25
- ## s1-2
26
- ## s1-3
27
- ### s1-3-2
28
- #### s1-3-2-1
29
- ##### s1-3-2-1-1
30
- ##### s1-3-2-1-2
31
- `,
32
- reversedTree: `\
33
- ###### h6
34
- ##### h5
35
- #### h4
36
- ### h3
37
- ## h2
38
- # h1
39
- `,
40
- };
41
-
42
- describe('@mdit-vue/plugin-headers > headers-plugin', () => {
43
- describe('should extract headers with default option (h2, h3)', () => {
44
- const md = MarkdownIt().use(headersPlugin);
45
-
46
- Object.entries(fixtures).forEach(([name, source]) => {
47
- it(name, () => {
48
- const env: MarkdownItEnv = {};
49
- md.render(source, env);
50
- expect(env.headers).toMatchSnapshot();
51
- });
52
- });
53
- });
54
-
55
- describe('should extract nothing', () => {
56
- const md = MarkdownIt().use(headersPlugin, {
57
- level: [],
58
- });
59
-
60
- Object.entries(fixtures).forEach(([name, source]) => {
61
- it(name, () => {
62
- const env: MarkdownItEnv = {};
63
- md.render(source, env);
64
- expect(env.headers).toEqual([]);
65
- });
66
- });
67
- });
68
-
69
- describe('should extract headers (h1, h2, h3, h4)', () => {
70
- const md = MarkdownIt().use(headersPlugin, {
71
- level: [1, 2, 3, 4],
72
- });
73
-
74
- Object.entries(fixtures).forEach(([name, source]) => {
75
- it(name, () => {
76
- const env: MarkdownItEnv = {};
77
- md.render(source, env);
78
- expect(env.headers).toMatchSnapshot();
79
- });
80
- });
81
- });
82
-
83
- describe('should not include html elements and should not escape texts', () => {
84
- const md = MarkdownIt({
85
- html: true,
86
- })
87
- .use(anchorPlugin, { slugify })
88
- .use(headersPlugin, { slugify });
89
-
90
- const testCases: [string, MarkdownItHeader[]][] = [
91
- // html element should be ignored
92
- [
93
- '## foo <bar />',
94
- [
95
- {
96
- level: 2,
97
- title: 'foo',
98
- slug: 'foo',
99
- children: [],
100
- },
101
- ],
102
- ],
103
- // inline code should not be escaped
104
- [
105
- '## foo <bar/> `<code />`',
106
- [
107
- {
108
- level: 2,
109
- title: 'foo <code />',
110
- slug: 'foo-code',
111
- children: [],
112
- },
113
- ],
114
- ],
115
- // text should not be escaped
116
- [
117
- '## foo <bar/> "baz"',
118
- [
119
- {
120
- level: 2,
121
- title: 'foo "baz"',
122
- slug: 'foo-baz',
123
- children: [],
124
- },
125
- ],
126
- ],
127
- // text should not be escaped
128
- [
129
- '## < test >',
130
- [
131
- {
132
- level: 2,
133
- title: '< test >',
134
- slug: 'test',
135
- children: [],
136
- },
137
- ],
138
- ],
139
- ];
140
-
141
- testCases.forEach(([source, expected], i) =>
142
- it(`case ${i}`, () => {
143
- const env: MarkdownItEnv = {};
144
- md.render(source, env);
145
- expect(env.headers).toEqual(expected);
146
- }),
147
- );
148
- });
149
- });