1- var fs = require ( 'fs' ) ;
1+ import * as fs from 'fs' ;
2+ const filePath = './src/README.md' ;
23// JSON.stringify
34
45function line ( char : string , times : number ) {
@@ -13,37 +14,56 @@ var regex = {
1314} ;
1415
1516function parseWithCode ( code : string , content : string ) {
16- return regex [ code ] . exec ( content ) [ 1 ] ;
17- }
18-
19- fs . readFile ( './src/README.md' , 'utf8' , function ( err , data ) {
20- if ( err ) {
21- console . log ( err ) ;
17+ if ( ! content ) {
18+ return false ;
19+ }
20+ if ( content . match ( regex [ code ] ) ) {
21+ return regex [ code ] . exec ( content ) [ 1 ] ;
22+ } else {
23+ return false ;
2224 }
23- console . log ( data ) ;
24- // resolve(data.split('\n'));
25- } ) ;
25+ }
2626
2727function build ( ) {
2828 var result = {
2929 project : { } ,
3030 chapters : [ ]
3131 } ;
3232
33- // load data from file into array
34-
33+ var lines = fs . readFileSync ( filePath , 'utf8' ) ;
34+ var arrayOfLines = lines . split ( '\n' ) ;
35+ var chapterCount = 0 ;
36+ var pageCount = 0 ;
37+
38+ for ( var i = 0 ; i < arrayOfLines . length ; i ++ ) {
39+ var projectTitleMatch = parseWithCode ( '#' , arrayOfLines [ i ] )
40+ if ( projectTitleMatch ) {
41+ // project.title
42+ result . project . title = projectTitleMatch ;
43+ result . project . description = '' ;
44+ for ( var j = i + 1 ; j < arrayOfLines . length ; j ++ ) {
45+ var chapterTitleMatch = parseWithCode ( '##' , arrayOfLines [ j ] ) ;
46+ if ( ! chapterTitleMatch ) {
47+ // project.description
48+ result . project . description . concat ( arrayOfLines [ j ] ) ;
49+ } else {
50+ result . chapters . push ( {
51+ title : chapterTitleMatch
52+ } ) ;
53+
54+ }
55+ }
56+ }
57+ }
3558
36- // data.forEach(function(line) {
37- // console.log(line);
38- // })
3959
40- // result.project.title = parseWithCode('#', content);
60+ // = parseWithCode('#', content);
4161 // result.project.description = '';
4262
4363 return result ;
4464}
4565
46- build ( ) ;
66+ console . log ( build ( ) ) ;
4767
4868// #
4969
0 commit comments