Skip to content

Commit 9f58a65

Browse files
committed
Change how hash urls work so they're bookmarkable and linkable
1 parent 6b95211 commit 9f58a65

File tree

4 files changed

+46
-9
lines changed

4 files changed

+46
-9
lines changed

_site/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
22
<html>
33
<head>
4-
<title>CanJS**</title>
4+
<title>CanJS</title>
55
<link rel="stylesheet" type="text/css" href="stylesheets/styles.css">
66
<link rel="stylesheet" type="text/css" href="stylesheets/tango.css">
77

_site/javascripts/application.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
var currentH2slug = "";
2+
13
$.Controller('Menu', {}, {
24
init : function(){
35
this.find('#menu').append(this.buildMenu());
@@ -24,8 +26,14 @@ $.Controller('Menu', {}, {
2426
for(var i = 0; i < nodes.length; i++){
2527
var h = $(nodes[i].node), text = $('<div>' + h.html() + '</div>');
2628
text.find('code').remove();
29+
var slug = $.trim(text.text()).toLowerCase().replace(/[^A-Za-z0-9_]/g, '_');
30+
if(nodes[i].level === 2){
31+
currentH2slug = slug
32+
} else if(currentH2slug != "" && nodes[i].level === 3){
33+
slug = currentH2slug + "-" + slug;
34+
}
2735
html.push('<li>');
28-
html.push('<a href="#heading-' + nodes[i].id + '" data-level="'+ nodes[i].level +'">' + text.text() + '</a>');
36+
html.push('<a href="#' + slug + '" class="heading-'+nodes[i].id+'" data-level="'+ nodes[i].level +'">' + text.text() + '</a>');
2937
html.push(build(nodes[i].id));
3038
html.push('</li>')
3139
}
@@ -37,9 +45,17 @@ $.Controller('Menu', {}, {
3745
var level = parseInt(this.headings[i].nodeName.match(/\d/), 10),
3846
parentLevel = level - 1,
3947
id = function(){ return i+1; }(),
40-
node = { node: this.headings[i], children: [], level: level, id: id };
48+
node = { node: this.headings[i], children: [], level: level, id: id },
49+
text = $('<div>' + $(this.headings[i]).html() + '</div>');
50+
text.find('code').remove();
4151
this.headingOffsets.push($(this.headings[i]).offset().top)
42-
$(this.headings[i]).attr('id', 'heading-' + id);
52+
var slug = $.trim(text.text()).toLowerCase().replace(/[^A-Za-z0-9_]/g, '_');
53+
if(level === 2){
54+
currentH2slug = slug;
55+
} else if(level === 3){
56+
slug = currentH2slug + "-" + slug;
57+
}
58+
$(this.headings[i]).attr('id', slug);
4359
byId[id] = node;
4460
lastByLevel[level] = node;
4561
byLevel[level].push(node);
@@ -65,7 +81,7 @@ $.Controller('Menu', {}, {
6581
var offset = this.headingOffsets[i];
6682
if(offset - windowHeight / 2 < scroll){
6783
var active = this.find('#menu a.active'),
68-
current = this.find('#menu a[href="#heading-' + (i + 1) + '"]');
84+
current = this.find('#menu a.heading-' + (i + 1));
6985
this.markActive(active, current);
7086
return;
7187
}

_site/stylesheets/styles.less

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,4 +285,9 @@ body {
285285
b,strong {
286286
font-weight: bold;
287287
}
288+
}
289+
body {
290+
-moz-tab-size: 1;
291+
-o-tab-size: 1;
292+
tab-size: 1;
288293
}

javascripts/application.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
var currentH2slug = "";
2+
13
$.Controller('Menu', {}, {
24
init : function(){
35
this.find('#menu').append(this.buildMenu());
@@ -24,8 +26,14 @@ $.Controller('Menu', {}, {
2426
for(var i = 0; i < nodes.length; i++){
2527
var h = $(nodes[i].node), text = $('<div>' + h.html() + '</div>');
2628
text.find('code').remove();
29+
var slug = $.trim(text.text()).toLowerCase().replace(/[^A-Za-z0-9_]/g, '_');
30+
if(nodes[i].level === 2){
31+
currentH2slug = slug
32+
} else if(currentH2slug != "" && nodes[i].level === 3){
33+
slug = currentH2slug + "-" + slug;
34+
}
2735
html.push('<li>');
28-
html.push('<a href="#heading-' + nodes[i].id + '" data-level="'+ nodes[i].level +'">' + text.text() + '</a>');
36+
html.push('<a href="#' + slug + '" class="heading-'+nodes[i].id+'" data-level="'+ nodes[i].level +'">' + text.text() + '</a>');
2937
html.push(build(nodes[i].id));
3038
html.push('</li>')
3139
}
@@ -37,9 +45,17 @@ $.Controller('Menu', {}, {
3745
var level = parseInt(this.headings[i].nodeName.match(/\d/), 10),
3846
parentLevel = level - 1,
3947
id = function(){ return i+1; }(),
40-
node = { node: this.headings[i], children: [], level: level, id: id };
48+
node = { node: this.headings[i], children: [], level: level, id: id },
49+
text = $('<div>' + $(this.headings[i]).html() + '</div>');
50+
text.find('code').remove();
4151
this.headingOffsets.push($(this.headings[i]).offset().top)
42-
$(this.headings[i]).attr('id', 'heading-' + id);
52+
var slug = $.trim(text.text()).toLowerCase().replace(/[^A-Za-z0-9_]/g, '_');
53+
if(level === 2){
54+
currentH2slug = slug;
55+
} else if(level === 3){
56+
slug = currentH2slug + "-" + slug;
57+
}
58+
$(this.headings[i]).attr('id', slug);
4359
byId[id] = node;
4460
lastByLevel[level] = node;
4561
byLevel[level].push(node);
@@ -65,7 +81,7 @@ $.Controller('Menu', {}, {
6581
var offset = this.headingOffsets[i];
6682
if(offset - windowHeight / 2 < scroll){
6783
var active = this.find('#menu a.active'),
68-
current = this.find('#menu a[href="#heading-' + (i + 1) + '"]');
84+
current = this.find('#menu a.heading-' + (i + 1));
6985
this.markActive(active, current);
7086
return;
7187
}

0 commit comments

Comments
 (0)