2.0.1 • Published 5 months ago

@dev-build-deploy/comment-it v2.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

CommentIt - Comment Extraction Library

Extract comment blocks from your files.

Features

  • Simple to use
  • Extracts single- and multiline- comment blocks
  • Supports a range of languages covering the top 25 languages used in GitHub (+ more)

Basic Usage

import { extractComments } from "@dev-build-deploy/comment-it";

const file = "README.md";

// Check if the file is supported by CommentIt
if (isSupported(file)) {
  const config = {
    /** Only consider comments in the first ..n lines */
    maxLines: 20,
    /** Group sequential singleline comments into a comment block */
    groupSingleline: true,
  };

  // Retrieve each comment block using an iterator
  for await (const comment of extractComments(file), /* OPTIONAL */ config) {
    console.log(JSON.stringify(comment, null, 2));
  }
}

The above example will result in:

{
  "type": "multiline",
  "format": { "start": "<!--", "end": "-->"},
  "contents": [
    {
      "line":   1,
      "column": { "start": 0, "end":  4 },
      "raw":    "<!--",
      "value":  ""
    },
    {
      "line":   2,
      "column": { "start": 0, "end": 64 },
      "raw":    "SPDX-FileCopyrightText: 2023 Kevin de Jong <monkaii@hotmail.com>",
      "value":  "SPDX-FileCopyrightText: 2023 Kevin de Jong <monkaii@hotmail.com>"
    },
    {
      "line":   3,
      "column": { "start": 0, "end": 41 },
      "raw":    "SPDX-License-Identifier: MIT",
      "value":  "SPDX-License-Identifier: MIT"
    },
    {
      "line":  4,
      "column": { "start": 0, "end":  3 },
      "raw": "-->",
      "value": ""
    }
  ]
}

{
  "type": "multiline",
  "format": { "start": "<!--", "end": "-->" },
  "contents": [
    {
      "line": 16,
      "column": { "start": 0, "end": 45 },
      "raw": "<!-- Hee hee, hid a comment block in here -->",
      "value": "Hee hee, hid a comment block in here"
    }
  ]
}

Custom Language(s)

You can use the addLanguage() function to add new languages to the validation set:

import { addLanguage } from "@dev-build-deploy/comment-it";

addLanguage({
  name: "Pingu Language",
  filenames: [".pingu"],
  extensions: [".noot"],
  singleline: "{%NOOTNOOT%}",
});

The same function can also be used to override a (default) configuration. For example, the following code snippet replaces the comment prefix from ; to # for files with the.ini file extension:

import { addLanguage } from "@dev-build-deploy/comment-it";

addLanguage({
  name: "Custom ini",
  extensions: [".ini"],
  singleline: "#",
});

Contributing

If you have suggestions for how comment-it could be improved, or want to report a bug, open an issue! We'd love all and any contributions.

For more, check out the Contributing Guide.

License

2.0.1

5 months ago

2.0.0

5 months ago

1.4.2

6 months ago

1.5.0

6 months ago

1.4.1

6 months ago

1.4.0

6 months ago

1.3.0

6 months ago

1.2.0

6 months ago

1.1.1

6 months ago

1.1.0

6 months ago

1.0.0

6 months ago

0.4.2

10 months ago

0.4.1

10 months ago

0.4.0

10 months ago

0.3.0

10 months ago

0.2.0

10 months ago

0.1.2

10 months ago

0.1.1

10 months ago

0.1.0

11 months ago

0.0.1

11 months ago