@mdit-vue/plugin-headers 0.4.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 +10 -0
- package/LICENSE +21 -0
- package/README.md +73 -0
- package/build.config.ts +11 -0
- package/dist/index.cjs +27 -0
- package/dist/index.d.ts +43 -0
- package/dist/index.mjs +23 -0
- package/package.json +51 -0
- package/src/headers-plugin.ts +40 -0
- package/src/index.ts +2 -0
- package/src/types.ts +37 -0
- package/tests/__snapshots__/headers-plugin.spec.ts.snap +223 -0
- package/tests/headers-plugin.spec.ts +149 -0
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.4.0](https://github.com/mdit-vue/mdit-vue/compare/v0.3.1...v0.4.0) (2022-07-02)
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
- **plugin-headers:** add headers plugin ([636aa9b](https://github.com/mdit-vue/mdit-vue/commit/636aa9be80051922ee290d61a5074f7d90d7b793))
|
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,73 @@
|
|
|
1
|
+
# @mdit-vue/plugin-headers
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@mdit-vue/plugin-headers)
|
|
4
|
+
[](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 headers.
|
|
7
|
+
|
|
8
|
+
- Extract all headers info into markdown-it `env.headers`.
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```sh
|
|
13
|
+
npm i @mdit-vue/plugin-headers
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Usage
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import MarkdownIt from 'markdown-it';
|
|
20
|
+
import { headersPlugin } from '@mdit-vue/plugin-headers';
|
|
21
|
+
|
|
22
|
+
const md = MarkdownIt({ html: true }).use(headersPlugin, {
|
|
23
|
+
// options
|
|
24
|
+
});
|
|
25
|
+
const env: MarkdownItEnv = {};
|
|
26
|
+
|
|
27
|
+
const rendered = md.render(
|
|
28
|
+
`\
|
|
29
|
+
# h1
|
|
30
|
+
## h2
|
|
31
|
+
### h3
|
|
32
|
+
`,
|
|
33
|
+
env,
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
console.log(env.headers);
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Options
|
|
40
|
+
|
|
41
|
+
### format
|
|
42
|
+
|
|
43
|
+
- Type: `(str: string) => string`
|
|
44
|
+
|
|
45
|
+
- Details:
|
|
46
|
+
|
|
47
|
+
A function for formatting header title.
|
|
48
|
+
|
|
49
|
+
### level
|
|
50
|
+
|
|
51
|
+
- Type: `number[]`
|
|
52
|
+
|
|
53
|
+
- Default: `[2, 3]`
|
|
54
|
+
|
|
55
|
+
- Details:
|
|
56
|
+
|
|
57
|
+
Heading level that going to be extracted.
|
|
58
|
+
|
|
59
|
+
When using this plugin with [markdown-it-anchor](https://github.com/valeriangalliat/markdown-it-anchor) (recommended), this option should be a subset of markdown-it-anchor's `level` option to ensure the header slugs are existed.
|
|
60
|
+
|
|
61
|
+
### slugify
|
|
62
|
+
|
|
63
|
+
- Type: `(str: string) => string`
|
|
64
|
+
|
|
65
|
+
- Default: `slugify` from `@mdit-vue/shared`
|
|
66
|
+
|
|
67
|
+
- Details:
|
|
68
|
+
|
|
69
|
+
A custom slugification function.
|
|
70
|
+
|
|
71
|
+
The default slugify function comes from [@mdit-vue/shared](https://github.com/mdit-vue/mdit-vue/tree/main/packages/shared) package.
|
|
72
|
+
|
|
73
|
+
When using this plugin with [markdown-it-anchor](https://github.com/valeriangalliat/markdown-it-anchor) (recommended), this option will be ignored because the `id` of the headings have already been determined by markdown-it-anchor's `slugify` option.
|
package/build.config.ts
ADDED
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
const shared = require('@mdit-vue/shared');
|
|
6
|
+
|
|
7
|
+
const headersPlugin = (md, { level = [2, 3], slugify = shared.slugify, format } = {}) => {
|
|
8
|
+
let headers;
|
|
9
|
+
md.core.ruler.push("resolveHeaders", (state) => {
|
|
10
|
+
headers = shared.resolveHeadersFromTokens(state.tokens, {
|
|
11
|
+
level,
|
|
12
|
+
shouldAllowHtml: false,
|
|
13
|
+
shouldEscapeText: false,
|
|
14
|
+
slugify,
|
|
15
|
+
format
|
|
16
|
+
});
|
|
17
|
+
return true;
|
|
18
|
+
});
|
|
19
|
+
const render = md.render.bind(md);
|
|
20
|
+
md.render = (src, env = {}) => {
|
|
21
|
+
const result = render(src, env);
|
|
22
|
+
env.headers = headers;
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
exports.headersPlugin = headersPlugin;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { PluginWithOptions } from 'markdown-it';
|
|
2
|
+
import { MarkdownItHeader } from '@mdit-vue/shared';
|
|
3
|
+
export { MarkdownItHeader } from '@mdit-vue/shared';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Options of @mdit-vue/plugin-headers
|
|
7
|
+
*/
|
|
8
|
+
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
|
+
* A function for formatting header title
|
|
18
|
+
*/
|
|
19
|
+
format?: (str: string) => string;
|
|
20
|
+
/**
|
|
21
|
+
* Heading level that going to be extracted
|
|
22
|
+
*
|
|
23
|
+
* Should be a subset of markdown-it-anchor's `level` option
|
|
24
|
+
* to ensure the slug is existed
|
|
25
|
+
*
|
|
26
|
+
* @default [2,3]
|
|
27
|
+
*/
|
|
28
|
+
level?: number[];
|
|
29
|
+
}
|
|
30
|
+
declare module '@mdit-vue/shared' {
|
|
31
|
+
interface MarkdownItEnv {
|
|
32
|
+
headers?: MarkdownItHeader[];
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Get markdown headers info
|
|
38
|
+
*
|
|
39
|
+
* Extract them to env
|
|
40
|
+
*/
|
|
41
|
+
declare const headersPlugin: PluginWithOptions<HeadersPluginOptions>;
|
|
42
|
+
|
|
43
|
+
export { HeadersPluginOptions, headersPlugin };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { resolveHeadersFromTokens, slugify } from '@mdit-vue/shared';
|
|
2
|
+
|
|
3
|
+
const headersPlugin = (md, { level = [2, 3], slugify: slugify$1 = slugify, format } = {}) => {
|
|
4
|
+
let headers;
|
|
5
|
+
md.core.ruler.push("resolveHeaders", (state) => {
|
|
6
|
+
headers = resolveHeadersFromTokens(state.tokens, {
|
|
7
|
+
level,
|
|
8
|
+
shouldAllowHtml: false,
|
|
9
|
+
shouldEscapeText: false,
|
|
10
|
+
slugify: slugify$1,
|
|
11
|
+
format
|
|
12
|
+
});
|
|
13
|
+
return true;
|
|
14
|
+
});
|
|
15
|
+
const render = md.render.bind(md);
|
|
16
|
+
md.render = (src, env = {}) => {
|
|
17
|
+
const result = render(src, env);
|
|
18
|
+
env.headers = headers;
|
|
19
|
+
return result;
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export { headersPlugin };
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mdit-vue/plugin-headers",
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"description": "A markdown-it plugin to get markdown headers",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"markdown-it",
|
|
7
|
+
"markdown-it-plugin",
|
|
8
|
+
"vue",
|
|
9
|
+
"header",
|
|
10
|
+
"heading"
|
|
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.4.0",
|
|
38
|
+
"@types/markdown-it": "^12.2.3",
|
|
39
|
+
"markdown-it": "^13.0.1"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"c8": "^7.11.3",
|
|
43
|
+
"markdown-it-anchor": "^8.6.4",
|
|
44
|
+
"unbuild": "^0.7.4",
|
|
45
|
+
"vitest": "^0.16.0"
|
|
46
|
+
},
|
|
47
|
+
"publishConfig": {
|
|
48
|
+
"access": "public"
|
|
49
|
+
},
|
|
50
|
+
"gitHead": "744887e6f4733af8da2045625e72a476947cfc25"
|
|
51
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import {
|
|
2
|
+
slugify as defaultSlugify,
|
|
3
|
+
resolveHeadersFromTokens,
|
|
4
|
+
} from '@mdit-vue/shared';
|
|
5
|
+
import type { MarkdownItEnv, MarkdownItHeader } from '@mdit-vue/shared';
|
|
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
ADDED
package/src/types.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { MarkdownItHeader } from '@mdit-vue/shared';
|
|
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/shared' {
|
|
34
|
+
interface MarkdownItEnv {
|
|
35
|
+
headers?: MarkdownItHeader[];
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,223 @@
|
|
|
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
|
+
`;
|
|
@@ -0,0 +1,149 @@
|
|
|
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
|
+
});
|