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

Add tests for several already resolved open issues #191

Open
wants to merge 5 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
5 changes: 4 additions & 1 deletion lib/insert.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand Down
12 changes: 12 additions & 0 deletions test/expected/commented-header.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!-- toc -->

- [First Heading](#first-heading)
- [Second Heading](#second-heading)

<!-- tocstop -->

# First Heading

<!-- # Commented Heading -->

# Second Heading
9 changes: 9 additions & 0 deletions test/expected/inline-code.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!-- toc -->

- [The `log-in` element](#the-log-in-element)

<!-- tocstop -->

## The `log-in` element

Lorem ipsum dolor sit amet, qui minim labore adipisicing minim sint cillum sint consectetur cupidatat.
14 changes: 14 additions & 0 deletions test/expected/symbols-in-heading.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!-- toc -->

- [Example Heading (With Parenthetic Comment)](#example-heading-with-parenthetic-comment)
- [path/to/file.txt](#pathtofiletxt)

<!-- tocstop -->

## 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.
7 changes: 7 additions & 0 deletions test/fixtures/commented-header.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!-- toc -->

# First Heading

<!-- # Commented Heading -->

# Second Heading
5 changes: 5 additions & 0 deletions test/fixtures/inline-code.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!-- toc -->

## The `log-in` element

Lorem ipsum dolor sit amet, qui minim labore adipisicing minim sint cillum sint consectetur cupidatat.
9 changes: 9 additions & 0 deletions test/fixtures/symbols-in-heading.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!-- toc -->

## 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.
18 changes: 18 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
})

});