Skip to content

Commit 0e290ef

Browse files
committed
fix: Correctly detect frontmatter to avoid mangling document
The previous format would incorrectly capture: - A single horizontal rule at the top of the file - Three literal dashes with arbitrary content after them. This was detected as a frontmatter, and an extra HR would be inserted after as a result. The new format requires that there are two sets of triple dashes on their own lines, where the first is the first text in the document.
1 parent 386b447 commit 0e290ef

File tree

3 files changed

+7
-1
lines changed

3 files changed

+7
-1
lines changed

lib/insert.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ module.exports = function insert(str, options) {
2828
if (m) newlines = m[0];
2929

3030
// does the file have front-matter?
31-
if (/^---/.test(str)) {
31+
if (/^---\n.*\n---\n/.test(str)) {
3232
// extract it temporarily so the syntax
3333
// doesn't get mistaken for a heading
3434
obj = utils.matter(str);

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"David Mohl (https://dvcrn.github.io)",
1515
"Federico Soave (https://github.com/Feder1co5oave)",
1616
"Gary Green (https://github.com/garygreen)",
17+
"Gregory Danielson III (https://gregdan3.dev)",
1718
"Jon Schlinkert (http://twitter.com/jonschlinkert)",
1819
"Josh Duff (https://tehshrike.github.io)",
1920
"Matt Ellis (http://sticklebackplastic.com)",

test/test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,4 +426,9 @@ describe('toc.insert', function() {
426426
assert.equal(strip(toc.insert(str, { linkify: true })), read('test/expected/insert.md'));
427427
assert.equal(strip(toc.insert(str, { linkify: false })), read('test/expected/insert-no-links.md'));
428428
});
429+
430+
it('should not mangle a file with an initial horizontal rule', function() {
431+
assert.equal(toc.insert('---\nExample\n'), '---\nExample\n');
432+
})
433+
429434
});

0 commit comments

Comments
 (0)