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

comment-serializer

v2.0.1

Published

A serializer for syntax-configurable source code documentation block comments.

Downloads

19

Readme

Comment Serializer

License Version Node support Bundle size Downloads

Comment Serializer parses a source string for documentation comment blocks and returns a serialized object. It is language and comment syntax style agnostic. It configured to support most documentation comment block styles.

Try out an example on RunKit.

Usage

source-code.txt:

//!
 // This is the general description.
 //
 // ^title Button
 // ^markup <button class="btn">My Button</button>
 ///

Blah, blah, blah... Some code...

my-serializer.js

const { readFileSync } = require('fs');
const serializer = require('comment-serializer');

const src = readFileSync('source-code.txt', { encoding: 'utf8' });

const mySerializer = serializer({
  tokens: {
    commentBegin: '//!',
    commentLinePrefix: '//',
    tagPrefix: '^',
    commentEnd: '///',
  },
});

const result = mySerializer(src);

Options

options.parsers

  • Required: No
  • Type: object

Custom tag parsers. An object of functions, keyed by the name of the tag to be parsed. The function receives the tag's content and must return the parsed value as a string.

When no parsers are provided, tags are serialized into tag and value pairs, but their values are not parsed in any way.

You can find some examples of custom parsers in examples/custom-parsers/parsers.js

Example

Source to parse:

/**
 * @mySpecialTag This value is special!
 */

The custom parser:

const mySerializer = serializer({
  parsers: {
    mySpecialTag: (value) => value.toUpperCase(),
  },
});

Result:

[
  {
    line: 1,
    source: '/**\n * @mySpecialTag This value is special!\n */',
    context: '',
    content: '@mySpecialTag This value is special!',
    preface: '',
    tags: [
      {
        line: 2,
        tag: 'mySpecialTag',
        value: 'This value is special!',
        valueParsed: 'THIS VALUE IS SPECIAL!',
        source: '@mySpecialTag This value is special!',
      },
    ],
  },
];

A more advanced example:

/**
 * @mySpecialTag
 *  - item-1
 *  - item-2
 *  - item-3
 */
const mySerializer = serializer({
  parsers: {
    mySpecialTag: (value) => {
      const match = value.match(/\s*[-*]\s+[\w-_]*/g);

      return [
        {
          type: 'items',
          value: match.map((item) => item.trim().replace(/[-*]\s/, '')),
        },
      ];
    },
  },
});

Result:

[
  {
    line: 1,
    source:
      '/**\n * @mySpecialTag\n *  - item-1\n *  - item-2\n *  - item-3\n */',
    context: '',
    content: '@mySpecialTag\n - item-1\n - item-2\n - item-3',
    preface: '',
    tags: [
      {
        line: 2,
        tag: 'mySpecialTag',
        value: '\n - item-1\n - item-2\n - item-3',
        valueParsed: [
          {
            type: 'items',
            value: ['item-1', 'item-2', 'item-3'],
          },
        ],
        source: '@mySpecialTag\n - item-1\n - item-2\n - item-3',
      },
    ],
  },
];
Tag Parsing Errors

Errors that occur while parsing a tag's value are caught. When this happens, tag's the valueParsed property will be an empty array, and the error object is added to the error property.

Example:

{
  line: 2,
  tag: 'mySpecialTag',
  value: '\n - item-1\n - item-2\n - item-3',
  valueParsed: [],
  source: '@mySpecialTag\n - item-1\n - item-2\n - item-3',
  error: { Error }
}

options.tokens

  • Required: No
  • Type: object

Customize the comment delimiters. The default tokens use JSDoc comment block syntax.

options.tokens.commentBegin

  • Required: No
  • Type: string
  • Default: '/**'

The delimiter marking the beginning of a comment block.

options.tokens.commentLinePrefix

  • Required: No
  • Type: string
  • Default: '*'

The delimiter marking a new line in the body of a comment block.

options.tokens.tagPrefix

  • Required: No
  • Type: string
  • Default: '@'

The delimiter marking the start of a tag in the comment body.

options.tokens.commentEnd

  • Required: No
  • Type: string
  • Default: '*/'

The delimiter marking the end of a comment block.

Token Syntax Support

Comment delimiters:

/**       <- commentBegin
 *        <- commentLinePrefix
 * @tag   <- tagPrefix (the "@" symbol)
 */       <- commentEnd

While most documentation comment styles should be supported, there are a few rules around the syntax:

  1. The commentBegin, commentLinePrefix, commentEnd and tagPrefix delimiter tokens must be distinct from each other.

  2. Delimiters should not rely on a whitespace character as a delimiter. For example, the following style would not be supported:

/**
  An unsupported style.

  @tag
 */