diff --git a/cli.js b/cli.js index 87dd4c0..ddf7843 100755 --- a/cli.js +++ b/cli.js @@ -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'], default: { firsth1: true, stripHeadingTags: true diff --git a/lib/insert.js b/lib/insert.js index 67f76ed..b0d4033 100644 --- a/lib/insert.js +++ b/lib/insert.js @@ -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 || /(?:)/g; var open = typeof options.open === 'string' ? options.open : '\n\n'; var close = typeof options.close === 'string' ? options.close : ''; diff --git a/test/expected/insert-regex.md b/test/expected/insert-regex.md new file mode 100644 index 0000000..b7485b4 --- /dev/null +++ b/test/expected/insert-regex.md @@ -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. diff --git a/test/expected/replace-existing-regex.md b/test/expected/replace-existing-regex.md new file mode 100644 index 0000000..b7485b4 --- /dev/null +++ b/test/expected/replace-existing-regex.md @@ -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. diff --git a/test/fixtures/insert-regex.md b/test/fixtures/insert-regex.md new file mode 100644 index 0000000..abc0661 --- /dev/null +++ b/test/fixtures/insert-regex.md @@ -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. + diff --git a/test/fixtures/replace-existing-regex.md b/test/fixtures/replace-existing-regex.md new file mode 100644 index 0000000..b71f970 --- /dev/null +++ b/test/fixtures/replace-existing-regex.md @@ -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. + diff --git a/test/test.js b/test/test.js index e93e41b..38a4634 100644 --- a/test/test.js +++ b/test/test.js @@ -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'));