Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
comment-to-doc
Advanced tools
Module for parsing and converting code comments into documentation
Module for parsing specific type of comments and generating documentation from them. Comments are similar to ones used in jsdoc. This module parses comments and lets you define how you render them into a document and type of document. By default the output is a .md file (can be anything) and some default comment types are defined.
Because I needed to generate some documentation from my code,
but outputs of other modules were not what I needed.
Some of them created HTML documentation webpages, others generated undesired markup.
I wanted specifically to create .md
files for each file that I had documented.
This module is though able to generate files with any extension and output defined by user.
npm install --save-dev comment-to-doc
The fastest way to get started with default configuration is like this:
import generateDocs, { Config, defaultTags } from "comment-to-doc";
const config: Config = {
files: [
'./src/**/*.tsx',
],
tags: defaultTags,
};
generateDocs(config);
{
files: string | string[],
excludeFiles?: string | string[],
tags?: Tag[],
outputExt?: string,
output?: (dir: string, fileName: string) => string,
template?: Template,
strict?: boolean,
tagsOrder?: string[],
tagsOrderInFiles?: {
[fileNameOrPath: string]: string[]
}
}
key | Mandatory | type | default | description |
---|---|---|---|---|
files | Mandatory | string or string[] | undefined | paths to files to read comments from (uses glob). |
excludeFiles | Optional | string or string[] | undefined | paths to files which should be excluded from files paths (uses glob). |
tags | Optional | Tag[] | [] | Tags configuration which define how each comment tag is rendered into documentation block. By default, if no tags are passed, only the tag with its content will be written to documentation file. |
outputExt | Optional | string | md | Extension of output documentation file. |
output | Optional | (dir: string, fileName: string) => string | ${dir}/${fileName}.${outputExt} | Output path for documentation. Read file directory and name can be used to generate one. |
template | Optional | Template | { rules: {}, strictness: undefined, errorHandling: 'error' } | Configuration for enforcing specific tags and their order. |
strict | Optional | boolean | false | If true, will only generate document with tags that are defined in "tags" array. By default all tags are used. |
tagsOrder | Optional | string[] | undefined | You can specify the order of tags that will be written in document file. Useful when multiple files are concatted into single documentation file. |
tagsOrderInFiles | Optional | { [fileNameOrPath: string]: string[] } | undefined | You can specify the order of tags that will be written in document file per file. Overrides general order specified in tagsOrder for given file. |
A simple tag is defined like this:
type Tag = {
tag: string,
render?: (parsedComment: ParsedComment) => string,
children?: Tag[]
}
For more details, click here to see how to configure tag in order to generate documentation.
TemplateStrictness.Strict | TemplateStrictness.IgnoreOrder
TemplateStrictness.Strict
- Fail validation if any mandatory tag that does not exist in "template.rules" array is found or order of mandatory tags is not correct.TemplateStrictness.IgnoreOrder
- Fail validation if any mandatory tag that does not exist in "template.rules" array is found. Order is not important.{ tag: string, mandatory?: boolean }
ErrorHandling.Error | ErrorHandling.Warn
ErrorHandling.Error
- Validation fails with an error and stops the process.ErrorHandling.Warn
- Validation fails with warnings and does not stop the process.Following example is the full syntax of the comment.
/**
* @MyTagName:alias {type} [my,extra,data] my description
* my content
*/
However, the only mandatory part of it is @MyTagName
, so it can be as simple as this:
/**
* @MyTag
*/
Multiple tags can alsoe exist in the same comment:
/**
* @Tag
*/
/**
* @MyTag
* @MyOtherTag
* @YetAnotherOne
*/
Valid comment styles are:
/**
* @Tag:alias {type} [my,extra,data] my description
* Content
*/
/** @Tag:alias {type} [my,extra,data] my description
Content
*/
/*
* @Tag:alias {type} [my,extra,data] my description
* Content
*/
/** @Tag:alias {type} [my,extra,data] my description */
/* @Tag:alias {type} [ my , extra, data] my description */
Note: Some of them do not support content
Note: Yes you can use spaces inside { type }
and [ my , extra, data]
Following text
...
My code or whatever else stuff
/**
* @MyTagName:alias {type} [my,extra,data] my description
* my content
*/
My code or whatever else stuff
/**
* @EmptyTag
*/
My code or whatever else stuff
/**
* @EmptyTag {mytype!}
*/
My code or whatever else stuff
...
will be parsed into docsJSON which is following:
[
{
"tag": "MyTagName",
"alias": "alias",
"type": "type",
"required": false,
"extras": [
"my",
"extra",
"data"
],
"description": "my description",
"content": "my content"
},
{
"tag": "EmptyTag",
"alias": "",
"type": "",
"required": false,
"extras": [],
"description": "",
"content": ""
},
{
"tag": "EmptyTag",
"alias": "",
"type": "mytype",
"required": true,
"extras": [],
"description": "",
"content": ""
}
]
!
mark.If you need to use the parser for your own purposes, you can import and use it like this:
import { commentParser } from "comment-to-doc";
commentParser(text, tags);
The tag type is following:
type Tag = {
tag: string,
render?: (parsedComment: ParsedComment) => string,
children?: Tag[]
}
Suppose we have a file called file.ts
on root level with following comment:
/**
* @MyTagName {Singer} [Eminem, Marshall Mathers] He makes good music
* Eminem said once:
* > The truth is you don't know what is going to happen tomorrow. Life is a crazy ride, and nothing is guaranteed. I am whatever you say I am; if I wasn't, then why would you say I am.
*
* @Image [https://upload.wikimedia.org/wikipedia/commons/4/46/Eminem.png, Eminem] Image
*/
To define a single tag, give it a name and define how to render it into documentation block.
const tag = {
tag: 'MyTagName',
render: ({tag, type, extras, description, content}) => {
return [
`## ${extras[0]}`,
`- Name: ${extras[1]}`,
`- He is ${type}`,
`- ${description}`,
'',
content,
'',
`- created by ${tag}`
].join('\n');
}
}
Specify configuration
const config = {
files: [
'example.ts',
],
tags: [tag]
};
Run generator
generateDocs(config);
This will be the output: example.md
Usage: comment-to-doc [options]
Options:
-c, --config <path> Path to configuration file.
-i, --info Include more info about generation results.
-h, --help display help for command
By default the module with search for configuration in ./comment-to-doc.config.js
,
comment-to-doc.config.js
)This is the very same configuration described above in Configuration section
const { defaultTags } = require('./lib')
module.exports = {
files: [
'./tests-input.tsx',
],
tags: defaultTags,
output: () => './cli-output.md'
};
For usage, see:
tests-input.tsx
tests-output.md
npm install
tests-input.tsx
filenpm run try
tests-output.md
for resultsnpm test
to run tests
If you come up with your own cool tags, you can send me a link to your repository, so I could add it here in this documentation.
FAQs
Module for parsing and converting code comments into documentation
We found that comment-to-doc demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.