Skip to content

Commit 888f5ec

Browse files
committed
Superpowered sorting
1 parent dd0aca9 commit 888f5ec

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ module.exports = function (indexes, options, callback) {
101101
callback(null,
102102
filterAccess(options.access,
103103
hierarchy(
104+
sort(
104105
inputs
105106
.filter(filterJS(options.extension, options.polyglot))
106107
.reduce(function (memo, file) {
@@ -117,8 +118,7 @@ module.exports = function (indexes, options, callback) {
117118
nest,
118119
options.github && github
119120
))
120-
.filter(Boolean)
121-
.sort(sort))));
121+
.filter(Boolean), options))));
122122
} catch (e) {
123123
callback(e);
124124
}

lib/sort.js

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
'use strict';
22

3+
var remark = require('remark');
4+
var inlineTokenizer = require('./inline_tokenizer');
5+
6+
function parseMarkdown(string) {
7+
return remark.use(inlineTokenizer).parse(string);
8+
}
9+
310
/**
411
* Sort two documentation objects, given an optional order object. Returns
512
* a numeric sorting value that is compatible with stream-sort.
@@ -9,6 +16,45 @@
916
* @return {number} sorting value
1017
* @private
1118
*/
12-
module.exports = function sortDocs(a, b) {
13-
return a.context.sortKey.localeCompare(b.context.sortKey);
19+
module.exports = function sortDocs(comments, options) {
20+
if (!options.toc) {
21+
return comments.sort(function (a, b) {
22+
return a.context.sortKey.localeCompare(b.context.sortKey);
23+
});
24+
} else {
25+
var indexes = options.toc.reduce(function (memo, val, i) {
26+
if (typeof val === 'object' && val.name) {
27+
val.kind = 'note';
28+
memo[val.name] = i;
29+
} else {
30+
memo[val] = i;
31+
}
32+
return memo;
33+
}, {});
34+
var fixed = options.toc.filter(function (val) {
35+
return typeof val === 'object' && val.name;
36+
}).map(function (val) {
37+
if (val.description) {
38+
val.description = parseMarkdown(val.description);
39+
}
40+
return val;
41+
});
42+
var unfixed = [];
43+
comments.forEach(function (comment) {
44+
if (!comment.memberof && indexes[comment.name] !== undefined) {
45+
fixed.push(comment);
46+
} else {
47+
unfixed.push(comment);
48+
}
49+
});
50+
fixed.sort(function (a, b) {
51+
if (indexes[a.name] !== undefined && indexes[b.name] !== undefined) {
52+
return indexes[a.name] - indexes[b.name];
53+
}
54+
});
55+
unfixed.sort(function (a, b) {
56+
return a.context.sortKey.localeCompare(b.context.sortKey);
57+
});
58+
return fixed.concat(unfixed);
59+
}
1460
};

0 commit comments

Comments
 (0)