@putout/processor-markdown 5.5.0 → 7.0.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
|
@@ -3,7 +3,11 @@
|
|
|
3
3
|
[NPMIMGURL]: https://img.shields.io/npm/v/@putout/processor-markdown.svg?style=flat&longCache=true
|
|
4
4
|
[NPMURL]: https://npmjs.org/package/@putout/processor-markdown "npm"
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
> **Markdown**, the simple and easy-to-use markup language you can use to format virtually any document.
|
|
7
|
+
>
|
|
8
|
+
> (c) [markdownguide.org](https://www.markdownguide.org/)
|
|
9
|
+
|
|
10
|
+
🐊[**Putout**](https://github.com/coderaiser/putout) processor adds ability to get **JavaScript**, **JSON** and **TypeScript** code from markdown files.
|
|
7
11
|
|
|
8
12
|
## Install
|
|
9
13
|
|
package/lib/markdown.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
|
|
1
|
+
import jsonProcessor from '@putout/processor-json';
|
|
2
|
+
import stringify from 'remark-stringify';
|
|
3
|
+
import preset from 'remark-preset-lint-consistent';
|
|
2
4
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
+
import {run} from './rules/index.mjs';
|
|
6
|
+
import {visit} from 'unist-util-visit';
|
|
7
|
+
import {unified} from 'unified';
|
|
5
8
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const simport = createSimport(__filename);
|
|
9
|
+
import {toPlace} from './parse-place.js';
|
|
10
|
+
import {initParseStore} from './parse-store.js';
|
|
10
11
|
|
|
11
12
|
const parseStore = initParseStore();
|
|
12
13
|
|
|
@@ -21,44 +22,11 @@ const stringifyOptions = {
|
|
|
21
22
|
},
|
|
22
23
|
};
|
|
23
24
|
|
|
24
|
-
|
|
25
|
+
export const files = [
|
|
25
26
|
'*.md',
|
|
26
27
|
];
|
|
27
28
|
|
|
28
|
-
const
|
|
29
|
-
const stringify = await simport('remark-stringify');
|
|
30
|
-
const preset = await simport('remark-preset-lint-consistent');
|
|
31
|
-
const jsonProcessor = await simport('@putout/processor-json');
|
|
32
|
-
const {run} = await simport('./rules/index.mjs');
|
|
33
|
-
|
|
34
|
-
// Fix: TypeError: visit is not a function
|
|
35
|
-
//
|
|
36
|
-
// remark installs "uninst-util-visit" v2 using npm and has `module.exports = visit`
|
|
37
|
-
// but v3 has `export const visit = () => {}`
|
|
38
|
-
// and simport imports nearest version of "uninst-util-visit" which is v2
|
|
39
|
-
// that's why regular import should be used here while remark not transit to v3
|
|
40
|
-
const {visit} = await import('unist-util-visit');
|
|
41
|
-
// sometimes cannot load unified with simport on node v14
|
|
42
|
-
const {unified} = await import('unified');
|
|
43
|
-
|
|
44
|
-
return {
|
|
45
|
-
unified,
|
|
46
|
-
stringify,
|
|
47
|
-
visit,
|
|
48
|
-
preset,
|
|
49
|
-
jsonProcessor,
|
|
50
|
-
run,
|
|
51
|
-
};
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
module.exports.find = async (rawSource) => {
|
|
55
|
-
const {
|
|
56
|
-
unified,
|
|
57
|
-
stringify,
|
|
58
|
-
preset,
|
|
59
|
-
run,
|
|
60
|
-
} = await loadDependencies();
|
|
61
|
-
|
|
29
|
+
export const find = async (rawSource) => {
|
|
62
30
|
await parseStore.init();
|
|
63
31
|
|
|
64
32
|
const {messages} = await unified()
|
|
@@ -72,14 +40,7 @@ module.exports.find = async (rawSource) => {
|
|
|
72
40
|
.map(toPlace);
|
|
73
41
|
};
|
|
74
42
|
|
|
75
|
-
|
|
76
|
-
const {
|
|
77
|
-
unified,
|
|
78
|
-
stringify,
|
|
79
|
-
preset,
|
|
80
|
-
run,
|
|
81
|
-
} = await loadDependencies();
|
|
82
|
-
|
|
43
|
+
export const fix = async (rawSource) => {
|
|
83
44
|
await parseStore.init();
|
|
84
45
|
|
|
85
46
|
const {value} = await unified()
|
|
@@ -92,14 +53,7 @@ module.exports.fix = async (rawSource) => {
|
|
|
92
53
|
return value;
|
|
93
54
|
};
|
|
94
55
|
|
|
95
|
-
|
|
96
|
-
const {
|
|
97
|
-
unified,
|
|
98
|
-
stringify,
|
|
99
|
-
visit,
|
|
100
|
-
jsonProcessor,
|
|
101
|
-
} = await loadDependencies();
|
|
102
|
-
|
|
56
|
+
export const branch = async (rawSource) => {
|
|
103
57
|
const list = [];
|
|
104
58
|
|
|
105
59
|
await unified()
|
|
@@ -107,7 +61,6 @@ module.exports.branch = async (rawSource) => {
|
|
|
107
61
|
.use(collect, {
|
|
108
62
|
list,
|
|
109
63
|
visit,
|
|
110
|
-
jsonProcessor,
|
|
111
64
|
})
|
|
112
65
|
.use(stringify)
|
|
113
66
|
.process(rawSource);
|
|
@@ -115,14 +68,7 @@ module.exports.branch = async (rawSource) => {
|
|
|
115
68
|
return list;
|
|
116
69
|
};
|
|
117
70
|
|
|
118
|
-
|
|
119
|
-
const {
|
|
120
|
-
unified,
|
|
121
|
-
stringify,
|
|
122
|
-
visit,
|
|
123
|
-
jsonProcessor,
|
|
124
|
-
} = await loadDependencies();
|
|
125
|
-
|
|
71
|
+
export const merge = async (rawSource, list) => {
|
|
126
72
|
const newList = list.slice();
|
|
127
73
|
|
|
128
74
|
const {value} = await unified()
|
|
@@ -131,7 +77,6 @@ module.exports.merge = async (rawSource, list) => {
|
|
|
131
77
|
list: newList,
|
|
132
78
|
rawSource,
|
|
133
79
|
visit,
|
|
134
|
-
jsonProcessor,
|
|
135
80
|
})
|
|
136
81
|
.use(stringify, stringifyOptions)
|
|
137
82
|
.process(rawSource);
|
|
@@ -141,70 +86,61 @@ module.exports.merge = async (rawSource, list) => {
|
|
|
141
86
|
return value;
|
|
142
87
|
};
|
|
143
88
|
|
|
144
|
-
const collect = ({list, visit}) => {
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
89
|
+
const collect = ({list, visit}) => (node) => {
|
|
90
|
+
visit(node, 'code', (node) => {
|
|
91
|
+
const {lang, value} = node;
|
|
92
|
+
const startLine = node.position.start.line;
|
|
93
|
+
|
|
94
|
+
if (/^(js|javascript)$/.test(lang)) {
|
|
95
|
+
list.push({
|
|
96
|
+
startLine,
|
|
97
|
+
source: value,
|
|
98
|
+
extension: 'js',
|
|
99
|
+
});
|
|
151
100
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
}
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (/^(ts|typescript)$/.test(lang)) {
|
|
105
|
+
list.push({
|
|
106
|
+
startLine,
|
|
107
|
+
source: value,
|
|
108
|
+
extension: 'ts',
|
|
109
|
+
});
|
|
161
110
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
});
|
|
168
|
-
|
|
169
|
-
return;
|
|
170
|
-
}
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (lang === 'json') {
|
|
115
|
+
const source = jsonProcessor.toJS(value);
|
|
171
116
|
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
});
|
|
180
|
-
}
|
|
181
|
-
});
|
|
182
|
-
};
|
|
117
|
+
list.push({
|
|
118
|
+
startLine,
|
|
119
|
+
source,
|
|
120
|
+
extension: 'json',
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
});
|
|
183
124
|
};
|
|
184
125
|
|
|
185
|
-
const apply = ({list, visit
|
|
126
|
+
const apply = ({list, visit}) => (node) => {
|
|
186
127
|
visit(node, 'code', (node) => {
|
|
187
128
|
const {lang} = node;
|
|
188
129
|
|
|
189
130
|
if (/^(js|javascript)$/.test(lang)) {
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
node.value = source;
|
|
131
|
+
node.value = list.shift();
|
|
193
132
|
return;
|
|
194
133
|
}
|
|
195
134
|
|
|
196
135
|
if (/^(ts|typescript)$/.test(lang)) {
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
node.value = source;
|
|
136
|
+
node.value = list.shift();
|
|
200
137
|
return;
|
|
201
138
|
}
|
|
202
139
|
|
|
203
140
|
if (lang === 'json') {
|
|
204
141
|
const code = list.shift();
|
|
205
|
-
const source = jsonProcessor.fromJS(code);
|
|
206
142
|
|
|
207
|
-
node.value =
|
|
143
|
+
node.value = jsonProcessor.fromJS(code);
|
|
208
144
|
}
|
|
209
145
|
});
|
|
210
146
|
};
|
package/lib/parse-place.js
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
module.exports.toPlace = function toPlace({reason, line, column, source, ruleId}) {
|
|
1
|
+
export function toPlace({reason, line, column, source, ruleId}) {
|
|
4
2
|
const {message, rule} = parseWatermark({
|
|
5
3
|
reason,
|
|
6
4
|
ruleId,
|
|
@@ -15,7 +13,7 @@ module.exports.toPlace = function toPlace({reason, line, column, source, ruleId}
|
|
|
15
13
|
column,
|
|
16
14
|
},
|
|
17
15
|
};
|
|
18
|
-
}
|
|
16
|
+
}
|
|
19
17
|
|
|
20
18
|
function parseWatermark({reason, ruleId, source}) {
|
|
21
19
|
const [watermark, remarkRule, message] = reason.split(': ');
|
package/lib/parse-store.js
CHANGED
|
@@ -1,9 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const {createSimport} = require('simport');
|
|
4
|
-
const simport = createSimport(__filename);
|
|
5
|
-
|
|
6
|
-
module.exports.initParseStore = () => {
|
|
1
|
+
export const initParseStore = () => {
|
|
7
2
|
let cache = null;
|
|
8
3
|
let parse = null;
|
|
9
4
|
|
|
@@ -24,7 +19,7 @@ module.exports.initParseStore = () => {
|
|
|
24
19
|
|
|
25
20
|
fn.init = async () => {
|
|
26
21
|
cache = null;
|
|
27
|
-
parse = await
|
|
22
|
+
({default: parse} = await import('remark-parse'));
|
|
28
23
|
};
|
|
29
24
|
|
|
30
25
|
fn.clear = async () => {
|
|
@@ -2,10 +2,8 @@ const report = () => 'Merge heading spaces';
|
|
|
2
2
|
|
|
3
3
|
const fix = (heading) => {
|
|
4
4
|
const newChildren = [];
|
|
5
|
-
const n = heading.children.length;
|
|
6
5
|
|
|
7
|
-
for (
|
|
8
|
-
const node = heading.children[i];
|
|
6
|
+
for (const [i, node] of heading.children.entries()) {
|
|
9
7
|
const nextNode = heading.children[i + 1];
|
|
10
8
|
|
|
11
9
|
if (bothSpaces(node, nextNode))
|
|
@@ -23,10 +21,7 @@ const traverse = (tree, {push}) => {
|
|
|
23
21
|
if (heading.type !== 'heading')
|
|
24
22
|
return;
|
|
25
23
|
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
for (let i = 0; i < n; i++) {
|
|
29
|
-
const node = heading.children[i];
|
|
24
|
+
for (const [i, node] of heading.children.entries()) {
|
|
30
25
|
const nextNode = heading.children[i + 1];
|
|
31
26
|
|
|
32
27
|
if (bothSpaces(node, nextNode)) {
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
const report = () => 'Avoid trailing whitespaces';
|
|
2
2
|
|
|
3
3
|
const fix = (heading, tree) => {
|
|
4
|
-
const latest = heading.children
|
|
4
|
+
const latest = heading.children.at(-1);
|
|
5
5
|
|
|
6
6
|
if (latest.type === 'text' && latest.value === ' ')
|
|
7
7
|
heading.children = heading.children.slice(0, -1);
|
|
8
8
|
|
|
9
|
-
if (latest.type === 'text' &&
|
|
9
|
+
if (latest.type === 'text' && latest.value.endsWith(' '))
|
|
10
10
|
latest.value = latest.value.slice(0, -1);
|
|
11
11
|
|
|
12
12
|
tree.children[0].children = heading.children;
|
|
@@ -18,9 +18,9 @@ const traverse = (tree, {push}) => {
|
|
|
18
18
|
if (heading.type !== 'heading')
|
|
19
19
|
return;
|
|
20
20
|
|
|
21
|
-
const latest = heading.children
|
|
21
|
+
const latest = heading.children.at(-1);
|
|
22
22
|
|
|
23
|
-
if (latest.type === 'text' &&
|
|
23
|
+
if (latest.type === 'text' && latest.value.endsWith(' '))
|
|
24
24
|
push(heading);
|
|
25
25
|
};
|
|
26
26
|
|
package/package.json
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/processor-markdown",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "7.0.0",
|
|
4
|
+
"type": "module",
|
|
4
5
|
"author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
|
|
5
|
-
"description": "
|
|
6
|
-
"homepage": "
|
|
6
|
+
"description": "🐊Putout processor adds ability to parse markdown files and lint js snippets",
|
|
7
|
+
"homepage": "https://github.com/coderaiser/putout/tree/master/packages/processor-markdown#readme",
|
|
7
8
|
"main": "lib/markdown.js",
|
|
8
9
|
"release": false,
|
|
9
10
|
"tag": false,
|
|
@@ -28,7 +29,6 @@
|
|
|
28
29
|
"remark-parse": "^10.0.0",
|
|
29
30
|
"remark-preset-lint-consistent": "^5.0.0",
|
|
30
31
|
"remark-stringify": "^10.0.0",
|
|
31
|
-
"simport": "^1.2.0",
|
|
32
32
|
"unified": "^10.0.0",
|
|
33
33
|
"unified-lint-rule": "^2.1.0",
|
|
34
34
|
"unist-util-visit": "^4.0.0"
|
|
@@ -39,25 +39,25 @@
|
|
|
39
39
|
"markdown"
|
|
40
40
|
],
|
|
41
41
|
"devDependencies": {
|
|
42
|
-
"@putout/test": "^
|
|
42
|
+
"@putout/test": "^5.0.0",
|
|
43
43
|
"c8": "^7.5.0",
|
|
44
44
|
"eslint": "^8.0.1",
|
|
45
45
|
"eslint-plugin-node": "^11.0.0",
|
|
46
|
-
"eslint-plugin-putout": "^
|
|
46
|
+
"eslint-plugin-putout": "^15.0.0",
|
|
47
47
|
"lerna": "^4.0.0",
|
|
48
|
-
"madrun": "^
|
|
48
|
+
"madrun": "^9.0.0",
|
|
49
49
|
"nodemon": "^2.0.1",
|
|
50
50
|
"putout": "*",
|
|
51
|
-
"supertape": "^
|
|
51
|
+
"supertape": "^7.0.0"
|
|
52
52
|
},
|
|
53
53
|
"peerDependencies": {
|
|
54
|
-
"putout": ">=
|
|
54
|
+
"putout": ">=25"
|
|
55
55
|
},
|
|
56
|
-
"license": "MIT",
|
|
57
56
|
"engines": {
|
|
58
|
-
"node": ">=
|
|
57
|
+
"node": ">=16"
|
|
59
58
|
},
|
|
60
59
|
"publishConfig": {
|
|
61
60
|
"access": "public"
|
|
62
|
-
}
|
|
61
|
+
},
|
|
62
|
+
"license": "MIT"
|
|
63
63
|
}
|