From 0e290efb22f09cea0f630d51f890bf19c08b26fb Mon Sep 17 00:00:00 2001 From: Gregory Danielson Date: Sun, 17 Sep 2023 17:26:44 -0500 Subject: [PATCH] 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. --- lib/insert.js | 2 +- package.json | 1 + test/test.js | 5 +++++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/insert.js b/lib/insert.js index 67f76ed..9b4382e 100644 --- a/lib/insert.js +++ b/lib/insert.js @@ -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); diff --git a/package.json b/package.json index 25d988d..90a06c4 100644 --- a/package.json +++ b/package.json @@ -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)", diff --git a/test/test.js b/test/test.js index e93e41b..055e804 100644 --- a/test/test.js +++ b/test/test.js @@ -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'); + }) + });