Skip to content

Commit bb79a6f

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 bb79a6f

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-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);

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)