Skip to content

Commit 1025f2b

Browse files
committed
Also apply anchors to headers. Replace anchorify with non-minified patched version to deal with nested nodes
1 parent 0b954a0 commit 1025f2b

File tree

3 files changed

+95
-7
lines changed

3 files changed

+95
-7
lines changed

anchorify.min.js

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,91 @@
22
* William DURAND <william.durand1@gmail.com>
33
* MIT Licensed
44
*/
5-
(function(a,c){var b=(function(){var d=/[ ;,.'?!_]/g;function f(i){return i.trim().replace(d,"-").replace(/[-]+/g,"-").replace(/-$/,"").toLowerCase()}function g(i){var k=1,j=i;while(a.getElementById(i)){i=j+"-"+k++}return i}function e(k){var l;for(var j=0;j<k.childNodes.length;j++){l=k.childNodes[j];if(l.nodeType===Node.TEXT_NODE){return l.nodeValue}}}return function h(m,l){var n=l.text||"&para;",k=l.cssClass||"anchor-link",o=l.skipExisting;var r,q,p;for(var j=0;j<m.length;j++){r=m[j];if(r.id&&o){continue}r.id=r.id||g(f(e(r)));p=a.createElement("a");p.className=k;p.href="#"+r.id;p.innerHTML=n;if(l.position=="prepend"){r.insertBefore(p,r.firstChild)}else{r.appendChild(p)}}}})();if(typeof c!=="undefined"){c.fn.anchorify=function(d){b(c(this).get(),d||{});return this}}else{window.anchorify=function(e){e=e||{};var d=a.querySelectorAll(e.sel||"h1, h2, h3, h4, h5");return b(d,e)}}})(document,jQuery);
5+
(function (document, $) {
6+
"use strict";
7+
8+
var anchorify = (function() {
9+
var _specialCharsRegex = /[ ;,.'?!_]/g;
10+
11+
function generateId(text) {
12+
return text
13+
.trim()
14+
.replace(_specialCharsRegex, '-')
15+
.replace(/[-]+/g, '-')
16+
.replace(/-$/, '')
17+
.toLowerCase();
18+
}
19+
20+
function uniqId(id) {
21+
var inc = 1,
22+
originalId = id;
23+
24+
while (document.getElementById(id)) {
25+
id = originalId + '-' + inc++;
26+
}
27+
28+
return id;
29+
}
30+
31+
function getText( elems ) {
32+
var ret = "", elem;
33+
34+
for ( var i = 0; elems[i]; i++ ) {
35+
elem = elems[i];
36+
37+
// Get the text from text nodes and CDATA nodes
38+
if ( elem.nodeType === 3 || elem.nodeType === 4 ) {
39+
ret += elem.nodeValue;
40+
41+
// Traverse everything else, except comment nodes
42+
} else if ( elem.nodeType !== 8 ) {
43+
ret += getText( elem.childNodes );
44+
}
45+
}
46+
47+
return ret;
48+
}
49+
50+
return function anchorify(els, options) {
51+
var text = options.text || '&para;',
52+
cssClass = options.cssClass || 'anchor-link',
53+
skipExisting = options.skipExisting;
54+
55+
var el, id, anchor;
56+
57+
for (var i = 0; i < els.length; i++) {
58+
el = els[i];
59+
if (el.id && skipExisting) {
60+
continue;
61+
}
62+
el.id = el.id || uniqId(generateId(getText([el])));
63+
64+
anchor = document.createElement('a');
65+
anchor.className = cssClass;
66+
anchor.href = '#' + el.id;
67+
anchor.innerHTML = text;
68+
69+
if (options.position == 'prepend') {
70+
el.insertBefore(anchor, el.firstChild);
71+
} else {
72+
el.appendChild(anchor);
73+
}
74+
}
75+
};
76+
})();
77+
78+
if (typeof $ !== 'undefined') {
79+
$.fn.anchorify = function (options) {
80+
anchorify($(this).get(), options || {});
81+
82+
return this;
83+
};
84+
} else {
85+
window.anchorify = function(options) {
86+
options = options || {};
87+
var els = document.querySelectorAll(options.sel || 'h1, h2, h3, h4, h5');
88+
89+
return anchorify(els, options);
90+
};
91+
}
92+
})(document, jQuery);

api-demo.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
(function() {
2-
// silly selector to match only the options for the validate() method
3-
$(".signature ul ul div > strong").anchorify({
4-
position: "prepend"
5-
});
62

73
$( ".entry-example" ).each(function() {
84
var iframeSrc,
@@ -48,4 +44,9 @@
4844
doc.write( iframeSrc );
4945
doc.close();
5046
});
47+
48+
// silly selector to match only the options for the validate() method
49+
$("#content").find(".signature ul ul div > strong, h1, h2, h3, h4, h5").anchorify({
50+
position: "prepend"
51+
});
5152
}());

style.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ Description: jQuery Validation Plugin Theme based on twentytwelve
1010
@import url("../twentytwelve/style.css");
1111

1212
/* show anchor on hover, adjust offset */
13-
strong[id] .anchor-link {
13+
* > .anchor-link {
1414
margin-left: -9px;
1515
visibility: hidden;
1616
}
17-
strong[id]:hover .anchor-link {
17+
*:hover > .anchor-link {
1818
visibility: inherit;
1919
}
2020

0 commit comments

Comments
 (0)