From e149e382d2f058769e0b1b3f6d778e8986579272 Mon Sep 17 00:00:00 2001
From: Taylor Hoff <primdevs@protonmail.com>
Date: Wed, 13 Dec 2017 20:35:25 -0500
Subject: [PATCH 1/2] Allow custom open and close parameters

---
 .verb.md                                |  3 +++
 cli.js                                  |  7 ++++--
 index.js                                |  1 +
 lib/insert.js                           | 10 ++++++++
 package.json                            |  2 +-
 test/expected/insert-regex.md           | 31 +++++++++++++++++++++++++
 test/expected/replace-existing-regex.md | 31 +++++++++++++++++++++++++
 test/fixtures/insert-regex.md           | 25 ++++++++++++++++++++
 test/fixtures/replace-existing-regex.md | 29 +++++++++++++++++++++++
 test/test.js                            | 16 +++++++++++++
 10 files changed, 152 insertions(+), 3 deletions(-)
 create mode 100644 test/expected/insert-regex.md
 create mode 100644 test/expected/replace-existing-regex.md
 create mode 100644 test/fixtures/insert-regex.md
 create mode 100644 test/fixtures/replace-existing-regex.md

diff --git a/.verb.md b/.verb.md
index 653bb6c..bdd7611 100644
--- a/.verb.md
+++ b/.verb.md
@@ -30,6 +30,9 @@ Usage: markdown-toc [options] <input>
 
   --no-stripHeadingTags: Do not strip extraneous HTML tags from heading
                          text before slugifying
+
+  --indent:     Provide the indentation to use - defaults to '  '
+                (to specify a tab, use the bash-escaped $'\t')
 ```
 
 ## Highlights
diff --git a/cli.js b/cli.js
index b601704..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'],
+  string: ['append', 'bullets', 'indent', 'open', 'close'],
   default: {
     firsth1: true,
     stripHeadingTags: true
@@ -36,7 +36,10 @@ if (args._.length !== 1) {
     '  --no-firsth1: Include the first h1-level heading in a file',
     '',
     '  --no-stripHeadingTags: Do not strip extraneous HTML tags from heading',
-    '                         text before slugifying'
+    '                         text before slugifying',
+    '',  
+    '  --indent:     Provide the indentation to use - defaults to \'  \'',
+    '                (to specify a tab, use the bash-escaped $\'\\t\')'
   ].join('\n'));
   process.exit(1);
 }
diff --git a/index.js b/index.js
index 109eb37..90cb8be 100644
--- a/index.js
+++ b/index.js
@@ -246,6 +246,7 @@ function strip(str, opts) {
  * Expose utils
  */
 
+toc.utils = utils;
 toc.bullets = bullets;
 toc.linkify = linkify;
 toc.slugify = utils.slugify;
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 || /(?:<!-- toc(?:\s*stop)? -->)/g;
   var open = typeof options.open === 'string' ? options.open : '<!-- toc -->\n\n';
   var close = typeof options.close === 'string' ? options.close : '<!-- tocstop -->';
diff --git a/package.json b/package.json
index 9280c8b..2c870b4 100644
--- a/package.json
+++ b/package.json
@@ -49,7 +49,7 @@
   "dependencies": {
     "concat-stream": "^1.5.2",
     "diacritics-map": "^0.1.0",
-    "gray-matter": "^2.1.0",
+    "gray-matter": "^3.1.1",
     "lazy-cache": "^2.0.2",
     "list-item": "^1.1.1",
     "markdown-link": "^0.1.1",
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'));

From 12fd1d3ac7ace46ecc81f94719874627223156dd Mon Sep 17 00:00:00 2001
From: Taylor Hoff <PrimordialHelios@users.noreply.github.com>
Date: Thu, 21 Dec 2017 06:12:03 -0500
Subject: [PATCH 2/2] Update cli.js

---
 cli.js | 1 +
 1 file changed, 1 insertion(+)

diff --git a/cli.js b/cli.js
index d05bb14..ddf7843 100755
--- a/cli.js
+++ b/cli.js
@@ -4,6 +4,7 @@ var fs = require('fs');
 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', 'open', 'close'],
   default: {
     firsth1: true,