Skip to content
This repository has been archived by the owner on Jan 14, 2022. It is now read-only.

Commit

Permalink
Merge pull request #5 from williamwilling/master
Browse files Browse the repository at this point in the history
h1 can now be the root of the table of contents.
  • Loading branch information
anatoo authored Mar 3, 2017
2 parents 937f3f9 + 1f63d31 commit 3a7b717
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ module.exports = function(options) {
function getRootLevel(headers) {
return headers.map(function(header) {
return header.level;
}).sort()[0] - 1 || 1;
}).sort()[0] - 1;
}

function buildTocItems(headers) {
Expand Down
42 changes: 42 additions & 0 deletions test/fixtures/h1/build/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@




<ol class="toc">

<li><a href="#aaa">aaa</a>

<ol class="toc">

<li><a href="#bbb">bbb</a>

</li>

<li><a href="#ccc">ccc</a>

</li>

</ol>

</li>

<li><a href="#ddd">ddd</a>

</li>

</ol>



<h1 id="aaa">aaa</h1>
<p>paragraph</p>

<h2 id="bbb">bbb</h2>
<p>paragraph</p>

<h2 id="ccc">ccc</h2>
<p>paragraph</p>

<h1 id="ddd">ddd</h1>
<p>paragraph</p>

42 changes: 42 additions & 0 deletions test/fixtures/h1/expected/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@




<ol class="toc">

<li><a href="#aaa">aaa</a>

<ol class="toc">

<li><a href="#bbb">bbb</a>

</li>

<li><a href="#ccc">ccc</a>

</li>

</ol>

</li>

<li><a href="#ddd">ddd</a>

</li>

</ol>



<h1 id="aaa">aaa</h1>
<p>paragraph</p>

<h2 id="bbb">bbb</h2>
<p>paragraph</p>

<h2 id="ccc">ccc</h2>
<p>paragraph</p>

<h1 id="ddd">ddd</h1>
<p>paragraph</p>

16 changes: 16 additions & 0 deletions test/fixtures/h1/src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
title: 'hoge'
autotoc: true
template: 'layout.eco'
---
<h1>aaa</h1>
<p>paragraph</p>

<h2>bbb</h2>
<p>paragraph</p>

<h2>ccc</h2>
<p>paragraph</p>

<h1>ddd</h1>
<p>paragraph</p>
15 changes: 15 additions & 0 deletions test/fixtures/h1/templates/layout.eco
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<% renderToc = (items) => %>
<ol class="toc">
<% for item in items: %>
<li><a href="#<%= item.id %>"><%= item.text %></a>
<% if item.children.length > 0: %><%- renderToc(item.children) %><% end %>
</li>
<% end %>
</ol>
<% end %>

<% if @toc: %>
<%- renderToc(@toc) %>
<% end %>

<%- @contents %>
18 changes: 18 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,22 @@ describe('metalsmith-autotoc', function() {
done();
});
});

it('should allow table of contents to include h1', function(done) {
metalsmith(__dirname + '/fixtures/h1')
.use(autotoc({
selector: 'h1, h2, h3'
}))
.use(templates({
engine: 'eco',
directory: './templates'
}))
.build(function(error) {
if (error) {
throw error;
}
equal('test/fixtures/h1/expected', 'test/fixtures/h1/build');
done();
});
});
});

0 comments on commit 3a7b717

Please sign in to comment.