Skip to content
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
5 changes: 5 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -426,4 +426,9 @@ 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');
})

});