marked 7.0.5 → 8.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/bin/main.js ADDED
@@ -0,0 +1,270 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * Marked CLI
5
+ * Copyright (c) 2011-2013, Christopher Jeffrey (MIT License)
6
+ */
7
+
8
+ import { promises } from 'node:fs';
9
+ import { dirname, resolve } from 'node:path';
10
+ import { homedir } from 'node:os';
11
+ import { createRequire } from 'node:module';
12
+ import { marked } from '../lib/marked.esm.js';
13
+
14
+ const { access, readFile, writeFile } = promises;
15
+ const require = createRequire(import.meta.url);
16
+
17
+ /**
18
+ * @param {Process} nodeProcess inject process so it can be mocked in tests.
19
+ */
20
+ export async function main(nodeProcess) {
21
+ /**
22
+ * Man Page
23
+ */
24
+ async function help() {
25
+ const { spawn } = await import('child_process');
26
+ const { fileURLToPath } = await import('url');
27
+
28
+ const options = {
29
+ cwd: nodeProcess.cwd(),
30
+ env: nodeProcess.env,
31
+ stdio: 'inherit'
32
+ };
33
+
34
+ const __dirname = dirname(fileURLToPath(import.meta.url));
35
+ const helpText = await readFile(resolve(__dirname, '../man/marked.1.txt'), 'utf8');
36
+
37
+ // eslint-disable-next-line promise/param-names
38
+ await new Promise(res => {
39
+ spawn('man', [resolve(__dirname, '../man/marked.1')], options)
40
+ .on('error', () => {
41
+ console.log(helpText);
42
+ })
43
+ .on('close', res);
44
+ });
45
+ }
46
+
47
+ async function version() {
48
+ const pkg = require('../package.json');
49
+ console.log(pkg.version);
50
+ }
51
+
52
+ /**
53
+ * Main
54
+ */
55
+ async function start(argv) {
56
+ const files = [];
57
+ const options = {};
58
+ let input;
59
+ let output;
60
+ let string;
61
+ let arg;
62
+ let tokens;
63
+ let config;
64
+ let opt;
65
+
66
+ function getArg() {
67
+ let arg = argv.shift();
68
+
69
+ if (arg.indexOf('--') === 0) {
70
+ // e.g. --opt
71
+ arg = arg.split('=');
72
+ if (arg.length > 1) {
73
+ // e.g. --opt=val
74
+ argv.unshift(arg.slice(1).join('='));
75
+ }
76
+ arg = arg[0];
77
+ } else if (arg[0] === '-') {
78
+ if (arg.length > 2) {
79
+ // e.g. -abc
80
+ argv = arg.substring(1).split('').map(function(ch) {
81
+ return '-' + ch;
82
+ }).concat(argv);
83
+ arg = argv.shift();
84
+ } else {
85
+ // e.g. -a
86
+ }
87
+ } else {
88
+ // e.g. foo
89
+ }
90
+
91
+ return arg;
92
+ }
93
+
94
+ while (argv.length) {
95
+ arg = getArg();
96
+ switch (arg) {
97
+ case '-o':
98
+ case '--output':
99
+ output = argv.shift();
100
+ break;
101
+ case '-i':
102
+ case '--input':
103
+ input = argv.shift();
104
+ break;
105
+ case '-s':
106
+ case '--string':
107
+ string = argv.shift();
108
+ break;
109
+ case '-t':
110
+ case '--tokens':
111
+ tokens = true;
112
+ break;
113
+ case '-c':
114
+ case '--config':
115
+ config = argv.shift();
116
+ break;
117
+ case '-h':
118
+ case '--help':
119
+ return await help();
120
+ case '-v':
121
+ case '--version':
122
+ return await version();
123
+ default:
124
+ if (arg.indexOf('--') === 0) {
125
+ opt = camelize(arg.replace(/^--(no-)?/, ''));
126
+ if (!marked.defaults.hasOwnProperty(opt)) {
127
+ continue;
128
+ }
129
+ if (arg.indexOf('--no-') === 0) {
130
+ options[opt] = typeof marked.defaults[opt] !== 'boolean'
131
+ ? null
132
+ : false;
133
+ } else {
134
+ options[opt] = typeof marked.defaults[opt] !== 'boolean'
135
+ ? argv.shift()
136
+ : true;
137
+ }
138
+ } else {
139
+ files.push(arg);
140
+ }
141
+ break;
142
+ }
143
+ }
144
+
145
+ async function getData() {
146
+ if (!input) {
147
+ if (files.length <= 2) {
148
+ if (string) {
149
+ return string;
150
+ }
151
+ return await getStdin();
152
+ }
153
+ input = files.pop();
154
+ }
155
+ return await readFile(input, 'utf8');
156
+ }
157
+
158
+ function resolveFile(file) {
159
+ return resolve(file.replace(/^~/, homedir));
160
+ }
161
+
162
+ function fileExists(file) {
163
+ return access(resolveFile(file)).then(() => true, () => false);
164
+ }
165
+
166
+ async function runConfig(file) {
167
+ const configFile = resolveFile(file);
168
+ let markedConfig;
169
+ try {
170
+ // try require for json
171
+ markedConfig = require(configFile);
172
+ } catch (err) {
173
+ if (err.code !== 'ERR_REQUIRE_ESM') {
174
+ throw err;
175
+ }
176
+ // must import esm
177
+ markedConfig = await import('file:///' + configFile);
178
+ }
179
+
180
+ if (markedConfig.default) {
181
+ markedConfig = markedConfig.default;
182
+ }
183
+
184
+ if (typeof markedConfig === 'function') {
185
+ markedConfig(marked);
186
+ } else {
187
+ marked.use(markedConfig);
188
+ }
189
+ }
190
+
191
+ const data = await getData();
192
+
193
+ if (config) {
194
+ if (!await fileExists(config)) {
195
+ throw Error(`Cannot load config file '${config}'`);
196
+ }
197
+
198
+ await runConfig(config);
199
+ } else {
200
+ const defaultConfig = [
201
+ '~/.marked.json',
202
+ '~/.marked.js',
203
+ '~/.marked/index.js'
204
+ ];
205
+
206
+ for (const configFile of defaultConfig) {
207
+ if (await fileExists(configFile)) {
208
+ await runConfig(configFile);
209
+ break;
210
+ }
211
+ }
212
+ }
213
+
214
+ const html = tokens
215
+ ? JSON.stringify(marked.lexer(data, options), null, 2)
216
+ : await marked.parse(data, options);
217
+
218
+ if (output) {
219
+ return await writeFile(output, html);
220
+ }
221
+
222
+ nodeProcess.stdout.write(html + '\n');
223
+ }
224
+
225
+ /**
226
+ * Helpers
227
+ */
228
+ function getStdin() {
229
+ return new Promise((resolve, reject) => {
230
+ const stdin = nodeProcess.stdin;
231
+ let buff = '';
232
+
233
+ stdin.setEncoding('utf8');
234
+
235
+ stdin.on('data', function(data) {
236
+ buff += data;
237
+ });
238
+
239
+ stdin.on('error', function(err) {
240
+ reject(err);
241
+ });
242
+
243
+ stdin.on('end', function() {
244
+ resolve(buff);
245
+ });
246
+
247
+ stdin.resume();
248
+ });
249
+ }
250
+
251
+ /**
252
+ * @param {string} text
253
+ */
254
+ function camelize(text) {
255
+ return text.replace(/(\w)-(\w)/g, function(_, a, b) {
256
+ return a + b.toUpperCase();
257
+ });
258
+ }
259
+
260
+ try {
261
+ await start(nodeProcess.argv.slice());
262
+ nodeProcess.exit(0);
263
+ } catch (err) {
264
+ if (err.code === 'ENOENT') {
265
+ nodeProcess.stderr.write('marked: output to ' + err.path + ': No such directory');
266
+ }
267
+ nodeProcess.stderr.write(err);
268
+ return nodeProcess.exit(1);
269
+ }
270
+ }
package/bin/marked.js CHANGED
@@ -5,213 +5,11 @@
5
5
  * Copyright (c) 2011-2013, Christopher Jeffrey (MIT License)
6
6
  */
7
7
 
8
- import { promises } from 'fs';
9
- import { marked } from '../lib/marked.esm.js';
10
-
11
- const { readFile, writeFile } = promises;
12
-
13
- /**
14
- * Man Page
15
- */
16
-
17
- async function help() {
18
- const { spawn } = await import('child_process');
19
-
20
- const options = {
21
- cwd: process.cwd(),
22
- env: process.env,
23
- setsid: false,
24
- stdio: 'inherit'
25
- };
26
-
27
- const { dirname, resolve } = await import('path');
28
- const { fileURLToPath } = await import('url');
29
- const __dirname = dirname(fileURLToPath(import.meta.url));
30
- const helpText = await readFile(resolve(__dirname, '../man/marked.1.txt'), 'utf8');
31
-
32
- // eslint-disable-next-line promise/param-names
33
- await new Promise(res => {
34
- spawn('man', [resolve(__dirname, '../man/marked.1')], options)
35
- .on('error', () => {
36
- console.log(helpText);
37
- })
38
- .on('close', res);
39
- });
40
- }
41
-
42
- async function version() {
43
- const { createRequire } = await import('module');
44
- const require = createRequire(import.meta.url);
45
- const pkg = require('../package.json');
46
- console.log(pkg.version);
47
- }
48
-
49
- /**
50
- * Main
51
- */
52
-
53
- async function main(argv) {
54
- const files = [];
55
- const options = {};
56
- let input;
57
- let output;
58
- let string;
59
- let arg;
60
- let tokens;
61
- let opt;
62
-
63
- function getarg() {
64
- let arg = argv.shift();
65
-
66
- if (arg.indexOf('--') === 0) {
67
- // e.g. --opt
68
- arg = arg.split('=');
69
- if (arg.length > 1) {
70
- // e.g. --opt=val
71
- argv.unshift(arg.slice(1).join('='));
72
- }
73
- arg = arg[0];
74
- } else if (arg[0] === '-') {
75
- if (arg.length > 2) {
76
- // e.g. -abc
77
- argv = arg.substring(1).split('').map(function(ch) {
78
- return '-' + ch;
79
- }).concat(argv);
80
- arg = argv.shift();
81
- } else {
82
- // e.g. -a
83
- }
84
- } else {
85
- // e.g. foo
86
- }
87
-
88
- return arg;
89
- }
90
-
91
- while (argv.length) {
92
- arg = getarg();
93
- switch (arg) {
94
- case '-o':
95
- case '--output':
96
- output = argv.shift();
97
- break;
98
- case '-i':
99
- case '--input':
100
- input = argv.shift();
101
- break;
102
- case '-s':
103
- case '--string':
104
- string = argv.shift();
105
- break;
106
- case '-t':
107
- case '--tokens':
108
- tokens = true;
109
- break;
110
- case '-h':
111
- case '--help':
112
- return await help();
113
- case '-v':
114
- case '--version':
115
- return await version();
116
- default:
117
- if (arg.indexOf('--') === 0) {
118
- opt = camelize(arg.replace(/^--(no-)?/, ''));
119
- if (!marked.defaults.hasOwnProperty(opt)) {
120
- continue;
121
- }
122
- if (arg.indexOf('--no-') === 0) {
123
- options[opt] = typeof marked.defaults[opt] !== 'boolean'
124
- ? null
125
- : false;
126
- } else {
127
- options[opt] = typeof marked.defaults[opt] !== 'boolean'
128
- ? argv.shift()
129
- : true;
130
- }
131
- } else {
132
- files.push(arg);
133
- }
134
- break;
135
- }
136
- }
137
-
138
- async function getData() {
139
- if (!input) {
140
- if (files.length <= 2) {
141
- if (string) {
142
- return string;
143
- }
144
- return await getStdin();
145
- }
146
- input = files.pop();
147
- }
148
- return await readFile(input, 'utf8');
149
- }
150
-
151
- const data = await getData();
152
-
153
- const html = tokens
154
- ? JSON.stringify(marked.lexer(data, options), null, 2)
155
- : marked(data, options);
156
-
157
- if (output) {
158
- return await writeFile(output, html);
159
- }
160
-
161
- process.stdout.write(html + '\n');
162
- }
163
-
164
- /**
165
- * Helpers
166
- */
167
-
168
- function getStdin() {
169
- return new Promise((resolve, reject) => {
170
- const stdin = process.stdin;
171
- let buff = '';
172
-
173
- stdin.setEncoding('utf8');
174
-
175
- stdin.on('data', function(data) {
176
- buff += data;
177
- });
178
-
179
- stdin.on('error', function(err) {
180
- reject(err);
181
- });
182
-
183
- stdin.on('end', function() {
184
- resolve(buff);
185
- });
186
-
187
- stdin.resume();
188
- });
189
- }
190
-
191
- /**
192
- * @param {string} text
193
- */
194
- function camelize(text) {
195
- return text.replace(/(\w)-(\w)/g, function(_, a, b) {
196
- return a + b.toUpperCase();
197
- });
198
- }
199
-
200
- function handleError(err) {
201
- if (err.code === 'ENOENT') {
202
- console.error('marked: output to ' + err.path + ': No such directory');
203
- return process.exit(1);
204
- }
205
- throw err;
206
- }
8
+ import { main } from './main.js';
207
9
 
208
10
  /**
209
11
  * Expose / Entry Point
210
12
  */
211
13
 
212
14
  process.title = 'marked';
213
- main(process.argv.slice()).then(code => {
214
- process.exit(code || 0);
215
- }).catch(err => {
216
- handleError(err);
217
- });
15
+ main(process);