npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

xdoc-parser

v3.2.3

Published

An XDoc comment parser.

Downloads

95

Readme

xdoc-parser

NPM Build Status FOSSA Status Commitizen friendly Open Source Helpers

An XDoc comment parser.

Introduction

XDoc is an alternative to JSDoc's syntax and heavily inspired by Rust's documentation style. By looking at JSDoc's documentation page, there really isn't a grammar for it. Thus, writing documentation and then trying to parse it makes things a bit diffcult. XDoc is sort of an answer to this problem.

XDoc is based on an actual grammar (using ANTLR4) and those who use languages such as TypeScript, Rust, or Swift should be familiar with the syntax. Here's an example that shows how you can write confusing syntaxes in a more familiar form:

JSDoc                           XDoc
============================    ===================================================
@param {Array.<string>} names   → @param names: string[]
@param {*} name                 → @param name: any
@param {(any|string)} name      → @param name: any | string
@param {string} [name]          → @param name?: string
@param {???} [name = "Joe"]     → @param somebody: any | (string & number) = "Joe"

Bear in mind that XDoc doesn't understand what @param really means. Since it is only a parser, it will just give you an AST. To fully grasp XDoc, I recommend you to check out the ANTLR grammar.

Example

A basic example would look like the following:

Example file:

/**
 * Description
 *
 * # API
 * 
 * ```
 * @param tag id - description.
 * ```
 */

 ...

Example program:

// ...
const xdoc = require('xdoc-parser');
const ast = xdoc(fs.readFileSync('...')).parseFile();
console.log(JSON.stringify(ast, null, 2));
// ...

In the example above, xdoc() will return an array containing an object with markdown and documentation:

[{
  markdown: RemarkNode,
  documentation: Partial<DocumentationNode>
}]

If you are parsing a single comment, then use the parse method as shown below:

Example program:

// ...
const xdoc = require('xdoc-parser');
const ast = xdoc(fs.readFileSync('...')).parse();
console.log(JSON.stringify(ast, null, 2));
// ...

Note: If you pass a file with multiple comments, parse() will only parse the first comment in the file.

Finally, to parse only the syntax and not the Markdown comments, use the parseSyntax method as shown below:

const xdoc = require('xdoc-parser');
const ast = xdoc('@tag id: type').parse();
console.log(JSON.stringify(ast, null, 2));

You may also access the core parser class (or other classes) and simply parse the XDoc syntax if that's all you care for. For example:

const xdoc = require('xdoc-parser')
const XDocASTGenerator = xdoc.core.XDocASTGenerator;
const XDocASTVisitor = xdoc.core.XDocASTVisitor;

const generate = (source) => new XDocASTGenerator(source).generate();
const parse = (source) => new XDocASTVisitor(generate(source)).visit();
const ast = parse('@tag id: type - description {@inline description}');
console.log(JSON.stringify(ast, null, 2));

Note: xdoc-parser is written in Typescript. Thus, you may easily import it as follows:

import xdoc from 'xdoc-parser'

Documentation Style

There are two ways that you can write your documentations. The first way is to write in JSDoc-style and the second way is to write in XDoc-style:

JSDoc-style

/**
 * Description
 * @tag id: string
 */

XDoc-style

/**
 * Description
 * 
 * # API
 * 
 * ```
 * @tag id: string
 * ```
 */

Note: Regardless of style used, xdoc() will parse only the annotations within a comment. Also, the type of comment does not matter. For example, # comment or """ comment """ should work.

Documentation

To be added...once mr-doc v4 is complete.

Contribution

There are two main classes in the codebase that make up the XDoc parser and you are free to modify them to enhance XDoc. The requirements are:

First, the grammar file can be modified to add or remove features. If you modify the grammar, you will need to run the build script npm run-script build and reflect the changes in XDocASTVisitor.ts.

Second, XDocCommentParser.ts strips out anything within a comment block. At the moment, the parser is handwritten and quite limited. It cannot parser triple-slash comments continuously like in Rustdoc. For example:

///
///...
///
...

License

FOSSA Status