Skip to content

Commit 50a73ff

Browse files
author
Manuel Mujica
committed
Get content list "plugin" working
Maybe this should be a bit-docs-* plugin
1 parent 257e250 commit 50a73ff

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,8 @@
2121
"homepage": "https://github.com/bit-docs/bit-docs-docjs-theme#readme",
2222
"devDependencies": {
2323
"bit-docs-generate-html": "^0.3.3"
24+
},
25+
"dependencies": {
26+
"jquery": "^3.1.1"
2427
}
2528
}

static/docjs.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,69 @@
1+
var $ = require("jquery");
2+
13
require("./styles/styles.less");
4+
5+
$(function() {
6+
var sections = [];
7+
var $element = $(".contents");
8+
9+
collectSignatures().each(function(ix) {
10+
var $h2 = $(this).find("h2");
11+
var id = "sig_" + $h2.text().replace(/\s/g, "").replace(/[^\w]/g, "_");
12+
13+
$(this).attr("id", "id");
14+
sections.push({ id: id, text: $h2.text() });
15+
});
16+
17+
collectHeadings().each(function(ix) {
18+
var $el = $(this);
19+
var id = "section_" + $el.text().replace(/\s/g, "").replace(/[^\w]/g, "_");
20+
21+
$el.attr("id", id).prepend(anchorTemplate({ id: id }));
22+
sections.push({ id: id, text: $el.text() });
23+
});
24+
25+
$element.html(contentListTemplate({ sections: sections }));
26+
27+
if (window.location.hash.length) {
28+
var id = window.location.hash.replace("#", "");
29+
var anchor = document.getElementById(id);
30+
31+
if (anchor) anchor.scrollIntoView(true);
32+
}
33+
});
34+
35+
function collectSignatures() {
36+
var cloned = $(".content .signature").clone();
37+
// remove release numbers
38+
cloned.find(".release").remove();
39+
return cloned;
40+
}
41+
42+
function collectHeadings() {
43+
return $(".content .comment h2, .content .comment h3");
44+
}
45+
46+
function anchorTemplate(ctx) {
47+
var id = encodeURIComponent(ctx.id);
48+
49+
return (
50+
'<a class="anchor" href="#' + id + '" aria-hidden="true">' +
51+
'<svg aria-hidden="true" class="octicon octicon-link" height="16" version="1.1" viewBox="0 0 16 16" width="16">' +
52+
'<path d="M4 9h1v1h-1c-1.5 0-3-1.69-3-3.5s1.55-3.5 3-3.5h4c1.45 0 3 1.69 3 3.5 0 1.41-0.91 2.72-2 3.25v-1.16c0.58-0.45 1-1.27 1-2.09 0-1.28-1.02-2.5-2-2.5H4c-0.98 0-2 1.22-2 2.5s1 2.5 2 2.5z m9-3h-1v1h1c1 0 2 1.22 2 2.5s-1.02 2.5-2 2.5H9c-0.98 0-2-1.22-2-2.5 0-0.83 0.42-1.64 1-2.09v-1.16c-1.09 0.53-2 1.84-2 3.25 0 1.81 1.55 3.5 3 3.5h4c1.45 0 3-1.69 3-3.5s-1.5-3.5-3-3.5z"></path>' +
53+
'</svg>' +
54+
'</a>'
55+
);
56+
}
57+
58+
function contentListTemplate(ctx) {
59+
var sections = ctx.sections || [];
60+
61+
var items = sections.map(function(section) {
62+
var text = section.text;
63+
var id = encodeURIComponent(section.id);
64+
65+
return '<li><a href="#' + id + '">' + text + '</a></li>';
66+
});
67+
68+
return '<ul>' + items.join('') + '</li>';
69+
}

0 commit comments

Comments
 (0)