Code Monkey home page Code Monkey logo

jsdoc-md's Introduction

jsdoc-md logo

jsdoc-md

npm version CI status

A Node.js CLI and equivalent JS API to analyze source JSDoc and generate documentation under a given heading in a markdown file (such as readme.md).

Installation

To install with npm, run:

npm install jsdoc-md --save-dev

Then, use either the CLI command jsdoc-md or the JS API function jsdocMd to generate documentation.

CLI

Command jsdoc-md

Analyzes JSDoc from source files nested in the current working directory to populate a markdown file documentation section. Source files are excluded via .gitignore files. If the optional peer dependency prettier is installed, the new markdown file contents is Prettier formatted.

It implements the function jsdocMd.

Arguments

Argument Alias Default Description
--source-glob -s **/*.{mjs,cjs,js} JSDoc source file glob pattern.
--markdown-path -m readme.md Path to the markdown file for docs insertion.
--target-heading -t API Markdown file heading to insert docs under.
--check -c Should an error be thrown instead of updating the markdown file if the contents would change; useful for checking docs are up to date in CI.

Examples

Using npx.

With defaults:

npx jsdoc-md

With arguments:

npx jsdoc-md --source-glob **/*.{mjs,cjs,js} --markdown-path readme.md --target-heading API

Using package scripts.

package.json scripts for a project that also uses eslint and prettier:

{
  "scripts": {
    "docs-update": "jsdoc-md",
    "docs-check": "jsdoc-md -c",
    "eslint": "eslint .",
    "prettier": "prettier -c .",
    "test": "npm run eslint && npm run prettier && npm run docs-check",
    "prepublishOnly": "npm test"
  }
}

Run the prettier script before docs-check in the test script so prettier reports formatting errors instead of jsdoc-md.

Whenever the source JSDoc changes, run the docs-update script:

npm run docs-update

API

function jsdocMd

Analyzes JSDoc from source files to populate a markdown file documentation section. Source files are excluded via .gitignore files. If the optional peer dependency prettier is installed, the new markdown file contents is Prettier formatted.

Parameter Type Description
options object? Options.
options.cwd string? A directory path to scope the search for source and .gitignore files, defaulting to process.cwd().
options.sourceGlob string? = **/*.{mjs,cjs,js} JSDoc source file glob pattern.
options.markdownPath string? = readme.md Path to the markdown file for docs insertion.
options.targetHeading string? = API Markdown file heading to insert docs under.
options.check boolean? = false Should an error be thrown instead of updating the markdown file if the contents would change; useful for checking docs are up to date in CI.

Returns: Promise<void> — Resolves once the operation is done.

Examples

Ways to import.

import { jsdocMd } from "jsdoc-md";
import jsdocMd from "jsdoc-md/jsdocMd.mjs";

Customizing options.

jsdocMd({
  cwd: "/path/to/project",
  sourceGlob: "index.mjs",
  markdownPath: "README.md",
  targetHeading: "Docs",
}).then(() => {
  console.log("Done!");
});

Caveats

No code inference

Missing JSDoc tags are not inferred by inspecting the code, so be sure to use all the necessary tags.

/**
 * The number 1.
 * @kind constant
 * @name ONE
 * @type {number}
 */
const ONE = 1;

Tag subset

A JSDoc tag subset is supported:

Namepath prefixes

Some JSDoc namepath prefixes are not supported:

Namepath special characters

JSDoc namepath special characters with surrounding quotes and backslash escapes (e.g. @name a."#b"."\"c") are not supported.

Inline tags

One JSDoc inline tag link syntax is supported for namepath links in JSDoc descriptions and tags with markdown content: [`b` method]{@link A#b}. Use normal markdown syntax for non-namepath links.

Other inline tags such as {@tutorial} are unsupported.

Example content

@example content outside <caption /> (which may also contain markdown) is treated as markdown. This allows multiple code blocks with syntax highlighting and explanatory content such as paragraphs and images. For example:

/**
 * Displays a message in a native popup window.
 * @kind function
 * @name popup
 * @param {string} message Message text.
 * @example <caption>Say `Hello!` to the user.</caption>
 * This usage:
 *
 * ```js
 * popup("Hello!");
 * ```
 *
 * Displays like this on macOS:
 *
 * ![Screenshot](path/to/screenshot.jpg)
 */
const popup = (message) => alert(message);

jsdoc-md's People

Contributors

jaydenseric avatar pur3miish avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

jsdoc-md's Issues

Make auto MDN linking ES global types case-sensitive

I think we should probably rethink the way ES global types are linked prior to the next release. Originally types were case-insensitive as people often write types like String like string. But now that we are dealing with a large list of globals like BigInt that have specific capitalization, not all should be case-insensitive.

Although some things like boolean and Boolean are technically different types (one is a primitive value, the other is an instance) they may map to the same MDN article if there are not separate articles available.

See also https://github.com/jaydenseric/eslint-config-env/blob/v2.0.0/index.js#L128.

Fix broken successive member links

outlineMembers() should be responsible for adding a new slug property to each member, using slugger.slug().

The slug properties need to be generated in the order the members are documented in the final markdown, so any sorting of members in the future will need to be an initial step in outlineMembers().

No other parts of the codebase should be using slugger.slug(); rather they should utilize the slug property of the member in question.

CLI to check if the markdown file is up to date

The CLI needs a new command or flag to check if the markdown file is up to date with the source JSDoc. This would be useful in package test scripts, so that CI can detect if JSDoc gets out of sync with the API docs in the markdown file (e.g. readme.md).

Code gets read but is not generated at all...

When I try to read test.js and put the data into docs.md by typing this: jsdoc-md -s test.js -m docs.md it shows no errors but it does not work...

test.js

/**
 * @type {Number}
 */
const test = 0

/**
 * @param {Number} lol - oh no...
 * 
 * @returns {0}
 */
function test2(lol) { return 0 }

docs.md

# API

how do we show type for namespace?

I am trying to build the equivalent of a ts interface.

So far I have...

/**
 * The account name and ABI.
 * @kind namespace
 * @name AccountAndAbi
 */

/**
 * Account name.
 * @kind constant
 * @name AccountAndAbi.account_name
 * @type {string}
 */

/**
 * ABI Uint8Array binary representation
 * @kind constant
 * @name AccountAndAbi.abi
 * @type {Uint8Array}
 */

which produces this...

screenshot 2018-11-17 at 21 21 43

I would like to know is their anyway to get the types included in the generated MD, or is there another way you suggest to writing a namespace?

Thanks.

Getting ERR_PACKAGE_PATH_NOT_EXPORTED

On a new install, when I try and run this using npx:

npx jsdoc-md

It gives me the following output:

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './lib/parser/index.js' is not defined by "exports" in /my/path/node_modules/comment-parser/package.json imported from /my/path/node_modules/jsdoc-md/private/jsdocCommentToMember.mjs

Any ideas? Could very well be user error on my part.

Publish the private JSDoc analysis API

Great project

What do you think about extracting jsdocCommentToMember into its own api? I would like to re-use that part of your library, without the markdown features?

Thanks

Missing target heading “API”.

No matter what -t value I give, or even if I touch API (with a single line of markdown or not), I cannot get passed this error.

Error: Missing target heading “API”.
    at /Users/path-to-script/node_modules/jsdoc-md/lib/remarkPluginReplaceSection.js:23:12
    at wrapped (/Users/path-to-script/node_modules/trough/wrap.js:25:19)
    at next (/Users/path-to-script/node_modules/trough/index.js:58:24)
    at Object.run (/Users/path-to-script/node_modules/trough/index.js:32:10)
    at executor (/Users/path-to-script/node_modules/unified/index.js:295:20)
    at Function.run (/Users/path-to-script/node_modules/unified/index.js:292:5)
    at pipelineRun (/Users/path-to-script/node_modules/unified/index.js:27:5)
    at wrapped (/Users/path-to-script/node_modules/trough/wrap.js:25:19)
    at next (/Users/path-to-script/node_modules/trough/index.js:58:24)
    at done (/Users/path-to-script/node_modules/trough/wrap.js:56:16)

Sort members in the documentation outline

At the moment, the order of documented members is largely determined by the order the JSDoc is scraped from the source code. Sorting members so that similar kinds are grouped (at each level), with typedefs grouped last, with alphabetical sort on pathnames within groups, would make the docs more coherent. It would also make the order deterministic, so that refactoring the order of functions etc. in source code will not result in large readme diffs.

The best place for sorting is likely in outlineMembers.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.