@putout/processor-markdown 5.6.0 → 7.0.1

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
- `putout` processor adds ability to get `js`, `json` and `ts` code from markdown files.
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,9 +1,13 @@
1
- 'use strict';
1
+ import jsonProcessor from '@putout/processor-json';
2
+ import stringify from 'remark-stringify';
3
+ import preset from 'remark-preset-lint-consistent';
2
4
 
3
- const once = require('once');
5
+ import {run} from './rules/index.mjs';
6
+ import {visit} from 'unist-util-visit';
7
+ import {unified} from 'unified';
4
8
 
5
- const {toPlace} = require('./parse-place');
6
- const {initParseStore} = require('./parse-store');
9
+ import {toPlace} from './parse-place.js';
10
+ import {initParseStore} from './parse-store.js';
7
11
 
8
12
  const parseStore = initParseStore();
9
13
 
@@ -18,36 +22,11 @@ const stringifyOptions = {
18
22
  },
19
23
  };
20
24
 
21
- module.exports.files = [
25
+ export const files = [
22
26
  '*.md',
23
27
  ];
24
28
 
25
- const loadDependencies = once(async () => {
26
- const {default: stringify} = await import('remark-stringify');
27
- const {default: preset} = await import('remark-preset-lint-consistent');
28
- const {default: jsonProcessor} = await import('@putout/processor-json');
29
- const {run} = await import('./rules/index.mjs');
30
- const {visit} = await import('unist-util-visit');
31
- const {unified} = await import('unified');
32
-
33
- return {
34
- unified,
35
- stringify,
36
- visit,
37
- preset,
38
- jsonProcessor,
39
- run,
40
- };
41
- });
42
-
43
- module.exports.find = async (rawSource) => {
44
- const {
45
- unified,
46
- stringify,
47
- preset,
48
- run,
49
- } = await loadDependencies();
50
-
29
+ export const find = async (rawSource) => {
51
30
  await parseStore.init();
52
31
 
53
32
  const {messages} = await unified()
@@ -61,14 +40,7 @@ module.exports.find = async (rawSource) => {
61
40
  .map(toPlace);
62
41
  };
63
42
 
64
- module.exports.fix = async (rawSource) => {
65
- const {
66
- unified,
67
- stringify,
68
- preset,
69
- run,
70
- } = await loadDependencies();
71
-
43
+ export const fix = async (rawSource) => {
72
44
  await parseStore.init();
73
45
 
74
46
  const {value} = await unified()
@@ -81,14 +53,7 @@ module.exports.fix = async (rawSource) => {
81
53
  return value;
82
54
  };
83
55
 
84
- module.exports.branch = async (rawSource) => {
85
- const {
86
- unified,
87
- stringify,
88
- visit,
89
- jsonProcessor,
90
- } = await loadDependencies();
91
-
56
+ export const branch = async (rawSource) => {
92
57
  const list = [];
93
58
 
94
59
  await unified()
@@ -96,7 +61,6 @@ module.exports.branch = async (rawSource) => {
96
61
  .use(collect, {
97
62
  list,
98
63
  visit,
99
- jsonProcessor,
100
64
  })
101
65
  .use(stringify)
102
66
  .process(rawSource);
@@ -104,14 +68,7 @@ module.exports.branch = async (rawSource) => {
104
68
  return list;
105
69
  };
106
70
 
107
- module.exports.merge = async (rawSource, list) => {
108
- const {
109
- unified,
110
- stringify,
111
- visit,
112
- jsonProcessor,
113
- } = await loadDependencies();
114
-
71
+ export const merge = async (rawSource, list) => {
115
72
  const newList = list.slice();
116
73
 
117
74
  const {value} = await unified()
@@ -120,7 +77,6 @@ module.exports.merge = async (rawSource, list) => {
120
77
  list: newList,
121
78
  rawSource,
122
79
  visit,
123
- jsonProcessor,
124
80
  })
125
81
  .use(stringify, stringifyOptions)
126
82
  .process(rawSource);
@@ -130,70 +86,61 @@ module.exports.merge = async (rawSource, list) => {
130
86
  return value;
131
87
  };
132
88
 
133
- const collect = ({list, visit}) => {
134
- const jsonProcessor = require('@putout/processor-json');
135
-
136
- return (node) => {
137
- visit(node, 'code', (node) => {
138
- const {lang, value} = node;
139
- const startLine = node.position.start.line;
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
+ });
140
100
 
141
- if (/^(js|javascript)$/.test(lang)) {
142
- list.push({
143
- startLine,
144
- source: value,
145
- extension: 'js',
146
- });
147
-
148
- return;
149
- }
101
+ return;
102
+ }
103
+
104
+ if (/^(ts|typescript)$/.test(lang)) {
105
+ list.push({
106
+ startLine,
107
+ source: value,
108
+ extension: 'ts',
109
+ });
150
110
 
151
- if (/^(ts|typescript)$/.test(lang)) {
152
- list.push({
153
- startLine,
154
- source: value,
155
- extension: 'ts',
156
- });
157
-
158
- return;
159
- }
111
+ return;
112
+ }
113
+
114
+ if (lang === 'json') {
115
+ const source = jsonProcessor.toJS(value);
160
116
 
161
- if (lang === 'json') {
162
- const source = jsonProcessor.toJS(value);
163
-
164
- list.push({
165
- startLine,
166
- source,
167
- extension: 'json',
168
- });
169
- }
170
- });
171
- };
117
+ list.push({
118
+ startLine,
119
+ source,
120
+ extension: 'json',
121
+ });
122
+ }
123
+ });
172
124
  };
173
125
 
174
- const apply = ({list, visit, jsonProcessor}) => (node) => {
126
+ const apply = ({list, visit}) => (node) => {
175
127
  visit(node, 'code', (node) => {
176
128
  const {lang} = node;
177
129
 
178
130
  if (/^(js|javascript)$/.test(lang)) {
179
- const source = list.shift();
180
-
181
- node.value = source;
131
+ node.value = list.shift();
182
132
  return;
183
133
  }
184
134
 
185
135
  if (/^(ts|typescript)$/.test(lang)) {
186
- const source = list.shift();
187
-
188
- node.value = source;
136
+ node.value = list.shift();
189
137
  return;
190
138
  }
191
139
 
192
140
  if (lang === 'json') {
193
141
  const code = list.shift();
194
- const source = jsonProcessor.fromJS(code);
195
142
 
196
- node.value = source;
143
+ node.value = jsonProcessor.fromJS(code);
197
144
  }
198
145
  });
199
146
  };
@@ -1,6 +1,4 @@
1
- 'use strict';
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(': ');
@@ -1,6 +1,4 @@
1
- 'use strict';
2
-
3
- module.exports.initParseStore = () => {
1
+ export const initParseStore = () => {
4
2
  let cache = null;
5
3
  let parse = null;
6
4
 
@@ -1,12 +1,12 @@
1
1
  const report = () => 'Avoid trailing whitespaces';
2
2
 
3
3
  const fix = (heading, tree) => {
4
- const latest = heading.children[heading.children.length - 1];
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' && / $/.test(latest.value))
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[heading.children.length - 1];
21
+ const latest = heading.children.at(-1);
22
22
 
23
- if (latest.type === 'text' && / $/.test(latest.value))
23
+ if (latest.type === 'text' && latest.value.endsWith(' '))
24
24
  push(heading);
25
25
  };
26
26
 
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@putout/processor-markdown",
3
- "version": "5.6.0",
4
- "type": "commonjs",
3
+ "version": "7.0.1",
4
+ "type": "module",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
- "description": "putout processor adds ability to parse markdown files and lint js snippets",
6
+ "description": "🐊Putout processor adds ability to parse markdown files and lint js snippets",
7
7
  "homepage": "https://github.com/coderaiser/putout/tree/master/packages/processor-markdown#readme",
8
8
  "main": "lib/markdown.js",
9
9
  "release": false,
@@ -39,25 +39,25 @@
39
39
  "markdown"
40
40
  ],
41
41
  "devDependencies": {
42
- "@putout/test": "^4.0.0",
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": "^13.0.0",
46
+ "eslint-plugin-putout": "^15.0.0",
47
47
  "lerna": "^4.0.0",
48
- "madrun": "^8.0.1",
48
+ "madrun": "^9.0.0",
49
49
  "nodemon": "^2.0.1",
50
50
  "putout": "*",
51
- "supertape": "^6.10.0"
51
+ "supertape": "^7.0.0"
52
52
  },
53
53
  "peerDependencies": {
54
- "putout": ">=18"
54
+ "putout": ">=26"
55
55
  },
56
- "license": "MIT",
57
56
  "engines": {
58
- "node": ">=14"
57
+ "node": ">=16"
59
58
  },
60
59
  "publishConfig": {
61
60
  "access": "public"
62
- }
61
+ },
62
+ "license": "MIT"
63
63
  }