Skip to content

Commit

Permalink
fix: Correctly detect frontmatter to avoid mangling document
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
gregdan3 committed Sep 17, 2023
1 parent 386b447 commit 0e290ef
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 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 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');
})

});

0 comments on commit 0e290ef

Please sign in to comment.