Skip to content

Commit ef9c445

Browse files
committed
docs: added searching aliases (see caolan#1339)
1 parent 57b8db8 commit ef9c445

File tree

4 files changed

+82
-45
lines changed

4 files changed

+82
-45
lines changed

support/jsdoc/jsdoc-custom.js

+17-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
/* eslint no-undef: "off" */
2+
if (typeof setImmediate !== 'function' && typeof async === 'object') {
3+
setImmediate = async.setImmediate;
4+
}
5+
26
$(function initSearchBar() {
37
function matchSubstrs(methodName) {
48
var tokens = [];
@@ -94,12 +98,20 @@ $(function initSearchBar() {
9498
// handle source files
9599
} else if (suggestion.indexOf('.html') !== -1) {
96100
location.href = host + suggestion;
97-
// handle searching from one of the source files or the home page
98-
} else if (currentPage !== 'docs.html') {
99-
location.href = host + 'docs.html#' + suggestion;
100101
} else {
101-
var $el = document.getElementById(suggestion);
102-
$('#main-container').animate({ scrollTop: $el.offsetTop - 60 }, 500);
102+
var parenIndex = suggestion.indexOf('(');
103+
if (parenIndex !== -1) {
104+
suggestion = suggestion.substring(0, parenIndex-1);
105+
}
106+
107+
// handle searching from one of the source files or the home page
108+
if (currentPage !== 'docs.html') {
109+
location.href = host + 'docs.html#' + suggestion;
110+
} else {
111+
var $el = document.getElementById(suggestion);
112+
$('#main-container').animate({ scrollTop: $el.offsetTop - 60 }, 500);
113+
location.hash = '#'+suggestion;
114+
}
103115
}
104116
});
105117

support/jsdoc/jsdoc-fix-html.js

+1-28
Original file line numberDiff line numberDiff line change
@@ -42,29 +42,6 @@ function extractModuleFiles(files) {
4242
});
4343
}
4444

45-
function getSearchableInfo($page, callback) {
46-
var $sourceLinks = $page.find('a[href$=".js.html"]');
47-
var sourceFiles = $sourceLinks.map(function() {
48-
return $(this).attr('href');
49-
}).toArray().sort();
50-
51-
var $methodLinks = $page.find('nav').find('a');
52-
var methods = $methodLinks.map(function() {
53-
var methodName = $(this).text();
54-
return (methodName === 'Home' ? null : methodName);
55-
}).toArray().sort();
56-
57-
fs.mkdirsSync(path.join(docsDir, 'data'));
58-
async.parallel([
59-
function(fileCallback) {
60-
fs.writeJson(path.join(docsDir, 'data/sourceFiles.json'), sourceFiles, fileCallback);
61-
},
62-
function(fileCallback) {
63-
fs.writeJson(path.join(docsDir, 'data/methodNames.json'), methods, fileCallback);
64-
}
65-
], callback);
66-
}
67-
6845
function combineFakeModules(files, callback) {
6946
var moduleFiles = extractModuleFiles(files);
7047

@@ -86,11 +63,7 @@ function combineFakeModules(files, callback) {
8663
});
8764
}, function(err) {
8865
if (err) return callback(err);
89-
90-
getSearchableInfo($mainPage, function(err) {
91-
if (err) return callback(err);
92-
generateHTMLFile(path.join(docsDir, docFilename), $mainPage, callback);
93-
});
66+
generateHTMLFile(path.join(docsDir, docFilename), $mainPage, callback);
9467
});
9568
});
9669
}

support/jsdoc/theme/publish.js

+61-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
'use strict';
33

44
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');
67
var helper = require('jsdoc/util/templateHelper');
78
var logger = require('jsdoc/util/logger');
89
var path = require('jsdoc/path');
@@ -226,10 +227,12 @@ function generate(type, title, docs, filename, resolveLinks) {
226227

227228
function generateSourceFiles(sourceFiles, encoding) {
228229
encoding = encoding || 'utf8';
230+
var sourceFilenames = [];
229231
Object.keys(sourceFiles).forEach(function(file) {
230232
var source;
231233
// links are keyed to the shortened path in each doclet's `meta.shortpath` property
232234
var sourceOutfile = helper.getUniqueFilename(sourceFiles[file].shortened);
235+
sourceFilenames.push(sourceOutfile);
233236
helper.registerLink(sourceFiles[file].shortened, sourceOutfile);
234237

235238
try {
@@ -241,9 +244,9 @@ function generateSourceFiles(sourceFiles, encoding) {
241244
catch(e) {
242245
logger.error('Error while generating source file %s: %s', file, e.message);
243246
}
244-
245247
generate('Source', sourceFiles[file].shortened, [source], sourceOutfile, false);
246248
});
249+
return sourceFilenames;
247250
}
248251

249252
/**
@@ -384,6 +387,40 @@ function buildNav(members) {
384387
return nav;
385388
}
386389

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+
387424
/**
388425
@param {TAFFY} taffyData See <http://taffydb.com/>.
389426
@param {object} opts
@@ -537,8 +574,21 @@ exports.publish = function(taffyData, opts, tutorials) {
537574
});
538575

539576
// do this after the urls have all been generated
577+
var methodNames = [];
540578
data().each(function(doclet) {
541579
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+
}
542592

543593
if (doclet.kind === 'member') {
544594
addSignatureTypes(doclet);
@@ -556,8 +606,8 @@ exports.publish = function(taffyData, opts, tutorials) {
556606
members.tutorials = tutorials.children;
557607

558608
// 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
561611
: false;
562612

563613
// add template helpers
@@ -573,12 +623,15 @@ exports.publish = function(taffyData, opts, tutorials) {
573623
attachModuleSymbols( find({ longname: {left: 'module:'} }), members.modules );
574624

575625
// generate the pretty-printed source files first so other pages can link to them
626+
var sourceFilenames = [];
576627
if (outputSourceFiles) {
577-
generateSourceFiles(sourceFiles, opts.encoding);
628+
sourceFilenames = generateSourceFiles(sourceFiles, opts.encoding);
578629
}
579630

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);
582635
}
583636

584637
// index page displays information from package.json and lists files
@@ -655,6 +708,6 @@ exports.publish = function(taffyData, opts, tutorials) {
655708
saveChildren(child);
656709
});
657710
}
658-
711+
659712
saveChildren(tutorials);
660713
};

support/jsdoc/theme/tmpl/layout.tmpl

+3-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
<link rel="stylesheet" href="styles/prettify-tomorrow.css">
1313

14-
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Montserrat:400,700">
14+
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Montserrat:400,700">
1515
<link rel="stylesheet" href="styles/jsdoc-default.css">
1616

1717
<!--[if lt IE 9]>
@@ -86,11 +86,10 @@
8686
<script src="https://cdn.jsdelivr.net/jquery/2.2.4/jquery.min.js"></script>
8787
<script src="https://cdn.jsdelivr.net/bootstrap/3.3.6/js/bootstrap.min.js"></script>
8888
<script src="https://cdn.jsdelivr.net/typeahead.js/0.11.1/typeahead.bundle.min.js"></script>
89-
9089
<script>prettyPrint();</script>
90+
<script src="scripts/async.js"></script>
91+
9192
<script src="scripts/linenumber.js" async></script>
9293
<script src="scripts/jsdoc-custom.js" async></script>
93-
94-
<script src="scripts/async.js" async></script>
9594
</body>
9695
</html>

0 commit comments

Comments
 (0)