02/25/2023

JSDoc Setup Guide

JSDoc is a popular tool used to generate documentation for JavaScript code. It allows developers to document their code in a standardized format, making it easier for others to understand and use their code.

Here is a step-by-step tutorial on how to set up JSDoc for your JavaScript project:

Install JSDoc

You can install JSDoc using npm (the Node.js package manager). Open your terminal or command prompt and run the following command:

npm install -g jsdoc

This command will install JSDoc globally on your system, so you can use it for any JavaScript project.

Create a JSDoc configuration file

JSDoc requires a configuration file to specify how the documentation should be generated. Create a new file named jsdoc.json in the root directory of your project, and add the following code:

{
  "source": {
    "include": ["./src"],
    "includePattern": ".+\\.js(doc)?$",
    "excludePattern": "(^|\\/|\\\\)_"
  },
  "opts": {
    "destination": "./docs",
    "recurse": true
  },
  "plugins": [
    "plugins/markdown"
  ],
  "markdown": {
    "parser": "gfm",
    "hardwrap": true
  },
  "templates": {
    "cleverLinks": false,
    "monospaceLinks": false
  }
}

This configuration file tells JSDoc to generate documentation for all JavaScript files in the ./src directory, and save the output in the ./docs directory. It also specifies that JSDoc should use the markdown plugin to support Markdown syntax in the documentation, and specifies some options for the Markdown parser.

Add JSDoc comments to your code

JSDoc uses special comments in your code to generate documentation. These comments start with the /** characters and contain tags that describe the code element being documented.

For example, here's how you can document a JavaScript function using JSDoc comments:

/**
 * Add two numbers together.
 * @param {number} a - The first number.
 * @param {number} b - The second number.
 * @returns {number} The sum of the two numbers.
 */
function add(a, b) {
  return a + b;
}

In this example, the @param tag is used to document the function's parameters, and the @returns tag is used to document the return value.

Generate the documentation

To generate the documentation, run the following command in your terminal or command prompt:

jsdoc -c jsdoc.json

This command tells JSDoc to use the jsdoc.json configuration file to generate the documentation. The output will be saved in the ./docs directory (as specified in the configuration file).

That's it! You should now have a set of documentation files generated by JSDoc for your JavaScript project. You can open the index.html file in the ./docs directory in a web browser to view the documentation.

Back

Comments


Be the first to comment!

Read More

Node JS Express Server

Even though Node JS has a built in HTTP request handler using a web server framework can reduce the amount of code and increase the reliability of your site. There are many options to ...

Kod.js client or server generated code captures
Kod.js client or server generated code captures

Kod.js

Kod.js is a code preview image generator that runs both in the browser and on Node JS. It is built using highlight.js and uses it for the themes and syntax highlighting.

Express Plugins

Express plugins can be used to process POST body data, serve static files, or serve content with CORS enabled. Learn the basics of app.use.

Building SvelteKit for Node JS

SvelteKit is a powerful tool that is great for making all types of websites, with out of the box support for services like Vercel. In this tu...