@putout/processor-markdown 6.0.0 → 7.0.2

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,16 @@
1
- 'use strict';
1
+ import {
2
+ toJS,
3
+ fromJS,
4
+ } from '@putout/processor-json';
5
+ import stringify from 'remark-stringify';
6
+ import preset from 'remark-preset-lint-consistent';
2
7
 
3
- const once = require('once');
8
+ import {run} from './rules/index.mjs';
9
+ import {visit} from 'unist-util-visit';
10
+ import {unified} from 'unified';
4
11
 
5
- const {toPlace} = require('./parse-place');
6
- const {initParseStore} = require('./parse-store');
12
+ import {toPlace} from './parse-place.js';
13
+ import {initParseStore} from './parse-store.js';
7
14
 
8
15
  const parseStore = initParseStore();
9
16
 
@@ -18,36 +25,11 @@ const stringifyOptions = {
18
25
  },
19
26
  };
20
27
 
21
- module.exports.files = [
28
+ export const files = [
22
29
  '*.md',
23
30
  ];
24
31
 
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
-
32
+ export const find = async (rawSource) => {
51
33
  await parseStore.init();
52
34
 
53
35
  const {messages} = await unified()
@@ -61,14 +43,7 @@ module.exports.find = async (rawSource) => {
61
43
  .map(toPlace);
62
44
  };
63
45
 
64
- module.exports.fix = async (rawSource) => {
65
- const {
66
- unified,
67
- stringify,
68
- preset,
69
- run,
70
- } = await loadDependencies();
71
-
46
+ export const fix = async (rawSource) => {
72
47
  await parseStore.init();
73
48
 
74
49
  const {value} = await unified()
@@ -81,14 +56,7 @@ module.exports.fix = async (rawSource) => {
81
56
  return value;
82
57
  };
83
58
 
84
- module.exports.branch = async (rawSource) => {
85
- const {
86
- unified,
87
- stringify,
88
- visit,
89
- jsonProcessor,
90
- } = await loadDependencies();
91
-
59
+ export const branch = async (rawSource) => {
92
60
  const list = [];
93
61
 
94
62
  await unified()
@@ -96,7 +64,6 @@ module.exports.branch = async (rawSource) => {
96
64
  .use(collect, {
97
65
  list,
98
66
  visit,
99
- jsonProcessor,
100
67
  })
101
68
  .use(stringify)
102
69
  .process(rawSource);
@@ -104,14 +71,7 @@ module.exports.branch = async (rawSource) => {
104
71
  return list;
105
72
  };
106
73
 
107
- module.exports.merge = async (rawSource, list) => {
108
- const {
109
- unified,
110
- stringify,
111
- visit,
112
- jsonProcessor,
113
- } = await loadDependencies();
114
-
74
+ export const merge = async (rawSource, list) => {
115
75
  const newList = list.slice();
116
76
 
117
77
  const {value} = await unified()
@@ -120,7 +80,6 @@ module.exports.merge = async (rawSource, list) => {
120
80
  list: newList,
121
81
  rawSource,
122
82
  visit,
123
- jsonProcessor,
124
83
  })
125
84
  .use(stringify, stringifyOptions)
126
85
  .process(rawSource);
@@ -130,70 +89,61 @@ module.exports.merge = async (rawSource, list) => {
130
89
  return value;
131
90
  };
132
91
 
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;
92
+ const collect = ({list, visit}) => (node) => {
93
+ visit(node, 'code', (node) => {
94
+ const {lang, value} = node;
95
+ const startLine = node.position.start.line;
96
+
97
+ if (/^(js|javascript)$/.test(lang)) {
98
+ list.push({
99
+ startLine,
100
+ source: value,
101
+ extension: 'js',
102
+ });
140
103
 
141
- if (/^(js|javascript)$/.test(lang)) {
142
- list.push({
143
- startLine,
144
- source: value,
145
- extension: 'js',
146
- });
147
-
148
- return;
149
- }
104
+ return;
105
+ }
106
+
107
+ if (/^(ts|typescript)$/.test(lang)) {
108
+ list.push({
109
+ startLine,
110
+ source: value,
111
+ extension: 'ts',
112
+ });
150
113
 
151
- if (/^(ts|typescript)$/.test(lang)) {
152
- list.push({
153
- startLine,
154
- source: value,
155
- extension: 'ts',
156
- });
157
-
158
- return;
159
- }
114
+ return;
115
+ }
116
+
117
+ if (lang === 'json') {
118
+ const source = toJS(value);
160
119
 
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
- };
120
+ list.push({
121
+ startLine,
122
+ source,
123
+ extension: 'json',
124
+ });
125
+ }
126
+ });
172
127
  };
173
128
 
174
- const apply = ({list, visit, jsonProcessor}) => (node) => {
129
+ const apply = ({list, visit}) => (node) => {
175
130
  visit(node, 'code', (node) => {
176
131
  const {lang} = node;
177
132
 
178
133
  if (/^(js|javascript)$/.test(lang)) {
179
- const source = list.shift();
180
-
181
- node.value = source;
134
+ node.value = list.shift();
182
135
  return;
183
136
  }
184
137
 
185
138
  if (/^(ts|typescript)$/.test(lang)) {
186
- const source = list.shift();
187
-
188
- node.value = source;
139
+ node.value = list.shift();
189
140
  return;
190
141
  }
191
142
 
192
143
  if (lang === 'json') {
193
144
  const code = list.shift();
194
- const source = jsonProcessor.fromJS(code);
195
145
 
196
- node.value = source;
146
+ node.value = fromJS(code);
197
147
  }
198
148
  });
199
149
  };
@@ -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
 
@@ -6,7 +6,7 @@ const fix = (heading, tree) => {
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;
@@ -20,7 +20,7 @@ const traverse = (tree, {push}) => {
20
20
 
21
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": "6.0.0",
4
- "type": "commonjs",
3
+ "version": "7.0.2",
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,
@@ -24,7 +24,7 @@
24
24
  "report": "madrun report"
25
25
  },
26
26
  "dependencies": {
27
- "@putout/processor-json": "^3.0.0",
27
+ "@putout/processor-json": "^5.0.0",
28
28
  "once": "^1.4.0",
29
29
  "remark-parse": "^10.0.0",
30
30
  "remark-preset-lint-consistent": "^5.0.0",
@@ -39,19 +39,19 @@
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
51
  "supertape": "^7.0.0"
52
52
  },
53
53
  "peerDependencies": {
54
- "putout": ">=25"
54
+ "putout": ">=26"
55
55
  },
56
56
  "engines": {
57
57
  "node": ">=16"