Convert parsed comment objects into formatted markdown documentation suitable for API reference generation.
Converts an array of parsed comment objects into structured markdown documentation with table of contents and formatted API sections.
/**
* Generate markdown API documentation from parsed comments
* @param {CommentObject[]} comments - Array of parsed comment objects from parseComments
* @returns {string} Formatted markdown documentation with TOC and API sections
*/
function api(comments);Usage Examples:
const dox = require('dox');
const fs = require('fs');
// Parse a JavaScript file
const source = fs.readFileSync('./mymodule.js', 'utf8');
const comments = dox.parseComments(source);
// Generate markdown documentation
const markdown = dox.api(comments);
console.log(markdown);
// Save to README file
fs.writeFileSync('./README.md', markdown);
// Example output structure:
// - [functionName()](#functionname)
// - [ClassName.method()](#classnamemethodname)
//
// ### functionName(param1:string, param2:number)
//
// Function description here
//
// ### ClassName.method(args)
//
// Method description hereThe API generator creates markdown with the following structure:
Automatically generated based on detected functions, methods, and classes:
- [add()](#add)
- [User.getName()](#usergetname)
- [Calculator.prototype.multiply()](#calculatorprototypemultiply)Each public API element gets its own section with:
The generator creates readable function signatures from context and tag information:
// Source code:
/**
* Add two numbers together
* @param {number} a First number
* @param {number} b Second number
* @return {number} Sum of the numbers
*/
function add(a, b) { return a + b; }
// Generated markdown:
### add(a:number, b:number)
<p>Add two numbers together</p>Methods include their full context path:
// Source code:
/**
* Get user's full name
* @return {string} The user's full name
*/
User.prototype.getName = function() { return this.name; };
// Generated markdown:
### User.prototype.getName()
<p>Get user's full name</p>The generator automatically filters out private APIs:
@api private are excluded/*! ... */ (ignored comments) are excludedmodule.exports assignments are filtered outComment descriptions are processed for optimal markdown output:
# charactersThe generator enhances code blocks with:
js)While dox doesn't provide built-in template customization, you can process the generated markdown:
const dox = require('dox');
// Generate base documentation
const comments = dox.parseComments(source);
const baseMarkdown = dox.api(comments);
// Add custom sections
const customMarkdown = `# My API Documentation
${baseMarkdown}
## Installation
\`npm install my-package\`
## License
MIT License
`;Create your own documentation generator using dox's parsing capabilities:
const dox = require('dox');
function generateCustomDocs(source) {
const comments = dox.parseComments(source);
// Filter only public APIs
const publicApis = comments.filter(comment => !comment.isPrivate);
// Group by type
const functions = publicApis.filter(c => c.ctx && c.ctx.type === 'function');
const methods = publicApis.filter(c => c.ctx && c.ctx.type === 'method');
const classes = publicApis.filter(c => c.ctx && c.ctx.type === 'class');
// Generate custom format
let docs = '# API Reference\n\n';
if (functions.length) {
docs += '## Functions\n\n';
functions.forEach(func => {
docs += `### ${func.ctx.name}\n\n`;
docs += `${func.description.full}\n\n`;
});
}
if (classes.length) {
docs += '## Classes\n\n';
classes.forEach(cls => {
docs += `### ${cls.ctx.name}\n\n`;
docs += `${cls.description.full}\n\n`;
});
}
return docs;
}Integrate documentation generation into your build process:
// gulpfile.js or build script
const dox = require('dox');
const fs = require('fs');
const glob = require('glob');
function generateDocs() {
const jsFiles = glob.sync('./src/**/*.js');
let allComments = [];
jsFiles.forEach(file => {
const source = fs.readFileSync(file, 'utf8');
const comments = dox.parseComments(source);
allComments = allComments.concat(comments);
});
const documentation = dox.api(allComments);
fs.writeFileSync('./docs/API.md', documentation);
}Use dox with static site generators:
// For Jekyll, Hexo, etc.
const dox = require('dox');
function generateForJekyll(source, frontMatter) {
const comments = dox.parseComments(source);
const markdown = dox.api(comments);
return `---
${frontMatter}
---
${markdown}`;
}The TOC is generated using internal helper functions:
Code blocks are enhanced through a multi-step process:
Internal cross-references and external links are processed:
@see tags are preserved as clickable linksThe documentation generator handles edge cases gracefully: