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 bb79a6f
Show file tree
Hide file tree
Showing 2 changed files with 6 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
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 bb79a6f

Please sign in to comment.