19
19
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20
20
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
21
22
- ' use strict' ;
22
+ " use strict" ;
23
23
24
- const fs = require ( 'fs' ) ;
25
- const path = require ( ' path' ) ;
26
- const unified = require ( ' unified' ) ;
27
- const markdown = require ( ' remark-parse' ) ;
28
- const remark2rehype = require ( ' remark-rehype' ) ;
29
- const raw = require ( ' rehype-raw' ) ;
30
- const htmlStringify = require ( ' rehype-stringify' ) ;
24
+ const fs = require ( "fs" ) ;
25
+ const path = require ( " path" ) ;
26
+ const unified = require ( " unified" ) ;
27
+ const markdown = require ( " remark-parse" ) ;
28
+ const remark2rehype = require ( " remark-rehype" ) ;
29
+ const raw = require ( " rehype-raw" ) ;
30
+ const htmlStringify = require ( " rehype-stringify" ) ;
31
31
32
- const html = require ( ' ./html' ) ;
33
- const json = require ( ' ./json' ) ;
32
+ const html = require ( " ./html" ) ;
33
+ const json = require ( " ./json" ) ;
34
34
35
35
// Parse the args.
36
36
// Don't use nopt or whatever for this. It's simple enough.
@@ -41,16 +41,16 @@ let nodeVersion = null;
41
41
let outputDir = null ;
42
42
let apilinks = { } ;
43
43
44
- args . forEach ( ( arg ) => {
45
- if ( ! arg . startsWith ( '--' ) ) {
44
+ args . forEach ( arg => {
45
+ if ( ! arg . startsWith ( "--" ) ) {
46
46
filename = arg ;
47
- } else if ( arg . startsWith ( ' --node-version=' ) ) {
48
- nodeVersion = arg . replace ( / ^ - - n o d e - v e r s i o n = / , '' ) ;
49
- } else if ( arg . startsWith ( ' --output-directory=' ) ) {
50
- outputDir = arg . replace ( / ^ - - o u t p u t - d i r e c t o r y = / , '' ) ;
51
- } else if ( arg . startsWith ( ' --apilinks=' ) ) {
52
- const linkFile = arg . replace ( / ^ - - a p i l i n k s = / , '' ) ;
53
- const data = fs . readFileSync ( linkFile , ' utf8' ) ;
47
+ } else if ( arg . startsWith ( " --node-version=" ) ) {
48
+ nodeVersion = arg . replace ( / ^ - - n o d e - v e r s i o n = / , "" ) ;
49
+ } else if ( arg . startsWith ( " --output-directory=" ) ) {
50
+ outputDir = arg . replace ( / ^ - - o u t p u t - d i r e c t o r y = / , "" ) ;
51
+ } else if ( arg . startsWith ( " --apilinks=" ) ) {
52
+ const linkFile = arg . replace ( / ^ - - a p i l i n k s = / , "" ) ;
53
+ const data = fs . readFileSync ( linkFile , " utf8" ) ;
54
54
if ( ! data . trim ( ) ) {
55
55
throw new Error ( `${ linkFile } is empty` ) ;
56
56
}
@@ -61,33 +61,37 @@ args.forEach((arg) => {
61
61
nodeVersion = nodeVersion || process . version ;
62
62
63
63
if ( ! filename ) {
64
- throw new Error ( ' No input file specified' ) ;
64
+ throw new Error ( " No input file specified" ) ;
65
65
} else if ( ! outputDir ) {
66
- throw new Error ( ' No output directory specified' ) ;
66
+ throw new Error ( " No output directory specified" ) ;
67
67
}
68
68
69
+ const fileTask = async ( ) => {
70
+ try {
71
+ const input = await fs . readFile ( filename , "utf8" ) ;
72
+ const content = unified ( )
73
+ . use ( markdown )
74
+ . use ( html . preprocessText )
75
+ . use ( json . jsonAPI , { filename } )
76
+ . use ( html . firstHeader )
77
+ . use ( html . preprocessElements , { filename } )
78
+ . use ( html . buildToc , { filename, apilinks } )
79
+ . use ( remark2rehype , { allowDangerousHTML : true } )
80
+ . use ( raw )
81
+ . use ( htmlStringify )
82
+ . process ( input ) ;
69
83
70
- fs . readFile ( filename , 'utf8' , async ( er , input ) => {
71
- if ( er ) throw er ;
72
-
73
- const content = unified ( )
74
- . use ( markdown )
75
- . use ( html . preprocessText )
76
- . use ( json . jsonAPI , { filename } )
77
- . use ( html . firstHeader )
78
- . use ( html . preprocessElements , { filename } )
79
- . use ( html . buildToc , { filename, apilinks } )
80
- . use ( remark2rehype , { allowDangerousHTML : true } )
81
- . use ( raw )
82
- . use ( htmlStringify )
83
- . processSync ( input ) ;
84
-
85
- const basename = path . basename ( filename , '.md' ) ;
86
-
87
- const myHtml = await html . toHTML ( { input, content, filename, nodeVersion } ) ;
88
- const htmlTarget = path . join ( outputDir , `${ basename } .html` ) ;
89
- fs . writeFileSync ( htmlTarget , myHtml ) ;
90
-
91
- const jsonTarget = path . join ( outputDir , `${ basename } .json` ) ;
92
- fs . writeFileSync ( jsonTarget , JSON . stringify ( content . json , null , 2 ) ) ;
93
- } ) ;
84
+ const basename = path . basename ( filename , ".md" ) ;
85
+ const myHtml = await html . toHTML ( { input, content, filename, nodeVersion } ) ;
86
+ const htmlTarget = path . join ( outputDir , `${ basename } .html` ) ;
87
+ const data = fs . writeFile ( htmlTarget , myHtml ) ;
88
+ const jsonTarget = path . join ( outputDir , `${ basename } .json` ) ;
89
+ const data_ = fs . writeFile (
90
+ jsonTarget ,
91
+ JSON . stringify ( content . json , null , 2 )
92
+ ) ;
93
+ } catch ( err ) {
94
+ console . error ( err ) ;
95
+ }
96
+ } ;
97
+ fileTask ( ) ;
0 commit comments