jsdoc-to-markdown icon indicating copy to clipboard operation
jsdoc-to-markdown copied to clipboard

--sort-by

Open ghost opened this issue 9 years ago • 6 comments

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

ghost avatar Oct 31 '16 02:10 ghost

You can obtain the template data yourself (via getTemplateData()), sort it as desired then pass it as the options.data option into .render().

75lb avatar May 17 '19 20:05 75lb

Thanks a ton, way better than my hack of patching jsdoc-parse. ;)

whitlockjc avatar May 17 '19 21:05 whitlockjc

You can obtain the template data yourself (via getTemplateData()), sort it as desired then pass it as the options.data option 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?

cosinekitty avatar Jul 21 '22 20:07 cosinekitty

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)

lysdexic-audio avatar Jul 29 '22 12:07 lysdexic-audio

just add this condition if you want your functions to come last etc

    if (nameA < nameB && a.kind !== 'function') {
      return -1;
    }

lysdexic-audio avatar Jul 29 '22 12:07 lysdexic-audio

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.

cosinekitty avatar Jul 29 '22 14:07 cosinekitty