Skip to content

Commit 4558dea

Browse files
author
Manuel Mujica
authored
Merge pull request #4 from bit-docs/pretty-anchors
Implement current Steal website pretty anchors
2 parents 5f615ae + eebb855 commit 4558dea

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,8 @@
2727
"devDependencies": {
2828
"bit-docs-generate-html": "^0.3.3",
2929
"bit-docs-html-toc": "^0.2.1"
30+
},
31+
"dependencies": {
32+
"jquery": "^3.1.1"
3033
}
3134
}

static/docjs.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,33 @@
1+
var $ = require("jquery");
2+
13
require("./styles/styles.less");
4+
5+
$(function() {
6+
collectHeadings().each(function() {
7+
var $el = $(this);
8+
var id = $el.attr("id");
9+
10+
if (!id) {
11+
id = "section_" + $el.text().replace(/\s/g, "").replace(/[^\w]/g, "_");
12+
$el.attr("id", id);
13+
}
14+
15+
$el.prepend(anchorTemplate({ id: id }));
16+
});
17+
18+
function collectHeadings() {
19+
return $(".content .comment h2, .content .comment h3");
20+
}
21+
22+
function anchorTemplate(ctx) {
23+
var id = encodeURIComponent(ctx.id);
24+
25+
return (
26+
'<a class="anchor" href="#' + id + '" aria-hidden="true">' +
27+
'<svg aria-hidden="true" class="octicon octicon-link" height="16" version="1.1" viewBox="0 0 16 16" width="16">' +
28+
'<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>' +
29+
'</svg>' +
30+
'</a>'
31+
);
32+
}
33+
});

0 commit comments

Comments
 (0)