mirror of
https://github.com/documentationjs/documentation.git
synced 2026-01-25 14:26:29 +00:00
* feat(core): Switch to Promises everywhere. Adopt Node v4 ES6 Big changes: * Uses template strings where appropriate * Config and argument parsing is unified and there is no such thing as formatterOptions anymore. All user-passed options go through mergeConfig. * The node API surface changed (again): `buildSync` is removed, building operations return Promises. * Now using Flow for internal type annotations. More changes: * Remove buildSync command * feat(inference): Partially implement object shorthand support * Refs #649 * Use Flow annotations to enforce types * Keep flow but switch to comment syntax * Clarify types * More flow improvements * Turn server into class * LinkerStack becomes class too * Fix comment description type * Run flow on lint * Many more flow fixes * More intense flow refactoring * Simplify inference steps * Update inference tests, flow errors down to 1 * Continue refining types * Fix more flow issues * Use 'use strict' everywhere * Make 'ast' property configurable * Fix many tests * Fix more tests * Fix more tests * Fix augments * Test Markdown meta support * Improve test coverage * Switch back from for of to for for speed
117 lines
3.4 KiB
JavaScript
117 lines
3.4 KiB
JavaScript
|
|
'use strict';
|
|
/* @flow */
|
|
|
|
var parseMarkdown = require('./parse_markdown');
|
|
var chalk = require('chalk');
|
|
var path = require('path');
|
|
var fs = require('fs');
|
|
|
|
/**
|
|
* Sort two documentation objects, given an optional order object. Returns
|
|
* a numeric sorting value that is compatible with stream-sort.
|
|
*
|
|
* @param {Array<Object>} comments all comments
|
|
* @param {Object} options options from documentation.yml
|
|
* @return {number} sorting value
|
|
* @private
|
|
*/
|
|
module.exports = function sortDocs(comments/*: Array<Comment>*/, options/*: Object*/) {
|
|
if (!options || !options.toc) {
|
|
return sortComments(comments, options && options.sortOrder);
|
|
}
|
|
var indexes = options.toc.reduce(function (memo, val, i) {
|
|
if (typeof val === 'object' && val.name) {
|
|
val.kind = 'note';
|
|
memo[val.name] = i;
|
|
} else {
|
|
memo[val] = i;
|
|
}
|
|
return memo;
|
|
}, Object.create(null));
|
|
var toBeSorted = options.toc.reduce(function (memo, val) {
|
|
if (typeof val === 'string') {
|
|
memo[val] = false;
|
|
}
|
|
return memo;
|
|
}, Object.create(null));
|
|
// Table of contents 'theme' entries: defined as objects
|
|
// in the YAML list
|
|
var fixed = options.toc
|
|
.filter(val => typeof val === 'object' && val.name)
|
|
.map(function (val) {
|
|
if (typeof val.file === 'string') {
|
|
var filename = val.file;
|
|
if (!path.isAbsolute(val.file)) {
|
|
filename = path.join(process.cwd(), val.file);
|
|
}
|
|
|
|
try {
|
|
val.description = fs.readFileSync(filename).toString();
|
|
delete val.file;
|
|
} catch (err) {
|
|
process.stderr.write(chalk.red(`Failed to read file ${filename}`));
|
|
}
|
|
}
|
|
if (typeof val.description === 'string') {
|
|
val.description = parseMarkdown(val.description);
|
|
}
|
|
return val;
|
|
});
|
|
var unfixed = [];
|
|
comments
|
|
.forEach(function (comment) {
|
|
// If comment is of kind 'note', this means that we must be _re_ sorting
|
|
// the list, and the TOC note entries were already added to the list. Bail
|
|
// out here so that we don't add duplicates.
|
|
if (comment.kind === 'note') {
|
|
return;
|
|
}
|
|
|
|
// If comment is top-level and `name` matches a TOC entry, add it to the
|
|
// to-be-sorted list.
|
|
if (!comment.memberof && indexes[comment.name] !== undefined) {
|
|
fixed.push(comment);
|
|
toBeSorted[comment.name] = true;
|
|
} else {
|
|
unfixed.push(comment);
|
|
}
|
|
});
|
|
fixed.sort((a, b) => {
|
|
if (indexes[a.name] !== undefined && indexes[b.name] !== undefined) {
|
|
return indexes[a.name] - indexes[b.name];
|
|
}
|
|
});
|
|
sortComments(unfixed, options.sortOrder);
|
|
Object.keys(toBeSorted)
|
|
.filter(key => toBeSorted[key] === false)
|
|
.forEach(key => {
|
|
process.stderr.write(chalk.red('Table of contents defined sorting of ' + key +
|
|
' but no documentation with that namepath was found\n'));
|
|
});
|
|
return fixed.concat(unfixed);
|
|
};
|
|
|
|
function compare(a/*: string */, b/*: string */) {
|
|
return a.localeCompare(b, undefined, {caseFirst: 'upper'});
|
|
}
|
|
|
|
function compareCommentsByName(a, b) {
|
|
var akey = a.memberof || a.name;
|
|
var bkey = b.memberof || b.name;
|
|
|
|
if (akey && bkey) {
|
|
return compare(akey, bkey);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
function compareCommentsBySourceLocation(a, b) {
|
|
return a.context.sortKey.localeCompare(b.context.sortKey);
|
|
}
|
|
|
|
function sortComments(comments, sortOrder) {
|
|
return comments.sort(sortOrder === 'alpha' ?
|
|
compareCommentsByName : compareCommentsBySourceLocation);
|
|
}
|