marked 3.0.5 → 4.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
@@ -69,7 +69,7 @@ $ cat hello.html
69
69
  <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
70
70
  <script>
71
71
  document.getElementById('content').innerHTML =
72
- marked('# Marked in the browser\n\nRendered by **marked**.');
72
+ marked.parse('# Marked in the browser\n\nRendered by **marked**.');
73
73
  </script>
74
74
  </body>
75
75
  </html>
@@ -5,16 +5,17 @@
5
5
  * Copyright (c) 2011-2013, Christopher Jeffrey (MIT License)
6
6
  */
7
7
 
8
- const fs = require('fs'),
9
- path = require('path'),
10
- marked = require('../');
8
+ import { promises } from 'fs';
9
+ import { marked } from '../lib/marked.esm.js';
10
+
11
+ const { readFile, writeFile } = promises;
11
12
 
12
13
  /**
13
14
  * Man Page
14
15
  */
15
16
 
16
- function help() {
17
- const spawn = require('child_process').spawn;
17
+ async function help() {
18
+ const { spawn } = await import('child_process');
18
19
 
19
20
  const options = {
20
21
  cwd: process.cwd(),
@@ -23,16 +24,18 @@ function help() {
23
24
  stdio: 'inherit'
24
25
  };
25
26
 
26
- spawn('man', [path.resolve(__dirname, '../man/marked.1')], options)
27
- .on('error', function() {
28
- fs.readFile(path.resolve(__dirname, '../man/marked.1.txt'), 'utf8', function(err, data) {
29
- if (err) throw err;
30
- console.log(data);
31
- });
27
+ const { dirname, resolve } = await import('path');
28
+ const { fileURLToPath } = await import('url');
29
+ const __dirname = dirname(fileURLToPath(import.meta.url));
30
+ spawn('man', [resolve(__dirname, '../man/marked.1')], options)
31
+ .on('error', async() => {
32
+ console.log(await readFile(resolve(__dirname, '../man/marked.1.txt'), 'utf8'));
32
33
  });
33
34
  }
34
35
 
35
- function version() {
36
+ async function version() {
37
+ const { createRequire } = await import('module');
38
+ const require = createRequire(import.meta.url);
36
39
  const pkg = require('../package.json');
37
40
  console.log(pkg.version);
38
41
  }
@@ -41,15 +44,15 @@ function version() {
41
44
  * Main
42
45
  */
43
46
 
44
- function main(argv, callback) {
45
- const files = [],
46
- options = {};
47
- let input,
48
- output,
49
- string,
50
- arg,
51
- tokens,
52
- opt;
47
+ async function main(argv) {
48
+ const files = [];
49
+ const options = {};
50
+ let input;
51
+ let output;
52
+ let string;
53
+ let arg;
54
+ let tokens;
55
+ let opt;
53
56
 
54
57
  function getarg() {
55
58
  let arg = argv.shift();
@@ -82,8 +85,6 @@ function main(argv, callback) {
82
85
  while (argv.length) {
83
86
  arg = getarg();
84
87
  switch (arg) {
85
- case '--test':
86
- return require('../test').main(process.argv.slice());
87
88
  case '-o':
88
89
  case '--output':
89
90
  output = argv.shift();
@@ -102,10 +103,10 @@ function main(argv, callback) {
102
103
  break;
103
104
  case '-h':
104
105
  case '--help':
105
- return help();
106
+ return await help();
106
107
  case '-v':
107
108
  case '--version':
108
- return version();
109
+ return await version();
109
110
  default:
110
111
  if (arg.indexOf('--') === 0) {
111
112
  opt = camelize(arg.replace(/^--(no-)?/, ''));
@@ -128,62 +129,57 @@ function main(argv, callback) {
128
129
  }
129
130
  }
130
131
 
131
- function getData(callback) {
132
+ async function getData() {
132
133
  if (!input) {
133
134
  if (files.length <= 2) {
134
135
  if (string) {
135
- return callback(null, string);
136
+ return string;
136
137
  }
137
- return getStdin(callback);
138
+ return await getStdin();
138
139
  }
139
140
  input = files.pop();
140
141
  }
141
- return fs.readFile(input, 'utf8', callback);
142
+ return await readFile(input, 'utf8');
142
143
  }
143
144
 
144
- return getData(function(err, data) {
145
- if (err) return callback(err);
145
+ const data = await getData();
146
146
 
147
- data = tokens
148
- ? JSON.stringify(marked.lexer(data, options), null, 2)
149
- : marked(data, options);
147
+ const html = tokens
148
+ ? JSON.stringify(marked.lexer(data, options), null, 2)
149
+ : marked(data, options);
150
150
 
151
- if (!output) {
152
- process.stdout.write(data + '\n');
153
- return callback();
154
- }
151
+ if (output) {
152
+ return await writeFile(output, data);
153
+ }
155
154
 
156
- return fs.writeFile(output, data, callback);
157
- });
155
+ process.stdout.write(html + '\n');
158
156
  }
159
157
 
160
158
  /**
161
159
  * Helpers
162
160
  */
163
161
 
164
- function getStdin(callback) {
165
- const stdin = process.stdin;
166
- let buff = '';
162
+ function getStdin() {
163
+ return new Promise((resolve, reject) => {
164
+ const stdin = process.stdin;
165
+ let buff = '';
167
166
 
168
- stdin.setEncoding('utf8');
167
+ stdin.setEncoding('utf8');
169
168
 
170
- stdin.on('data', function(data) {
171
- buff += data;
172
- });
169
+ stdin.on('data', function(data) {
170
+ buff += data;
171
+ });
173
172
 
174
- stdin.on('error', function(err) {
175
- return callback(err);
176
- });
173
+ stdin.on('error', function(err) {
174
+ reject(err);
175
+ });
177
176
 
178
- stdin.on('end', function() {
179
- return callback(null, buff);
180
- });
177
+ stdin.on('end', function() {
178
+ resolve(buff);
179
+ });
181
180
 
182
- try {
183
181
  stdin.resume();
184
- } catch (e) {
185
- callback(e);
186
- }
182
+ });
187
183
  }
188
184
 
189
185
  function camelize(text) {
@@ -204,12 +200,9 @@ function handleError(err) {
204
200
  * Expose / Entry Point
205
201
  */
206
202
 
207
- if (!module.parent) {
208
- process.title = 'marked';
209
- main(process.argv.slice(), function(err, code) {
210
- if (err) return handleError(err);
211
- return process.exit(code || 0);
212
- });
213
- } else {
214
- module.exports = main;
215
- }
203
+ process.title = 'marked';
204
+ main(process.argv.slice()).then(code => {
205
+ process.exit(code || 0);
206
+ }).catch(err => {
207
+ handleError(err);
208
+ });