Installations
npm install tokenize-comment
Developer Guide
Typescript
No
Module System
CommonJS
Min. Node Version
>=8
Node Version
11.1.0
NPM Version
6.4.1
Score
73.5
Supply Chain
99.3
Quality
75.3
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Love this project? Help keep it running — sponsor us today! 🚀
Developer
jonschlinkert
Download Statistics
Total Downloads
497,340
Last Day
239
Last Week
1,860
Last Month
5,045
Last Year
69,017
GitHub Statistics
MIT License
14 Stars
38 Commits
6 Forks
4 Watchers
1 Branches
2 Contributors
Updated on Feb 11, 2025
Bundle Size
9.50 kB
Minified
3.32 kB
Minified + Gzipped
Package Meta Information
Latest Version
3.0.1
Package Id
tokenize-comment@3.0.1
Size
7.03 kB
NPM Version
6.4.1
Node Version
11.1.0
Published on
Nov 20, 2018
Total Downloads
Cumulative downloads
Total Downloads
497,340
Last Day
-14.3%
239
Compared to previous day
Last Week
59.7%
1,860
Compared to previous week
Last Month
58.2%
5,045
Compared to previous month
Last Year
-8.1%
69,017
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
1
Dev Dependencies
5
tokenize-comment

Uses snapdragon to tokenize a single JavaScript block comment into an object, with description, tags, and code example sections that can be passed to any other comment parsers for further parsing.
Install
Install with npm:
1$ npm install --save tokenize-comment
What does this do?
This is a node.js library for tokenizing a single JavaScript block comment into an object with the following properties:
description
examples
tags
footer
Why is this necessary?
After working with a number of different comment parsers and documentation systems, including dox, doctrine, jsdoc, catharsis, closure-compiler, verb, js-comments, parse-comments, among others, a few things became clear:
- There are certain features that are common to all of them, but they are implemented in totally different ways, and they all return completely different objects
- None of them follow the same conventions
- None of them have solid support for markdown formatting or examples in code comments (some have basic support, but weird conventions that need to be followed)
doctrine is a good example the disparity. It's a great parser that produces reliable results. But if you review the doctrine issues you'll see mention of the need to adhere to "jsdoc specifications" quite often. Unfortunately:
- jsdoc is not a specification and does not have anything close to a spec to follow. Only docs.
- Even if jsdoc did have a real spec, it's an attempt at implementing a Javadoc parser in JavaScript, which itself does not have a specification. Similarly, Oracle has some documentation for Javadoc, but no specification (at least, I could not find a spec and was informed there wasn't one when I contacted support)
- where jsdoc falls short, doctrine augments the "spec" with closure compiler conventions (which also is not a real specification)
- doctrine throws errors on a great variety of nuances and edge cases that jsdoc itself has no problem with and will normalize out for you
To be clear, I'm not picking on doctrine, it's one of the better parsers (and I'm using it to parse the tags returned by tokenize-comment
).
The solution
By tokenizing the comment first, we achieve the following:
- it's fast, since we're only interested in sifting out descriptions from examples and tags
- we get better support for different flavors of code examples (we can write indented or gfm code examples, or use the javadoc
@example
tag if we want) - we use doctrine, catharsis, or any other comment parser to do further processing on any of the values.
As a result, you can write code examples the way you want, and still follow jsdoc conventions for every other feature.
Example
Given the following comment:
1/** 2 * foo bar baz 3 * 4 * ```js 5 * var foo = "bar"; 6 * ``` 7 * @param {string} something 8 * @param {string} else 9 */
tokenize-comment
would return something like this:
1{ 2 description: 'foo bar baz', 3 footer: '', 4 examples: [ 5 { 6 type: 'gfm', 7 val: '```js\nvar foo = "bar";\n```', 8 description: '', 9 language: 'js', 10 code: '\nvar foo = "bar";\n' 11 } 12 ], 13 tags: [ 14 { 15 type: 'tag', 16 raw: '@param {string} something', 17 key: 'param', 18 val: '{string} something' 19 }, 20 { 21 type: 'tag', 22 raw: '@param {string} else', 23 key: 'param', 24 val: '{string} else' 25 } 26 ] 27}
We can now pass each tag to doctrine.parseTag()
or catharsis.parse()
, and format the resulting comment object to follow whatever convention we want. You can do something similar with the other properties as well.
Usage
The main export is a function that takes a string with a single javascript comment only, no code.
1var tokenize = require('tokenize-comment'); 2var token = tokenize(commentString); 3console.log(token);
The comment can be a "raw" comment with leading stars:
1/** 2 * foo bar baz 3 * @param {String} abc Some description 4 * @param {Object} def Another description 5 */
Or a comment with stars already stripped (with or without leading whitespace):
1 foo bar baz 2 @param {String} abc Some description 3 @param {Object} def Another description
Code examples
Recognizes gfm, javadoc and indented code examples. See the unit tests for a number of more complex examples.
GitHub Flavored Markdown
Supports GFM style code examples. The following comment:
1/** 2 * foo bar baz 3 * 4 * ```js 5 * var foo = "bar"; 6 * ``` 7 * @param {string} something 8 * @param {string} else 9 */
Results in:
1{ 2 description: 'foo bar baz', 3 footer: '', 4 examples: [ 5 { 6 type: 'gfm', 7 val: '```js\nvar foo = "bar";\n```', 8 description: '', 9 language: 'js', 10 code: '\nvar foo = "bar";\n' 11 } 12 ], 13 tags: [ 14 { 15 type: 'tag', 16 raw: '@param {string} something', 17 key: 'param', 18 val: '{string} something' 19 }, 20 { 21 type: 'tag', 22 raw: '@param {string} else', 23 key: 'param', 24 val: '{string} else' 25 } 26 ] 27}
Indented code
Supports indented code examples:
1/** 2 * foo bar baz 3 * 4 * var foo = "bar"; 5 * 6 * @param {string} something 7 * @param {string} else 8 */
javadoc (jsdoc)
Supports javadoc-style code examples:
1/** 2 * foo bar baz 3 * 4 * @example 5 * var foo = "bar"; 6 * var baz = "qux"; 7 * 8 * @param {string} something 9 * @param {string} else 10 */
Results in:
1{ 2 description: 'foo bar baz', 3 footer: '', 4 examples: [ 5 { 6 type: 'javadoc', 7 language: '', 8 description: '', 9 raw: '@example\nvar foo = "bar";\nvar baz = "qux";\n', 10 val: '\nvar foo = "bar";\nvar baz = "qux";\n' 11 } 12 ], 13 tags: [ 14 { 15 type: 'tag', 16 raw: '@param {string} something', 17 key: 'param', 18 val: '{string} something' 19 }, 20 { 21 type: 'tag', 22 raw: '@param {string} else', 23 key: 'param', 24 val: '{string} else' 25 } 26 ] 27}
Mixture
It will also recognize a mixture of formats (javadoc-style examples must always be last):
1/** 2 * This is a comment with 3 * several lines of text. 4 * 5 * An example 6 * 7 * ```js 8 * var foo = bar; 9 * var foo = bar; 10 * var foo = bar; 11 * ``` 12 * 13 * Another example 14 * 15 * var baz = fez; 16 * var baz = fez; 17 * var baz = fez; 18 * 19 * Another example 20 * 21 * var baz = fez; 22 * var baz = fez; 23 * 24 * 25 * 26 * And another example 27 * 28 * ```js 29 * var foo = bar; 30 * var foo = bar; 31 * ``` 32 * 33 * Another example 34 * 35 * @example 36 * var baz = fez; 37 * 38 * @example 39 * // this is a comment 40 * var alalla = zzzz; 41 * 42 * @param {String} foo bar 43 * @returns {Object} Instance of Foo 44 * @api public 45 */
Results in:
1{ 2 description: 'This is a comment with\nseveral lines of text.', 3 footer: '', 4 examples: [ 5 { 6 type: 'gfm', 7 language: 'js', 8 description: 'An example', 9 raw: '```js\nvar foo = bar;\nvar foo = bar;\nvar foo = bar;\n```', 10 val: '\nvar foo = bar;\nvar foo = bar;\nvar foo = bar;\n' 11 }, 12 { 13 type: 'indented', 14 language: '', 15 description: 'Another example', 16 raw: ' var baz = fez;\n var baz = fez;\n var baz = fez;\n', 17 val: 'var baz = fez;\nvar baz = fez;\nvar baz = fez;\n' 18 }, 19 { 20 type: 'indented', 21 language: '', 22 description: 'Another example', 23 raw: ' var baz = fez;\n var baz = fez;\n', 24 val: 'var baz = fez;\nvar baz = fez;\n' 25 }, 26 { 27 type: 'gfm', 28 language: 'js', 29 description: 'And another example', 30 raw: '```js\nvar foo = bar;\nvar foo = bar;\n```', 31 val: '\nvar foo = bar;\nvar foo = bar;\n' 32 }, 33 { 34 type: 'javadoc', 35 language: '', 36 description: 'Another example', 37 raw: '@example\nvar baz = fez;\n', 38 val: '\nvar baz = fez;\n' 39 }, 40 { 41 type: 'javadoc', 42 language: '', 43 description: '', 44 raw: '@example\n// this is a comment\nvar alalla = zzzz;\n', 45 val: '\n// this is a comment\nvar alalla = zzzz;\n' 46 } 47 ], 48 tags: [ 49 { 50 type: 'tag', 51 raw: '@param {String} foo bar', 52 key: 'param', 53 val: '{String} foo bar' 54 }, 55 { 56 type: 'tag', 57 raw: '@returns {Object} Instance of Foo', 58 key: 'returns', 59 val: '{Object} Instance of Foo' 60 }, 61 { 62 type: 'tag', 63 raw: '@api public', 64 key: 'api', 65 val: 'public' 66 } 67 ] 68}
About
Related projects
- parse-comments: Parse code comments from JavaScript or any language that uses the same format. | homepage
- snapdragon: Easy-to-use plugin system for creating powerful, fast and versatile parsers and compilers, with built-in source-map… more | homepage
- strip-comments: Strip comments from code. Removes line comments, block comments, the first comment only, or all… more | homepage
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Please read the contributing guide for advice on opening issues, pull requests, and coding standards.
Building docs
(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)
To generate the readme, run the following command:
1$ npm install -g verbose/verb#dev verb-generate-readme && verb
Running tests
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
1$ npm install && npm test
Author
Jon Schlinkert
License
Copyright © 2017, Jon Schlinkert. Released under the MIT License.
This file was generated by verb-generate-readme, v0.4.3, on March 16, 2017.

No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no SAST tool detected
Details
- Warn: no pull requests merged into dev branch
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Score
3
/10
Last Scanned on 2025-02-10
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to tokenize-comment
@nojaja/tokenize-comment
Uses snapdragon to tokenize a single JavaScript block comment into an object, with description, tags, and code example sections that can be passed to any other comment parsers for further parsing.
@master/text-template
Tokenize any texts and render with data ( less than 2KB )
text-template-forked
Tokenize any texts and render with data ( less than 2KB )
@flex-development/docast-util-from-docs
docast utility to parse docblocks