{"id":15646882,"url":"https://github.com/jonschlinkert/extract-comments","last_synced_at":"2025-05-07T15:22:02.059Z","repository":{"id":17240577,"uuid":"20009723","full_name":"jonschlinkert/extract-comments","owner":"jonschlinkert","description":"Extract JavaScript code comments from a string or glob of files.","archived":false,"fork":false,"pushed_at":"2018-11-24T10:55:59.000Z","size":399,"stargazers_count":49,"open_issues_count":3,"forks_count":10,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-15T10:57:51.845Z","etag":null,"topics":["code-comments","comments","esprima","extract","javascript","jonschlinkert","nodejs","parse","tokenize"],"latest_commit_sha":null,"homepage":"https://github.com/jonschlinkert","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jonschlinkert.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-05-21T06:02:09.000Z","updated_at":"2024-11-27T02:37:40.000Z","dependencies_parsed_at":"2022-08-22T18:20:35.346Z","dependency_job_id":null,"html_url":"https://github.com/jonschlinkert/extract-comments","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonschlinkert%2Fextract-comments","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonschlinkert%2Fextract-comments/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonschlinkert%2Fextract-comments/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonschlinkert%2Fextract-comments/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jonschlinkert","download_url":"https://codeload.github.com/jonschlinkert/extract-comments/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252902749,"owners_count":21822300,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["code-comments","comments","esprima","extract","javascript","jonschlinkert","nodejs","parse","tokenize"],"created_at":"2024-10-03T12:15:34.268Z","updated_at":"2025-05-07T15:22:02.034Z","avatar_url":"https://github.com/jonschlinkert.png","language":"JavaScript","readme":"# extract-comments [![NPM version](https://img.shields.io/npm/v/extract-comments.svg?style=flat)](https://www.npmjs.com/package/extract-comments) [![NPM monthly downloads](https://img.shields.io/npm/dm/extract-comments.svg?style=flat)](https://npmjs.org/package/extract-comments) [![NPM total downloads](https://img.shields.io/npm/dt/extract-comments.svg?style=flat)](https://npmjs.org/package/extract-comments) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/extract-comments.svg?style=flat\u0026label=Travis)](https://travis-ci.org/jonschlinkert/extract-comments)\n\n\u003e Uses esprima to extract line and block comments from a string of JavaScript. Also optionally parses code context (the next line of code after a comment).\n\nPlease consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support.\n\n## Install\n\nInstall with [npm](https://www.npmjs.com/):\n\n```sh\n$ npm install --save extract-comments\n```\n\n## Usage\n\n```js\nvar extract = require('extract-comments');\n\n// pass a string of JavaScript\nextract(string);\n```\n\n**Example**\n\n```js\nvar str = '/**\\n * this is\\n *\\n * a comment\\n*/\\n\\n\\nvar foo = \"bar\";\\n';\nvar comments = extract(str);\nconsole.log(comments);\n\n[{\n  type: 'block',\n  raw: '/**\\n * this is\\n *\\n * a comment\\n*/',\n  value: 'this is\\na comment',\n  loc: { start: { line: 1, column: 0 }, end: { line: 5, column: 33 } },\n  code:\n   { line: 7,\n     loc: { start: { line: 7, column: 36 }, end: { line: 7, column: 52 } },\n     value: 'var foo = \"bar\";' }\n```\n\n## Extractors\n\nBy default, [esprima](http://esprima.org) is used for extracting comments. This can easily be changed by passing a function to `options.extractor`.\n\n**The easy way**\n\nUse a published module, such as:\n\n* [babel-extract-comments](https://github.com/jonschlinkert/babel-extract-comments)\n* [esprima-extract-comments](https://github.com/jonschlinkert/esprima-extract-comments)\n* [espree-extract-comments](https://github.com/jonschlinkert/espree-extract-comments)\n\nExample:\n\n```js\nextract(str, {extractor: require('babel-extract-comments')});\n```\n\nIf you create a compatible extractor, feel free to do pr [or create an issue](https://github.com/jonschlinkert/extract-comments/issues/new) to add it to the readme!\n\n**Roll your own**\n\n```js\nextract(str, {\n  extractor: function(str) {\n    // must return an array of tokens with:\n    // - type: 'Block', 'CommentBlock', 'Line' or 'CommentLine'\n    // - value: the comment inner string\n    // - loc: with `start` and `end` line and column\n    // example:\n    return [\n      { \n        type: 'Block',\n        {start: { line: 1, column: 0 },\n          end: { line: 5, column: 33 }},\n        value: ' this is a comment string '\n      }\n    ];\n  }\n});\n```\n\n## API\n\n### [extract](index.js#L26)\n\nExtract comments from the given `string`.\n\n**Params**\n\n* `string` **{String}**\n* `options` **{Object}**: Pass `first: true` to return after the first comment is found.\n* `tranformFn` **{Function}**: (optional) Tranform function to modify each comment\n* `returns` **{Array}**: Returns an array of comment objects\n\n**Example**\n\n```js\nconst extract = require('extract-comments');\nconsole.log(extract(string, options));\n```\n\n### [.block](index.js#L44)\n\nExtract block comments from the given `string`.\n\n**Params**\n\n* `string` **{String}**\n* `options` **{Object}**: Pass `first: true` to return after the first comment is found.\n* `returns` **{String}**\n\n**Example**\n\n```js\nconsole.log(extract.block(string, options));\n```\n\n### [.line](index.js#L61)\n\nExtract line comments from the given `string`.\n\n**Params**\n\n* `string` **{String}**\n* `options` **{Object}**: Pass `first: true` to return after the first comment is found.\n* `returns` **{String}**\n\n**Example**\n\n```js\nconsole.log(extract.line(string, options));\n```\n\n### [.first](index.js#L78)\n\nExtract the first comment from the given `string`.\n\n**Params**\n\n* `string` **{String}**\n* `options` **{Object}**: Pass `first: true` to return after the first comment is found.\n* `returns` **{String}**\n\n**Example**\n\n```js\nconsole.log(extract.first(string, options));\n```\n\n## Release history\n\n**v0.10.0**\n\n* Parsing is now handled by esprima, so only JavaScript can be parsed. I'm working on parsers for other languages and will cross-link those here when they're pushed up.\n* Breaking change: since parsing is now done by esprima, on both the line and block comment objects, the `loc.start.pos` and `loc.end.pos` properties have been renamed to `loc.start.column` and `loc.end.column`.\n\n**v0.9.0**\n\n* Breaking change: `lines` property was removed from `Block` comments, since this can easily be done by splitting `value`\n\n## About\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cstrong\u003eContributing\u003c/strong\u003e\u003c/summary\u003e\n\nPull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cstrong\u003eRunning Tests\u003c/strong\u003e\u003c/summary\u003e\n\nRunning 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:\n\n```sh\n$ npm install \u0026\u0026 npm test\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cstrong\u003eBuilding docs\u003c/strong\u003e\u003c/summary\u003e\n\n_(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_\n\nTo generate the readme, run the following command:\n\n```sh\n$ npm install -g verbose/verb#dev verb-generate-readme \u0026\u0026 verb\n```\n\n\u003c/details\u003e\n\n### Related projects\n\nYou might also be interested in these projects:\n\n* [babel-extract-comments](https://www.npmjs.com/package/babel-extract-comments): Uses babel (babylon) to extract JavaScript code comments from a JavaScript string or file. | [homepage](https://github.com/jonschlinkert/babel-extract-comments \"Uses babel (babylon) to extract JavaScript code comments from a JavaScript string or file.\")\n* [code-context](https://www.npmjs.com/package/code-context): Parse a string of javascript to determine the context for functions, variables and comments based… [more](https://github.com/jonschlinkert/code-context) | [homepage](https://github.com/jonschlinkert/code-context \"Parse a string of javascript to determine the context for functions, variables and comments based on the code that follows.\")\n* [espree-extract-comments](https://www.npmjs.com/package/espree-extract-comments): Uses espree to extract JavaScript code comments from a string. Returns an array of comment… [more](https://github.com/jonschlinkert/espree-extract-comments) | [homepage](https://github.com/jonschlinkert/espree-extract-comments \"Uses espree to extract JavaScript code comments from a string. Returns an array of comment objects, with line, column, index, comment type and comment string.\")\n* [esprima-extract-comments](https://www.npmjs.com/package/esprima-extract-comments): Extract code comments from string or from a glob of files using esprima. | [homepage](https://github.com/jonschlinkert/esprima-extract-comments \"Extract code comments from string or from a glob of files using esprima.\")\n* [parse-comments](https://www.npmjs.com/package/parse-comments): Parse code comments from JavaScript or any language that uses the same format. | [homepage](https://github.com/jonschlinkert/parse-comments \"Parse code comments from JavaScript or any language that uses the same format.\")\n\n### Contributors\n\n| **Commits** | **Contributor** | \n| --- | --- |\n| 93 | [jonschlinkert](https://github.com/jonschlinkert) |\n| 3 | [cazzer](https://github.com/cazzer) |\n| 1 | [architectcodes](https://github.com/architectcodes) |\n\n### Author\n\n**Jon Schlinkert**\n\n* [linkedin/in/jonschlinkert](https://linkedin.com/in/jonschlinkert)\n* [github/jonschlinkert](https://github.com/jonschlinkert)\n* [twitter/jonschlinkert](https://twitter.com/jonschlinkert)\n\n### License\n\nCopyright © 2018, [Jon Schlinkert](https://github.com/jonschlinkert).\nReleased under the [MIT License](LICENSE).\n\n***\n\n_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on February 12, 2018._","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonschlinkert%2Fextract-comments","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjonschlinkert%2Fextract-comments","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonschlinkert%2Fextract-comments/lists"}