jsdoc-to-markdown
jsdoc-to-markdown copied to clipboard
--sort-by
Hi,
Option --sort-by was mandatory for me. I use differents signatures for methods, like :
- foo.bar(string)
- foo.bar(Array.
)
And I can't imagine there are not together. See https://github.com/avighier/simple-dijs#Di
You can obtain the template data yourself (via getTemplateData()), sort it as desired then pass it as the options.data option into .render().
Thanks a ton, way better than my hack of patching jsdoc-parse. ;)
You can obtain the template data yourself (via
getTemplateData()), sort it as desired then pass it as theoptions.dataoption into.render().
Could you elaborate on this? I have a long list of functions that I would like to sort alphabetically in the generated markdown. It sounds like I need to write my own JavaScript program, then call into some other code in order to make this work. There is no way to do this from the command line and/or configuration file?
Yep - you can run a .js file directly with node which calls jsdoc2md
ie: $ node js2md.js
// js2md.js
'use strict'
const jsdoc2md = require('jsdoc-to-markdown')
const fs = require('fs')
const path = require('path')
/* input and output paths */
const inputFiles = 'path/to/your/jsfiles.js';
const outputDir = __dirname;
let namePaths;
(async () => {
await jsdoc2md.clear();
const jsDocOpts = { files: inputFiles };
namePaths = await jsdoc2md.getNamepaths(jsDocOpts);
console.log("namepaths", namePaths);
/* get template data */
const templateData = jsdoc2md.getTemplateDataSync(jsDocOpts);
// sort template data
// see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
templateData.sort(function(a,b) {
const nameA = a.longname.toUpperCase(); // ignore upper and lowercase
const nameB = b.longname.toUpperCase(); // ignore upper and lowercase
if (nameA < nameB) {
return -1;
}
if (nameA > nameB) {
return 1;
}
// names must be equal
return 0;
})
const renderOpts = { data: templateData, separators: true, 'name-format': false, 'no-gfm': true, 'example-lang': "js", "member-index-format": "list" };
console.log(`rendering all that good stuff..`)
const output = jsdoc2md.renderSync(renderOpts);
fs.writeFileSync(path.resolve(outputDir, 'mySweetDocs.md'), output)
just add this condition if you want your functions to come last etc
if (nameA < nameB && a.kind !== 'function') {
return -1;
}
Thanks @lysdexic-audio for the helpful explanation. I would have never figured that out! Actually, I ended up writing my own Python script to post-process the generated Markdown, and it is about the same amount of code. But this will be a good reference for others as a faster starting point than what I did.