Skip to content

Commit

Permalink
Fix: Fixes #2.
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanfitzer committed Nov 24, 2021
1 parent 354239e commit 10a90b0
Show file tree
Hide file tree
Showing 10 changed files with 281 additions and 239 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: CI

on:
- push
- pull_request

jobs:
test:
name: Node.js ${{ matrix.node-version }} on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
node-version:
- 18
- 16
- 14
- 7
os:
- ubuntu-latest
- macos-latest
- windows-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
.DS_Store
node_modules
.tm_properties
package-lock.json
7 changes: 0 additions & 7 deletions .npmignore

This file was deleted.

1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
25 changes: 6 additions & 19 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,22 +1,9 @@
Copyright (c) 2015 Ryan Fitzer
MIT License

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
Copyright (c) Ryan Fitzer <[email protected]> (ryanfitzer.com)

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
208 changes: 114 additions & 94 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# Comment Serializer #
# Comment Serializer

[![NPM version](https://badge.fury.io/js/comment-serializer.svg)](https://www.npmjs.com/package/comment-serializer) [![Try on RunKit](https://badge.runkitcdn.com/comment-serializer.svg)](https://runkit.com/npm/comment-serializer)

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.
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](https://runkit.com/npm/comment-serializer).


## Usage ##
## Usage

**source-code.txt**:

```txt
//!
// This is the general description.
Expand All @@ -22,39 +22,39 @@ Blah, blah, blah... Some code...
```

**my-serializer.js**

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

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

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

const result = mySerializer( src );
const result = mySerializer(src);
```

## Options

## Options ##
### `options.parsers`

### `options.parsers` ###

- Required: No
- Type: `object`
- 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`](examples/custom-parsers/parsers.js)

#### Example ####
#### Example

Source to parse:

Expand All @@ -69,29 +69,32 @@ The custom parser:
```js
const mySerializer = serializer({
parsers: {
mySpecialTag: ( value ) => value.toUpperCase()
}
mySpecialTag: (value) => value.toUpperCase(),
},
});
```

Result:

```js
[{
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!'
}]
}]

[
{
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:
Expand All @@ -108,89 +111,106 @@ A more advanced example:
```js
const mySerializer = serializer({
parsers: {
'mySpecialTag': ( value ) => {

const match = value.match( /\s*[-*]\s+[\w-_]*/g );

mySpecialTag: (value) => {
const match = value.match(/\s*[-*]\s+[\w-_]*/g);

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

Result:

```js
[{
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'
}]
}]
[
{
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',
},
],
},
];
```

### `options.tokens` ###
##### Tag Parsing Errors

- Required: No
- Type: `object`
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.

Customize the comment delimiters. The default tokens use [JSDoc comment block](https://google.github.io/styleguide/jsguide.html#jsdoc) syntax.
Example:

```js
{
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`

### `options.tokens.commentBegin` ###
- Required: No
- Type: `object`

- Required: No
- Type: `string`
- Default: `'/**'`
Customize the comment delimiters. The default tokens use [JSDoc comment block](https://google.github.io/styleguide/jsguide.html#jsdoc) syntax.

The delimiter marking the beginning of a comment block.
### `options.tokens.commentBegin`

- Required: No
- Type: `string`
- Default: `'/**'`

### `options.tokens.commentLinePrefix` ###
The delimiter marking the beginning of a comment block.

- Required: No
- Type: `string`
- Default: `'*'`
### `options.tokens.commentLinePrefix`

The delimiter marking a new line in the body of a comment block.
- Required: No
- Type: `string`
- Default: `'*'`

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

### `options.tokens.tagPrefix` ###
### `options.tokens.tagPrefix`

- Required: No
- Type: `string`
- Default: `'@'`
- Required: No
- Type: `string`
- Default: `'@'`

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

### `options.tokens.commentEnd`

### `options.tokens.commentEnd` ###

- Required: No
- Type: `string`
- Default: `'*/'`
- Required: No
- Type: `string`
- Default: `'*/'`

The delimiter marking the end of a comment block.



## Token Syntax Support ##
## Token Syntax Support

Comment delimiters:

Expand All @@ -199,18 +219,18 @@ Comment delimiters:
* <- 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.
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:

2. Delimiters should not rely on a whitespace character as a delimiter. For example, the following style would not be supported:
```
/**
An unsupported style.
```
/**
An unsupported style.
@tag
*/
```
@tag
*/
```
Loading

0 comments on commit 10a90b0

Please sign in to comment.