@@ -14,8 +14,10 @@ function parseList(list, level = 0) {
1414 s += parseList ( item , level + 1 )
1515 } else {
1616 for ( let itemChild of item . children ) {
17- if ( itemChild . tag == 'p' ) s += '\t' . repeat ( level ) + '- ' + itemChild . text + ' \n'
18- if ( itemChild . tag == 'ul' ) s += parseList ( itemChild , level + 1 )
17+ if ( itemChild . tag == 'p' && itemChild . text . length > 0 )
18+ s += '\t' . repeat ( level ) + '- ' + itemChild . text . trim ( ) + '\n'
19+ if ( itemChild . tag == 'ul' )
20+ s += parseList ( itemChild , level + 1 )
1921 }
2022 }
2123 }
@@ -29,9 +31,12 @@ const TableHeaders = [
2931 "_**Federation ranks**_" ,
3032 "_**Empire ranks**_" ,
3133 "_**CQC ranks**_" ,
34+ "_**Military Ranks**_" ,
35+ "_**Exobiologist Ranks**_" ,
3236]
3337
3438const ListHeaders = [
39+ "Star Descriptions" ,
3540 "Planet Classes" ,
3641 "Atmosphere Classes" ,
3742 "Volcanism classes" ,
@@ -48,6 +53,19 @@ const ListHeaders2 = [
4853]
4954
5055let currentHeader
56+ let isListHeader = false
57+
58+ function createTable ( header , text ) {
59+ let rows = [
60+ header ,
61+ '|---:|---|' ,
62+ ]
63+ for ( let row of text . split ( / ? , / ) ) {
64+ let s = row . split ( / \s * = \s * / )
65+ rows . push ( `|${ s [ 0 ] . trim ( ) } |${ s [ 1 ] . replace ( / ^ ' ( .+ ) ' $ / , '$1' ) } |` )
66+ }
67+ return rows . join ( '\n' )
68+ }
5169
5270for ( let element of data . children ) {
5371 if ( docs . length == 0 && element . tag != 'h1' ) continue
@@ -65,66 +83,59 @@ for (let element of data.children) {
6583 if ( ! text ) continue
6684 if ( text . startsWith ( 'Example: {' ) && text . endsWith ( '}' ) ) {
6785 text = text . replace ( / ^ E x a m p l e : / , '' )
68- section . paragraphs . push ( { text : "Example:" } )
86+ section . paragraphs . push ( { text : "Example:" } )
6987 }
7088 if ( text . startsWith ( '{' ) && text . endsWith ( '}' ) ) {
7189 let code = text
7290 try {
7391 code = JSON . stringify ( JSON . parse ( text ) , null , '\t' )
7492 } catch ( e ) { }
75- text = '```\n' + code + '\n```'
93+ text = '```json \n' + code + '\n```'
7694 }
7795 if ( TableHeaders . some ( e => text . startsWith ( e ) ) ) {
78- text = text . replace ( / ^ [ ^ \: ] + : / , "$&\n\nIndex|Rank\n-:|\n" )
79- text = text . replace ( / , * / g, "\n" )
80- text = text . replace ( / = * / g, "|" )
81- section . paragraphs . push ( {
82- text : text ,
83- } )
96+ let matches = text . match ( / ^ ( [ ^ \: ] + : ) ( .+ ) / )
97+ text = matches [ 1 ]
98+ text += '\n\n'
99+ text += createTable ( '|Index|Rank|' , matches [ 2 ] )
100+ section . paragraphs . push ( { text } )
84101 } else if ( currentHeader == 'Engineer IDs' ) {
85- text = text . replace ( / \d + / g, "\n$&|" )
86- text = "ID|Name\n-:|" + text
87- section . paragraphs . push ( {
88- text : text ,
89- } )
102+ text = text . replace ( / \d + / g, ",$&=" ) . replace ( / ^ , / , '' )
103+ text = createTable ( '|ID|Name|' , text )
104+ section . paragraphs . push ( { text } )
90105 } else if ( ListHeaders . includes ( currentHeader ) ) {
91106 let paragraph = {
92- text : '- ' + text . replace ( / , \s * $ / , '' ) ,
107+ text : '- ' + text . replace ( / , \s * $ / , '' ) , noNewLine : true ,
93108 }
109+ isListHeader = true
94110 section . paragraphs . push ( paragraph )
95111 } else if ( ListHeaders2 . some ( e => text . startsWith ( e ) ) ) {
96112 text = text . replace ( / ^ ( [ ^ \: ] + : ) \s * / , "$1\n\n- " )
97113 text = text . replace ( / , * / g, "\n- " )
98- section . paragraphs . push ( {
99- text : text ,
100- } )
114+ section . paragraphs . push ( { text } )
101115 } else {
102- let paragraph = {
103- text : text ,
104- }
105- section . paragraphs . push ( paragraph )
116+ section . paragraphs . push ( { text } )
106117 }
107118 }
119+
108120 if ( element . tag == 'h2' ) {
121+ if ( isListHeader )
122+ section . paragraphs [ section . paragraphs . length - 1 ] . noNewLine = false
123+ isListHeader = false
124+ currentHeader = element . text . replace ( / [ \d \. ] + \s * / , '' ) . trim ( )
109125 let paragraph = {
110- text : '## ' + element . text . replace ( / [ \d \. ] + \s * / , '' ) ,
126+ text : '## ' + currentHeader ,
111127 }
112128 section . paragraphs . push ( paragraph )
113- currentHeader = paragraph . text . substring ( 4 )
114129 }
130+
115131 if ( element . tag == 'ul' ) {
116132 let text = parseList ( element )
117- let paragraph = {
118- text : text ,
119- }
120- section . paragraphs . push ( paragraph )
133+ section . paragraphs . push ( { text : text . trimEnd ( ) } )
121134 }
135+
122136 if ( element . tag == 'div' ) {
123- let text = element . children . reduce ( ( acc , cur ) => acc + '- ' + cur . text . replace ( / , / , '' ) + ' \n' , '' )
124- let paragraph = {
125- text : text ,
126- }
127- section . paragraphs . push ( paragraph )
137+ let text = element . children . reduce ( ( acc , cur ) => acc + '- ' + cur . text . replace ( / , / , '' ) + '\n' , '' )
138+ section . paragraphs . push ( { text : text . trimEnd ( ) } )
128139 }
129140}
130141
@@ -143,7 +154,7 @@ for (let section of docs) {
143154 // })
144155 // paragraph.text = paragraph.text.map(e => e)
145156 }
146- content += section . paragraphs . map ( e => e . text ) . join ( '\n \n' )
157+ content += section . paragraphs . map ( e => e . text + ( e . noNewLine ? '' : '\n' ) ) . join ( '\n' )
147158 if ( title == 'Introduction' ) title = 'index'
148159 fs . writeFileSync ( path . resolve ( 'docs' , `${ title . replace ( / " / g, '' ) } .md` ) , content )
149160}
0 commit comments