Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom open and close parameters #110

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var toc = require('./index.js');
var utils = require('./lib/utils');
var args = utils.minimist(process.argv.slice(2), {
boolean: ['i', 'json', 'firsth1', 'stripHeadingTags'],
string: ['append', 'bullets', 'indent'],
string: ['append', 'bullets', 'indent', 'open', 'close'],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PrimordialHelios there was a merge conflict here from a previous PR. I fixed it by adding , 'indent'.

default: {
firsth1: true,
stripHeadingTags: true
Expand Down
10 changes: 10 additions & 0 deletions lib/insert.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ var utils = require('./utils');
module.exports = function insert(str, options) {
options = options || {};

// Create RegEx based on open and close parameters
if (options.open && options.close) {
// Sanitize open and close to create a regex
options.regex = new RegExp(
options.open.replace(/[#-.]|[[-^]|[?|{}]/g, '\\$&') + '|' +
options.close.replace(/[#-.]|[[-^]|[?|{}]/g, '\\$&'), 'g'
);
// Add newlines to toc opening
options.open += '\n\n';
}
var regex = options.regex || /(?:<!-- toc(?:\s*stop)? -->)/g;
var open = typeof options.open === 'string' ? options.open : '<!-- toc -->\n\n';
var close = typeof options.close === 'string' ? options.close : '<!-- tocstop -->';
Expand Down
31 changes: 31 additions & 0 deletions test/expected/insert-regex.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
name: Test
---
# Test

> This is a test!

[comment]: <> (toc)

- [Quickstart](#quickstart)
- [Options](#options)
- [Usage examples](#usage-examples)
- [Contributing](#contributing)
- [Author](#author)

[comment]: <> (tocstop)

## Quickstart
This is the quickstart section.

## Options
This is the options section.

## Usage examples
This is the usage examples section.

## Contributing
This is the Contributing section.

## Author
This is the Author section.
31 changes: 31 additions & 0 deletions test/expected/replace-existing-regex.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
name: Test
---
# Test

> This is a test!

[comment]: <> (toc)

- [Quickstart](#quickstart)
- [Options](#options)
- [Usage examples](#usage-examples)
- [Contributing](#contributing)
- [Author](#author)

[comment]: <> (tocstop)

## Quickstart
This is the quickstart section.

## Options
This is the options section.

## Usage examples
This is the usage examples section.

## Contributing
This is the Contributing section.

## Author
This is the Author section.
25 changes: 25 additions & 0 deletions test/fixtures/insert-regex.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
name: Test
---

# Test

> This is a test!

[comment]: <> (toc)

## Quickstart
This is the quickstart section.

## Options
This is the options section.

## Usage examples
This is the usage examples section.

## Contributing
This is the Contributing section.

## Author
This is the Author section.

29 changes: 29 additions & 0 deletions test/fixtures/replace-existing-regex.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
name: Test
---

# Test

> This is a test!

[comment]: <> (toc)
- old toc line 1
- old toc line 2
- old toc line 3
[comment]: <> (tocstop)

## Quickstart
This is the quickstart section.

## Options
This is the options section.

## Usage examples
This is the usage examples section.

## Contributing
This is the Contributing section.

## Author
This is the Author section.

16 changes: 16 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,22 @@ describe('toc.insert', function() {
assert.equal(strip(toc.insert(str)), read('test/expected/replace-existing.md'));
});

it('should insert a markdown TOC beneath a `[comment]: <> (toc)` comment.', function() {
var str = read('test/fixtures/insert-regex.md');
assert.equal(strip(toc.insert(str, {
open: '[comment]: <> (toc)',
close: '[comment]: <> (tocstop)'
})), read('test/expected/insert-regex.md'));
});

it('should replace an old TOC between `[comment]: <> (toc)...[comment]: <> (tocstop)` comments.', function() {
var str = read('test/fixtures/replace-existing-regex.md');
assert.equal(strip(toc.insert(str, {
open: '[comment]: <> (toc)',
close: '[comment]: <> (tocstop)'
})), read('test/expected/replace-existing-regex.md'));
});

it('should insert the toc passed on the options.', function() {
var str = read('test/fixtures/replace-existing.md');
assert.equal(strip(toc.insert(str, {toc: toc(str).content})), read('test/expected/replace-existing.md'));
Expand Down