doxdox Dox Parser
doxdox-parser-dox is a plugin package for the doxdox documentation generator, specifically designed to parse JavaScript source code comments formatted according to the Dox style. It acts as an adapter, allowing doxdox-core to process and extract documentation from files utilizing Dox-compliant comment blocks. Currently, this package is in its 4.0.0-preview.25 release, indicating active development towards a stable v4. The project appears to follow a rapid release cadence for its preview versions, frequently pushing hotfixes and minor feature updates. As a core parser for the doxdox ecosystem, it differentiates itself by providing a robust, opinionated parsing mechanism that feeds into doxdox's broader templating and rendering capabilities, enabling users to generate clean and modern API documentation from their existing Dox-formatted comments. It integrates seamlessly with the doxdox-core library, which is a required peer dependency.
Common errors
-
Error: Cannot find module 'doxdox-core'
cause doxdox-core is a peer dependency and must be installed separately from doxdox-parser-dox.fixInstall the core library: `npm install doxdox-core@4.0.0-preview.25` or the corresponding compatible version. -
SyntaxError: require() not supported in ES module scope
cause doxdox v4 and its parser plugins are designed for ES Modules (ESM) and do not support CommonJS `require()` statements.fixEnsure your project is configured for ESM (e.g., by setting `"type": "module"` in `package.json` or using `.mjs` file extensions) and use `import` statements for all modules. -
Node.js version x.x.x is not supported. Required Node.js version is >=20.
cause Your Node.js environment is running a version older than the minimum required 20.x for doxdox v4 components.fixUpgrade Node.js to version 20.x or newer using a version manager like `nvm` or by installing a recent Node.js LTS release. -
Parsing failed due to comment/code pattern matching issues.
cause The parser encountered Dox comments or surrounding code patterns that it could not correctly interpret, possibly due to non-standard formatting or internal parser logic changes.fixReview your source code's Dox comments to ensure they adhere to standard formatting. If the issue persists with valid Dox syntax, consider reporting it to the doxdox project as a potential parser bug, referencing the affected preview version.
Warnings
- breaking The minimum Node.js version required for doxdox v4 and its associated parsers has been increased to 20.x.
- gotcha This package is currently in a preview state (v4.0.0-preview.X) and is subject to frequent updates, including potential API changes, before a stable v4 release.
- breaking The internal parser method signature and expected output data structure may have undergone breaking changes to align with a unified parser interface across doxdox v4.
- gotcha As a preview package, the exact structure of the parsed output (Document interface) or the ParserConfig object may evolve before a stable v4 release.
Install
-
npm install doxdox-parser-dox -
yarn add doxdox-parser-dox -
pnpm add doxdox-parser-dox
Imports
- DoxParser
import DoxParser from 'doxdox-parser-dox';
import { DoxParser } from 'doxdox-parser-dox'; - ParserConfig
import type { ParserConfig } from 'doxdox-parser-dox'; - Document
import type { Document } from 'doxdox-core';
Quickstart
import { DoxParser } from 'doxdox-parser-dox';
// In a typical doxdox setup, the core library would manage parser instantiation and execution.
// This quickstart demonstrates how the DoxParser itself works by calling its parse method directly.
// Sample JavaScript/TypeScript code with Dox comments
const sampleCode = `
/**
* @file This is a test file demonstrating Dox comments.
* @module Calculator
*/
/**
* Adds two numbers and returns their sum.
*
* This function takes two numeric arguments and
* performs an addition operation.
*
* @param {number} a - The first operand.
* @param {number} b - The second operand.
* @returns {number} The sum of 'a' and 'b'.
* @example
* const result = add(5, 3); // result is 8
*/
function add(a: number, b: number): number {
return a + b;
}
/**
* Subtracts the second number from the first.
*
* @param {number} x - The number to subtract from.
* @param {number} y - The number to subtract.
* @returns {number} The difference (x - y).
*/
const subtract = (x: number, y: number): number => x - y;
/**
* Multiplies two numbers.
* @private
* @param {number} factor1 - The first factor.
* @param {number} factor2 - The second factor.
* @returns {number} The product.
*/
function multiply(factor1: number, factor2: number): number {
return factor1 * factor2;
}
`;
async function parseDoxCode() {
const parser = new DoxParser();
const filePath = 'src/math-utils.ts'; // Represents the file path for context
const options = {}; // doxdox parser options, if any
console.log(`Parsing Dox-style comments from: ${filePath}\n`);
try {
const parsedDocument = await parser.parse(sampleCode, filePath, options);
console.log('--- Parsed Document Output (Excerpt) ---');
console.log(`File: ${parsedDocument.file}`);
console.log(`Module: ${parsedDocument.module}`);
console.log('Methods:');
parsedDocument.methods.forEach((method, index) => {
console.log(` ${index + 1}. Name: ${method.name}`);
console.log(` Description: ${method.description}`);
if (method.params) {
console.log(' Parameters:');
method.params.forEach(param => {
console.log(` - ${param.name} (${param.type}): ${param.description}`);
});
}
if (method.returns) {
console.log(` Returns: ${method.returns[0].type} - ${method.returns[0].description}`);
}
if (method.tags) {
console.log(` Tags: ${method.tags.map(tag => `@${tag.tag} ${tag.name}`).join(', ')}`);
}
console.log('');
});
} catch (error) {
console.error('Error during parsing:', error);
}
}
parseDoxCode();