2
2
'use strict' ;
3
3
4
4
var doop = require ( 'jsdoc/util/doop' ) ;
5
- var fs = require ( 'jsdoc/fs' ) ;
5
+ var fs = require ( 'jsdoc/fs' ) ; // jsdoc/fs offer non-standard functions (mkPath)
6
+ var fsExtra = require ( 'fs-extra' ) ;
6
7
var helper = require ( 'jsdoc/util/templateHelper' ) ;
7
8
var logger = require ( 'jsdoc/util/logger' ) ;
8
9
var path = require ( 'jsdoc/path' ) ;
@@ -226,10 +227,12 @@ function generate(type, title, docs, filename, resolveLinks) {
226
227
227
228
function generateSourceFiles ( sourceFiles , encoding ) {
228
229
encoding = encoding || 'utf8' ;
230
+ var sourceFilenames = [ ] ;
229
231
Object . keys ( sourceFiles ) . forEach ( function ( file ) {
230
232
var source ;
231
233
// links are keyed to the shortened path in each doclet's `meta.shortpath` property
232
234
var sourceOutfile = helper . getUniqueFilename ( sourceFiles [ file ] . shortened ) ;
235
+ sourceFilenames . push ( sourceOutfile ) ;
233
236
helper . registerLink ( sourceFiles [ file ] . shortened , sourceOutfile ) ;
234
237
235
238
try {
@@ -241,9 +244,9 @@ function generateSourceFiles(sourceFiles, encoding) {
241
244
catch ( e ) {
242
245
logger . error ( 'Error while generating source file %s: %s' , file , e . message ) ;
243
246
}
244
-
245
247
generate ( 'Source' , sourceFiles [ file ] . shortened , [ source ] , sourceOutfile , false ) ;
246
248
} ) ;
249
+ return sourceFilenames ;
247
250
}
248
251
249
252
/**
@@ -384,6 +387,40 @@ function buildNav(members) {
384
387
return nav ;
385
388
}
386
389
390
+ /**
391
+ Sorts an array of strings alphabetically
392
+ @param {Array<String> } strArr - Array of strings to sort
393
+ @return {Array<String> } The sorted array
394
+ */
395
+ function sortStrs ( strArr ) {
396
+ return strArr . sort ( function ( s1 , s2 ) {
397
+ var lowerCaseS1 = s1 . toLowerCase ( ) ;
398
+ var lowerCaseS2 = s2 . toLowerCase ( ) ;
399
+
400
+ if ( lowerCaseS1 < lowerCaseS2 ) {
401
+ return - 1 ;
402
+ } else if ( lowerCaseS1 > lowerCaseS2 ) {
403
+ return 1 ;
404
+ } else {
405
+ return 0 ;
406
+ }
407
+ } ) ;
408
+ }
409
+
410
+ /**
411
+ Prints into <outdir>/data a `methodNames.json` and a `sourceFiles.json`
412
+ JSON file that contains methods and files that can be searched on the
413
+ generated doc site.
414
+ @param {Array<String> } methodNames - A list of method names
415
+ @param {Array<String> } sourceFilenames - A list of source filenames
416
+ */
417
+ function writeSearchData ( methodNames , sourceFilenames ) {
418
+ var dataDir = path . join ( outdir , 'data' ) ;
419
+ fsExtra . mkdirsSync ( dataDir ) ;
420
+ fsExtra . writeJsonSync ( path . join ( dataDir , 'methodNames.json' ) , sortStrs ( methodNames ) , 'utf8' ) ;
421
+ fsExtra . writeJsonSync ( path . join ( dataDir , 'sourceFiles.json' ) , sortStrs ( sourceFilenames ) , 'utf8' ) ;
422
+ }
423
+
387
424
/**
388
425
@param {TAFFY } taffyData See <http://taffydb.com/>.
389
426
@param {object } opts
@@ -537,8 +574,21 @@ exports.publish = function(taffyData, opts, tutorials) {
537
574
} ) ;
538
575
539
576
// do this after the urls have all been generated
577
+ var methodNames = [ ] ;
540
578
data ( ) . each ( function ( doclet ) {
541
579
doclet . ancestors = getAncestorLinks ( doclet ) ;
580
+ if ( doclet . kind === 'function' ) {
581
+ var alias = doclet . alias ;
582
+ var name = doclet . name ;
583
+ if ( alias ) {
584
+ if ( Array . isArray ( alias ) ) {
585
+ alias = alias . join ( ', ' ) ;
586
+ }
587
+ methodNames . push ( name + ` (${ alias } )` ) ;
588
+ } else {
589
+ methodNames . push ( name ) ;
590
+ }
591
+ }
542
592
543
593
if ( doclet . kind === 'member' ) {
544
594
addSignatureTypes ( doclet ) ;
@@ -556,8 +606,8 @@ exports.publish = function(taffyData, opts, tutorials) {
556
606
members . tutorials = tutorials . children ;
557
607
558
608
// output pretty-printed source files by default
559
- var outputSourceFiles = conf . default && conf . default . outputSourceFiles !== false
560
- ? true
609
+ var outputSourceFiles = conf . default && conf . default . outputSourceFiles !== false
610
+ ? true
561
611
: false ;
562
612
563
613
// add template helpers
@@ -573,12 +623,15 @@ exports.publish = function(taffyData, opts, tutorials) {
573
623
attachModuleSymbols ( find ( { longname : { left : 'module:' } } ) , members . modules ) ;
574
624
575
625
// generate the pretty-printed source files first so other pages can link to them
626
+ var sourceFilenames = [ ] ;
576
627
if ( outputSourceFiles ) {
577
- generateSourceFiles ( sourceFiles , opts . encoding ) ;
628
+ sourceFilenames = generateSourceFiles ( sourceFiles , opts . encoding ) ;
578
629
}
579
630
580
- if ( members . globals . length ) {
581
- generate ( '' , 'Global' , [ { kind : 'globalobj' } ] , globalUrl ) ;
631
+ writeSearchData ( methodNames , sourceFilenames ) ;
632
+
633
+ if ( members . globals . length ) {
634
+ generate ( '' , 'Global' , [ { kind : 'globalobj' } ] , globalUrl ) ;
582
635
}
583
636
584
637
// index page displays information from package.json and lists files
@@ -655,6 +708,6 @@ exports.publish = function(taffyData, opts, tutorials) {
655
708
saveChildren ( child ) ;
656
709
} ) ;
657
710
}
658
-
711
+
659
712
saveChildren ( tutorials ) ;
660
713
} ;
0 commit comments