Skip to content

Commit 7f179e9

Browse files
author
Manuel Mujica
authored
Merge pull request #14 from bit-docs/headings-container
Allow setting the headings container selector
2 parents ed293f8 + 8172d94 commit 7f179e9

File tree

5 files changed

+153
-34
lines changed

5 files changed

+153
-34
lines changed

readme.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,20 @@ Where `$VERSION` is the latest version on npm.
3131

3232
In your template add a class **on-this-page-container**:
3333

34-
```js
34+
```html
3535
<div class="on-this-page-container"></div>
3636
```
3737

38+
By default, all heading tags children of the first `article` tag on the page will
39+
be collected to create the table of contents; if you want to use a different element
40+
just do:
41+
42+
```html
43+
<div
44+
class="on-this-page-container"
45+
data-heading-container-selector="#my-custom-selector"
46+
>
47+
</div>
48+
```
49+
3850
The table of contents will be injected into this element at run time.

test/test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
require("./make-tree-test");
22
require("./toc-control-test");
3+
require("./toc-container-control-test");
4+

test/toc-container-control-test.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
var $ = require("jquery");
2+
var assert = require("chai/chai").assert;
3+
var TOCContainer = require("../toc-container-control");
4+
5+
require("steal-mocha");
6+
7+
describe("TOCContainer", function() {
8+
var $el, $testArea;
9+
10+
beforeEach(function() {
11+
$("body").append('<div id="toc-container"></div>');
12+
13+
$el = $("#toc-container");
14+
$testArea = $("#test-area");
15+
});
16+
17+
afterEach(function() {
18+
$el.remove();
19+
$testArea.empty();
20+
});
21+
22+
it("by default gets headings from 'article' children", function() {
23+
var headings = [
24+
'<article>',
25+
"<h2>Usage</h2>",
26+
"<h2>Install</h2>",
27+
"<h2>Configure</h2>",
28+
"</article>"
29+
];
30+
31+
// append the headings to the DOM and then instantiate the control
32+
$("body").append(headings.join(""));
33+
new TOCContainer($el.get(0));
34+
35+
var $items = $("#toc-container ul");
36+
assert.equal(
37+
$items.html(),
38+
[
39+
'<li><a href="#usage">Usage</a></li>',
40+
'<li><a href="#install">Install</a></li>',
41+
'<li><a href="#configure">Configure</a></li>',
42+
].join(""),
43+
"should create table of contents from the headings inside container"
44+
);
45+
46+
// remove headings container from the DOM
47+
$("article").remove();
48+
});
49+
50+
it("reads the headings container selector from the element", function() {
51+
// set the container selector as a data-* attribute
52+
$el.attr(
53+
"data-headings-container-selector",
54+
".my-custom-container"
55+
);
56+
57+
var headings = [
58+
'<div class="my-custom-container">',
59+
"<h2>Usage</h2>",
60+
"<h2>Install</h2>",
61+
"<h2>Configure</h2>",
62+
"<h2>Configure</h2>",
63+
"</div>"
64+
];
65+
66+
// append the headings to the DOM and then instantiate the control
67+
$("body").append(headings.join(""));
68+
new TOCContainer($el.get(0));
69+
70+
var $items = $("#toc-container ul");
71+
assert.equal(
72+
$items.html(),
73+
[
74+
'<li><a href="#usage">Usage</a></li>',
75+
'<li><a href="#install">Install</a></li>',
76+
'<li><a href="#configure">Configure</a></li>',
77+
'<li><a href="#configure-1">Configure</a></li>'
78+
].join(""),
79+
"should create table of contents from the headings inside container"
80+
);
81+
82+
// remove headings container from the DOM
83+
$(".my-custom-container").remove();
84+
});
85+
});

toc-container-control.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
var Control = require("can-control");
2+
var TableOfContents = require("./toc-control");
3+
4+
module.exports = Control.extend({
5+
init: function(el) {
6+
el.style.display = "none";
7+
8+
var depth = this.getOutlineDepth();
9+
var tagName = this.getOutlineTagName();
10+
var selector = this.getHeadingsContainerSelector(el);
11+
12+
var toc = document.createElement(tagName);
13+
toc.className = "on-this-page";
14+
el.appendChild(toc);
15+
16+
new TableOfContents(toc, {
17+
depth: depth,
18+
tagName: tagName,
19+
headingsContainerSelector: selector
20+
});
21+
},
22+
23+
getDocObject() {
24+
return window.docObject || {};
25+
},
26+
27+
getOutlineTagName: function() {
28+
var docObject = this.getDocObject();
29+
var outline = docObject.outline || {};
30+
31+
return (outline.tag === "ol") ? "ol" : "ul";
32+
},
33+
34+
getOutlineDepth: function() {
35+
var docObject = this.getDocObject();
36+
var depth = docObject.outline && docObject.outline.depth;
37+
38+
return (typeof depth === "number" ? Math.min(depth, 6) : 1);
39+
},
40+
41+
getHeadingsContainerSelector(el) {
42+
var selector = el.dataset.headingsContainerSelector;
43+
44+
return selector ? selector : "article";
45+
}
46+
});

toc.js

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,9 @@
1-
var Control = require("can-control");
2-
var TableOfContents = require("./toc-control");
1+
var TOCContainer = require("./toc-container-control");
32

4-
var TOCContainer = Control.extend({
5-
init: function(el) {
6-
el.style.display = "none";
3+
var el = document.getElementsByClassName("on-this-page-container");
74

8-
var depth = this.getOutlineDepth();
9-
var tagName = this.getOutlineTagName();
10-
var toc = document.createElement(tagName);
11-
12-
toc.className = "on-this-page";
13-
el.appendChild(toc);
14-
15-
new TableOfContents(toc, {
16-
depth: depth,
17-
tagName: tagName,
18-
headingsContainerSelector: "article"
19-
});
20-
},
21-
22-
getOutlineTagName: function() {
23-
var outline = window.docObject.outline || {};
24-
return (outline.tag === "ol") ? "ol" : "ul";
25-
},
26-
27-
getOutlineDepth: function() {
28-
var depth = window.docObject.outline && window.docObject.outline.depth;
29-
return (typeof depth === "number" ? Math.min(depth, 6) : 1);
30-
}
31-
});
32-
33-
new TOCContainer(
34-
document.getElementsByClassName("on-this-page-container")[0]
35-
);
5+
if (el.length) {
6+
new TOCContainer(el);
7+
} else {
8+
console.log("An element with class 'on-this-page-container' is required");
9+
}

0 commit comments

Comments
 (0)