@@ -4,18 +4,18 @@ var path = require('path');
44var compiler = require ( 'jsdoc-api' ) ;
55var sourceFiles = require ( './source-files' ) ;
66
7- var pkg = JSON . parse ( fs . readFileSync (
8- path . resolve ( __dirname , '../package.json' ) ) ) ;
7+ var pkg = JSON . parse (
8+ fs . readFileSync ( path . resolve ( __dirname , '../package.json' ) )
9+ ) ;
910var directory = [ ] ;
1011
1112var template = _ . template (
12- fs . readFileSync (
13- path . resolve ( __dirname , './docs.template' ) ,
14- { encoding : 'utf8' }
15- )
13+ fs . readFileSync ( path . resolve ( __dirname , './docs.template' ) , {
14+ encoding : 'utf8' ,
15+ } )
1616) ;
1717
18- pkg . template = function ( str ) {
18+ pkg . template = function ( str ) {
1919 if ( typeof str === 'string' ) {
2020 var regex = / \$ \w * / g;
2121 var match = str . match ( regex ) ;
@@ -30,58 +30,52 @@ pkg.template = function(str) {
3030preprocess ( ) ;
3131
3232function preprocess ( ) {
33-
34- _ . each ( sourceFiles , function ( file ) {
35-
33+ _ . each ( sourceFiles , function ( file ) {
3634 var sourceFile = path . resolve ( __dirname , '../' , file ) ;
37- var pivotDir = [ '/docs/' , file . replace ( 'jsm/' , '' )
38- . replace ( 'src/' , '' ) . replace ( '.js' , '/' ) ] . join ( '' ) ;
35+ var pivotDir = [
36+ '/docs/' ,
37+ file . replace ( 'jsm/' , '' ) . replace ( 'src/' , '' ) . replace ( '.js' , '/' ) ,
38+ ] . join ( '' ) ;
3939
4040 var citations = compiler . explainSync ( {
4141 files : sourceFile ,
42- cache : false
42+ cache : false ,
4343 } ) ;
4444
4545 var root = getRoot ( citations ) ;
4646
4747 directory . push ( { name : root . longname , dir : pivotDir } ) ;
48-
4948 } ) ;
5049
5150 process ( ) ;
52-
5351}
5452
5553function process ( ) {
56-
57- _ . each ( sourceFiles , function ( file ) {
58-
54+ _ . each ( sourceFiles , function ( file ) {
5955 var sourceFile = path . resolve ( __dirname , '../' , file ) ;
60- var pivotDir = [ '/docs/' , file . replace ( 'jsm/' , '' )
61- . replace ( 'src/' , '' ) . replace ( '.js' , '/' ) ] . join ( '' ) ;
56+ var pivotDir = [
57+ '/docs/' ,
58+ file . replace ( 'jsm/' , '' ) . replace ( 'src/' , '' ) . replace ( '.js' , '/' ) ,
59+ ] . join ( '' ) ;
6260
6361 var outputDir = path . resolve ( __dirname , '../wiki' + pivotDir ) ;
6462 var outputFile = path . join ( outputDir , '/README.md' ) ;
6563
6664 var citations = compiler . explainSync ( {
6765 files : sourceFile ,
68- cache : false
66+ cache : false ,
6967 } ) ;
7068
71- citations . slice ( 0 ) . forEach ( function ( object ) {
72-
69+ citations . slice ( 0 ) . forEach ( function ( object ) {
7370 var a = object . undocumented ;
7471 var b = / p a c k a g e : u n d e f i n e d / i. test ( object . longname ) ;
7572 var c = / T w o \. U t i l s \. E v e n t s \. ( b i n d | u n b i n d ) / i. test ( object . memberof ) ;
7673 var d = / ( p r i v a t e | p r o t e c t e d ) / i. test ( object . access ) ;
7774
7875 if ( a || b || c || d ) {
79-
8076 // Remove private / hidden / incomplete documented citations
8177 citations . splice ( citations . indexOf ( object ) , 1 ) ;
82-
8378 } else {
84-
8579 expandLink ( object , 'description' ) ;
8680 _ . each ( object . params , expandParam , object ) ;
8781 _ . each ( object . returns , expandParam , object ) ;
@@ -90,11 +84,11 @@ function process() {
9084 object . see = _ . map ( object . see , expandSee , object ) ;
9185
9286 var sn ;
93- sn = object . longname . replace ( / # / ig , '.' ) ;
87+ sn = object . longname . replace ( / # / gi , '.' ) ;
9488 var snList = sn . split ( '.' ) ;
95- var snIndex = ( snList . length > 2 ) ? 2 : 1 ;
89+ var snIndex = snList . length > 2 ? 2 : 1 ;
9690 object . shortname = snList . slice ( snIndex ) . join ( '.' ) ;
97- object . prefixname = sn . replace ( object . shortname , "" ) ;
91+ object . prefixname = sn . replace ( object . shortname , '' ) ;
9892
9993 // name and href for augments property
10094 var an ;
@@ -108,17 +102,15 @@ function process() {
108102 if ( an ) {
109103 object . augmentsHref = getHref ( an . slice ( 1 ) . join ( '.' ) ) ;
110104 }
111-
112105 }
113-
114106 } ) ;
115107
116108 var citationsByScope = {
117109 instance : [ ] ,
118- static : [ ]
110+ static : [ ] ,
119111 } ;
120112
121- _ . each ( citations , function ( citation ) {
113+ _ . each ( citations , function ( citation ) {
122114 if ( / # / i. test ( citation . longname ) ) {
123115 citationsByScope . instance . push ( citation ) ;
124116 } else {
@@ -137,33 +129,43 @@ function process() {
137129 // })
138130 // );
139131
140- fs . mkdirSync ( outputDir + '/' , { recursive : true } ) ;
141- fs . writeFileSync ( outputFile . replace ( 'README.md' , 'docs.json' ) ,
142- JSON . stringify ( citations ) ) ;
132+ // Ensure outputDir is inside the project root
133+ var projectRoot = path . resolve ( __dirname , '../' ) ;
134+ var relative = path . relative ( projectRoot , outputDir ) ;
143135
144- fs . writeFileSync ( outputFile , template ( {
145- root : getRoot ( citations , true ) ,
146- citations : citations ,
147- package : pkg
148- } ) ) ;
136+ if ( relative && ! relative . startsWith ( '..' ) && ! path . isAbsolute ( relative ) ) {
137+ fs . mkdirSync ( outputDir , { recursive : true } ) ;
138+ } else {
139+ throw new Error (
140+ 'Refusing to create output directory outside project root: ' + outputDir
141+ ) ;
142+ }
143+ fs . writeFileSync (
144+ outputFile . replace ( 'README.md' , 'docs.json' ) ,
145+ JSON . stringify ( citations )
146+ ) ;
147+
148+ fs . writeFileSync (
149+ outputFile ,
150+ template ( {
151+ root : getRoot ( citations , true ) ,
152+ citations : citations ,
153+ package : pkg ,
154+ } )
155+ ) ;
149156
150157 console . log ( 'Generated' , outputFile ) ;
151-
152158 } ) ;
153-
154159}
155160
156161function getHref ( name ) {
157-
158162 name = name . toLowerCase ( ) ;
159163
160164 for ( var i = 0 ; i < sourceFiles . length ; i ++ ) {
161-
162165 var sf = sourceFiles [ i ] ;
163166 if ( sf . includes ( name ) ) {
164167 return transform ( sf ) ;
165168 }
166-
167169 }
168170
169171 return null ;
@@ -172,11 +174,8 @@ function getHref(name) {
172174 var path = str . replace ( 'src/' , '' ) . replace ( 'jsm/' , '' ) . replace ( '.js' , '' ) ;
173175 return `/docs/${ path } /` ;
174176 }
175-
176177}
177178
178-
179-
180179function getRoot ( citations , shouldSplice ) {
181180 var list = citations . slice ( 0 ) ;
182181 for ( var i = 0 ; i < list . length ; i ++ ) {
@@ -214,26 +213,20 @@ function expandParam(param) {
214213}
215214
216215function expandLink ( object , property ) {
217-
218216 var value = object [ property ] ;
219217 var shouldRecurse = false ;
220218
221219 if ( value ) {
222-
223220 var regex = / \{ @ l i n k ( [ \w \d : / ? \- . # ] * ) \} / i;
224221 var link = value . match ( regex ) ;
225222
226223 if ( link && link . length > 1 ) {
227-
228224 var name = link [ 1 ] ;
229225
230226 if ( / h t t p / i. test ( name ) ) {
231-
232227 object [ property ] = value . replace ( regex , '[$1]($1)' ) ;
233228 shouldRecurse = true ;
234-
235229 } else {
236-
237230 var fragments = name . split ( / [ . # ] / i) ;
238231 var longname = name . replace ( / # / i, '.' ) ;
239232
@@ -250,24 +243,20 @@ function expandLink(object, property) {
250243 '(' ,
251244 dir ,
252245 hash ? '#' + hash . toLowerCase ( ) : '' ,
253- ')'
246+ ')' ,
254247 ] . join ( '' ) ;
255248
256249 object [ property ] = value . replace ( regex , href ) ;
257250 shouldRecurse = true ;
258-
259251 }
260-
261252 }
262-
263253 }
264254
265255 if ( shouldRecurse ) {
266256 expandLink ( object , property ) ;
267257 }
268258
269259 return object ;
270-
271260}
272261
273262function getDirectoryMatch ( str ) {
0 commit comments