diff --git a/lib/insert.js b/lib/insert.js index 67f76ed..c06881e 100644 --- a/lib/insert.js +++ b/lib/insert.js @@ -28,7 +28,7 @@ module.exports = function insert(str, options) { if (m) newlines = m[0]; // does the file have front-matter? - if (/^---/.test(str)) { + if (/^---\n.*\n---\n/.test(str)) { // extract it temporarily so the syntax // doesn't get mistaken for a heading obj = utils.matter(str); @@ -50,6 +50,9 @@ module.exports = function insert(str, options) { sections.splice(1, 0, open + toc(last, options).content + '\n\n' + close); } + // Trim empty sections to avoid adding newlines to document in join + sections = sections.filter((section) => section.length > 0); + var resultString = sections.join('\n\n') + newlines; // if front-matter was found, put it back now if (obj) { diff --git a/package.json b/package.json index 25d988d..90a06c4 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "David Mohl (https://dvcrn.github.io)", "Federico Soave (https://github.com/Feder1co5oave)", "Gary Green (https://github.com/garygreen)", + "Gregory Danielson III (https://gregdan3.dev)", "Jon Schlinkert (http://twitter.com/jonschlinkert)", "Josh Duff (https://tehshrike.github.io)", "Matt Ellis (http://sticklebackplastic.com)", diff --git a/test/expected/commented-header.md b/test/expected/commented-header.md new file mode 100644 index 0000000..5754fb1 --- /dev/null +++ b/test/expected/commented-header.md @@ -0,0 +1,12 @@ + + +- [First Heading](#first-heading) +- [Second Heading](#second-heading) + + + +# First Heading + + + +# Second Heading \ No newline at end of file diff --git a/test/expected/inline-code.md b/test/expected/inline-code.md new file mode 100644 index 0000000..3f6380f --- /dev/null +++ b/test/expected/inline-code.md @@ -0,0 +1,9 @@ + + +- [The `log-in` element](#the-log-in-element) + + + +## The `log-in` element + +Lorem ipsum dolor sit amet, qui minim labore adipisicing minim sint cillum sint consectetur cupidatat. \ No newline at end of file diff --git a/test/expected/symbols-in-heading.md b/test/expected/symbols-in-heading.md new file mode 100644 index 0000000..500a4c1 --- /dev/null +++ b/test/expected/symbols-in-heading.md @@ -0,0 +1,14 @@ + + +- [Example Heading (With Parenthetic Comment)](#example-heading-with-parenthetic-comment) +- [path/to/file.txt](#pathtofiletxt) + + + +## Example Heading (With Parenthetic Comment) + +Lorem ipsum dolor sit amet, qui minim labore adipisicing minim sint cillum sint consectetur cupidatat. + +## path/to/file.txt + +Lorem ipsum dolor sit amet, qui minim labore adipisicing minim sint cillum sint consectetur cupidatat. \ No newline at end of file diff --git a/test/fixtures/commented-header.md b/test/fixtures/commented-header.md new file mode 100644 index 0000000..6722418 --- /dev/null +++ b/test/fixtures/commented-header.md @@ -0,0 +1,7 @@ + + +# First Heading + + + +# Second Heading \ No newline at end of file diff --git a/test/fixtures/inline-code.md b/test/fixtures/inline-code.md new file mode 100644 index 0000000..e904922 --- /dev/null +++ b/test/fixtures/inline-code.md @@ -0,0 +1,5 @@ + + +## The `log-in` element + +Lorem ipsum dolor sit amet, qui minim labore adipisicing minim sint cillum sint consectetur cupidatat. \ No newline at end of file diff --git a/test/fixtures/symbols-in-heading.md b/test/fixtures/symbols-in-heading.md new file mode 100644 index 0000000..82ac6d3 --- /dev/null +++ b/test/fixtures/symbols-in-heading.md @@ -0,0 +1,9 @@ + + +## Example Heading (With Parenthetic Comment) + +Lorem ipsum dolor sit amet, qui minim labore adipisicing minim sint cillum sint consectetur cupidatat. + +## path/to/file.txt + +Lorem ipsum dolor sit amet, qui minim labore adipisicing minim sint cillum sint consectetur cupidatat. \ No newline at end of file diff --git a/test/test.js b/test/test.js index e93e41b..fe231e6 100644 --- a/test/test.js +++ b/test/test.js @@ -426,4 +426,22 @@ describe('toc.insert', function() { assert.equal(strip(toc.insert(str, { linkify: true })), read('test/expected/insert.md')); assert.equal(strip(toc.insert(str, { linkify: false })), read('test/expected/insert-no-links.md')); }); + + it('should not mangle a file with an initial horizontal rule', function() { + assert.equal(toc.insert('---\nExample\n'), '---\nExample\n'); + }) + + it('should not generate a link to a commented heading', function() { + assert.equal(toc.insert(read('test/fixtures/commented-header.md')), read('test/expected/commented-header.md')); + }) + + + it('should not strip inline code', function() { + assert.equal(toc.insert(read('test/fixtures/inline-code.md')), read('test/expected/inline-code.md')) + }) + + it('should strip symbols instead of making them dashes in slug', function() { + assert.equal(toc.insert(read('test/fixtures/symbols-in-heading.md')), read('test/expected/symbols-in-heading.md')) + }) + });