|
1 | 1 | 'use strict';
|
2 | 2 |
|
| 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 | + |
3 | 10 | /**
|
4 | 11 | * Sort two documentation objects, given an optional order object. Returns
|
5 | 12 | * a numeric sorting value that is compatible with stream-sort.
|
|
9 | 16 | * @return {number} sorting value
|
10 | 17 | * @private
|
11 | 18 | */
|
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 | + } |
14 | 60 | };
|
0 commit comments