From 0e290efb22f09cea0f630d51f890bf19c08b26fb Mon Sep 17 00:00:00 2001 From: Gregory Danielson Date: Sun, 17 Sep 2023 17:26:44 -0500 Subject: [PATCH 1/5] 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'); + }) + }); From 76a76dce097a12a40f2770859914f7fbda1b88a4 Mon Sep 17 00:00:00 2001 From: Gregory Danielson Date: Sun, 17 Sep 2023 18:44:53 -0500 Subject: [PATCH 2/5] fix: Trim empty sections before joining to avoid spare newlines in document --- lib/insert.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/insert.js b/lib/insert.js index 9b4382e..c06881e 100644 --- a/lib/insert.js +++ b/lib/insert.js @@ -50,6 +50,9 @@ module.exports = function insert(str, options) { sections.splice(1, 0, open + toc(last, options).content + '\n\n' + close); } + // Trim empty sections to avoid adding newlines to document in join + sections = sections.filter((section) => section.length > 0); + var resultString = sections.join('\n\n') + newlines; // if front-matter was found, put it back now if (obj) { From 301a4d815c5a2ec1631fd14f772ca1bb9a163b79 Mon Sep 17 00:00:00 2001 From: Gregory Danielson Date: Sun, 17 Sep 2023 18:49:51 -0500 Subject: [PATCH 3/5] chore: Add test for commented header (closes #178; already fixed) --- test/expected/commented-header.md | 12 ++++++++++++ test/fixtures/commented-header.md | 7 +++++++ test/test.js | 4 ++++ 3 files changed, 23 insertions(+) create mode 100644 test/expected/commented-header.md create mode 100644 test/fixtures/commented-header.md diff --git a/test/expected/commented-header.md b/test/expected/commented-header.md new file mode 100644 index 0000000..5754fb1 --- /dev/null +++ b/test/expected/commented-header.md @@ -0,0 +1,12 @@ + + +- [First Heading](#first-heading) +- [Second Heading](#second-heading) + + + +# First Heading + + + +# Second Heading \ No newline at end of file diff --git a/test/fixtures/commented-header.md b/test/fixtures/commented-header.md new file mode 100644 index 0000000..6722418 --- /dev/null +++ b/test/fixtures/commented-header.md @@ -0,0 +1,7 @@ + + +# First Heading + + + +# Second Heading \ No newline at end of file diff --git a/test/test.js b/test/test.js index 055e804..f29e77f 100644 --- a/test/test.js +++ b/test/test.js @@ -431,4 +431,8 @@ describe('toc.insert', function() { assert.equal(toc.insert('---\nExample\n'), '---\nExample\n'); }) + it('should not generate a link to a commented heading', function() { + assert.equal(toc.insert(read('test/fixtures/commented-header.md')), read('test/expected/commented-header.md')); + }) + }); From 57970fc14e211e6409b6570a141f3ff537a5a92c Mon Sep 17 00:00:00 2001 From: Gregory Danielson Date: Tue, 19 Sep 2023 10:50:47 -0500 Subject: [PATCH 4/5] chore: Add test for inline code in header (closes #170; already fixed) --- test/expected/inline-code.md | 9 +++++++++ test/fixtures/inline-code.md | 5 +++++ test/test.js | 5 +++++ 3 files changed, 19 insertions(+) create mode 100644 test/expected/inline-code.md create mode 100644 test/fixtures/inline-code.md diff --git a/test/expected/inline-code.md b/test/expected/inline-code.md new file mode 100644 index 0000000..3f6380f --- /dev/null +++ b/test/expected/inline-code.md @@ -0,0 +1,9 @@ + + +- [The `log-in` element](#the-log-in-element) + + + +## The `log-in` element + +Lorem ipsum dolor sit amet, qui minim labore adipisicing minim sint cillum sint consectetur cupidatat. \ No newline at end of file diff --git a/test/fixtures/inline-code.md b/test/fixtures/inline-code.md new file mode 100644 index 0000000..e904922 --- /dev/null +++ b/test/fixtures/inline-code.md @@ -0,0 +1,5 @@ + + +## The `log-in` element + +Lorem ipsum dolor sit amet, qui minim labore adipisicing minim sint cillum sint consectetur cupidatat. \ No newline at end of file diff --git a/test/test.js b/test/test.js index f29e77f..7e6f7c0 100644 --- a/test/test.js +++ b/test/test.js @@ -435,4 +435,9 @@ describe('toc.insert', function() { assert.equal(toc.insert(read('test/fixtures/commented-header.md')), read('test/expected/commented-header.md')); }) + + it('should not strip inline code', function() { + assert.equal(toc.insert(read('test/fixtures/inline-code.md')), read('test/expected/inline-code.md')) + }) + }); From a800ab7cad4972b4251f44b6fceb18515bc6899b Mon Sep 17 00:00:00 2001 From: Gregory Danielson Date: Tue, 19 Sep 2023 10:55:37 -0500 Subject: [PATCH 5/5] chore: Add test for symbols in heading (closes #171; already fixed) --- test/expected/symbols-in-heading.md | 14 ++++++++++++++ test/fixtures/symbols-in-heading.md | 9 +++++++++ test/test.js | 4 ++++ 3 files changed, 27 insertions(+) create mode 100644 test/expected/symbols-in-heading.md create mode 100644 test/fixtures/symbols-in-heading.md diff --git a/test/expected/symbols-in-heading.md b/test/expected/symbols-in-heading.md new file mode 100644 index 0000000..500a4c1 --- /dev/null +++ b/test/expected/symbols-in-heading.md @@ -0,0 +1,14 @@ + + +- [Example Heading (With Parenthetic Comment)](#example-heading-with-parenthetic-comment) +- [path/to/file.txt](#pathtofiletxt) + + + +## Example Heading (With Parenthetic Comment) + +Lorem ipsum dolor sit amet, qui minim labore adipisicing minim sint cillum sint consectetur cupidatat. + +## path/to/file.txt + +Lorem ipsum dolor sit amet, qui minim labore adipisicing minim sint cillum sint consectetur cupidatat. \ No newline at end of file diff --git a/test/fixtures/symbols-in-heading.md b/test/fixtures/symbols-in-heading.md new file mode 100644 index 0000000..82ac6d3 --- /dev/null +++ b/test/fixtures/symbols-in-heading.md @@ -0,0 +1,9 @@ + + +## Example Heading (With Parenthetic Comment) + +Lorem ipsum dolor sit amet, qui minim labore adipisicing minim sint cillum sint consectetur cupidatat. + +## path/to/file.txt + +Lorem ipsum dolor sit amet, qui minim labore adipisicing minim sint cillum sint consectetur cupidatat. \ No newline at end of file diff --git a/test/test.js b/test/test.js index 7e6f7c0..fe231e6 100644 --- a/test/test.js +++ b/test/test.js @@ -440,4 +440,8 @@ describe('toc.insert', function() { assert.equal(toc.insert(read('test/fixtures/inline-code.md')), read('test/expected/inline-code.md')) }) + it('should strip symbols instead of making them dashes in slug', function() { + assert.equal(toc.insert(read('test/fixtures/symbols-in-heading.md')), read('test/expected/symbols-in-heading.md')) + }) + });