Skip to content

Commit eb2b62e

Browse files
author
Manuel Mujica
committed
Use github like ids for heading anchors
1 parent ff82d7d commit eb2b62e

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

make-example.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ generate(docMap, {
1818
"bit-docs-docjs-theme": __dirname,
1919
"bit-docs-prettify": "^0.1.0",
2020
"bit-docs-html-highlight-line": "^0.2.2",
21-
"bit-docs-html-toc": "^0.2.1"
21+
"bit-docs-html-toc": "^0.4.0"
2222
}
2323
},
2424
dest: path.join(__dirname, "temp"),

static/docjs.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,19 @@ var $ = require("jquery");
33
require("./styles/styles.less");
44

55
$(function() {
6+
var headings = {};
7+
68
collectHeadings().each(function() {
79
var $el = $(this);
810
var id = $el.attr("id");
911

1012
if (!id) {
11-
id = "section_" + $el.text().replace(/\s/g, "").replace(/[^\w]/g, "_");
13+
id = makeAnchorHeadingId($el.text());
14+
var count = headings[id] || 0;
15+
16+
id += (count > 0) ? "-" + count : "";
17+
headings[id] = count + 1;
18+
1219
$el.attr("id", id);
1320
}
1421

@@ -19,6 +26,13 @@ $(function() {
1926
return $(".content .comment h2, .content .comment h3");
2027
}
2128

29+
function makeAnchorHeadingId(anchorText) {
30+
return (anchorText || "")
31+
.replace(/\s/g, "-") // replace spaces with dashes
32+
.replace(/[^\w\-]/g, "") // remove punctuation
33+
.toLowerCase();
34+
}
35+
2236
function anchorTemplate(ctx) {
2337
var id = encodeURIComponent(ctx.id);
2438

0 commit comments

Comments
 (0)