UNPKG

type2docfx

Version:

A tool to convert json format output from TypeDoc to universal reference model for DocFx to consume.

126 lines (125 loc) 3.97 kB
"use strict"; exports.__esModule = true; var constants_1 = require("./common/constants"); var regex_1 = require("./common/regex"); var flags_1 = require("./common/flags"); function groupOrphanFunctions(elements) { if (elements && elements.length) { var mapping = {}; for (var i = 0; i < elements.length; i++) { if (elements[i].type === 'function') { var key = elements[i].module ? elements[i].module : 'ParentToPackage'; if (!mapping[key]) { mapping[key] = []; } mapping[key].push(elements[i]); elements.splice(i, 1); i--; } } return mapping; } } exports.groupOrphanFunctions = groupOrphanFunctions; function insertFunctionToIndex(index, functions) { if (index && functions) { index.items[0].children = index.items[0].children.concat(functions.map(function (f) { return f.uid; })); index.items = index.items.concat(functions); } } exports.insertFunctionToIndex = insertFunctionToIndex; function postTransform(element, references) { var roots = flattening(element); roots.forEach(function (root) { insertReferences(root, references); }); return roots; } exports.postTransform = postTransform; function insertReferences(root, references) { if (!references || Object.keys(references).length === 0) { return; } root.references = []; for (var key in references) { var reference = { uid: key, 'spec.typeScript': [] }; var match = void 0; var lastIndex = 0; while ((match = regex_1.uidRegex.exec(references[key])) !== null) { if (lastIndex < match.index) { reference['spec.typeScript'].push(getReference(references[key].substring(lastIndex, match.index))); } lastIndex = match.index + match[0].length; reference['spec.typeScript'].push(getReference(getItemName(match[1]), match[1])); } if (lastIndex < references[key].length) { reference['spec.typeScript'].push(getReference(references[key].substring(lastIndex))); } root.references.push(reference); } } function getReference(name, uid) { var reference = { name: name, fullName: name }; if (uid) { reference.uid = uid; } return reference; } function getItemName(uid) { var tmp = uid.split('.'); return tmp[tmp.length - 1]; } function flattening(element) { if (!element) { return []; } var result = []; result.push({ items: [element] }); if (element.children) { var childrenUid_1 = []; var children = element.children; if (flags_1.flags.enableAlphabetOrder) { children = children.sort(sortYamlModel); } children.forEach(function (child) { if (child.children && child.children.length > 0) { result = result.concat(flattening(child)); } else { childrenUid_1.push(child.uid); result[0].items.push(child); } }); element.children = childrenUid_1; return result; } } function sortYamlModel(a, b) { if (a.numericValue !== undefined && b.numericValue !== undefined) { return a.numericValue - b.numericValue; } // sort classes alphabetically, contructor first if (b.name === constants_1.constructorName) { return 1; } if (a.name === constants_1.constructorName) { return -1; } var nameA = a.name.toUpperCase(); var nameB = b.name.toUpperCase(); if (nameA < nameB) { return -1; } if (nameA > nameB) { return 1; } return 0; }