Skip to content

Commit 7f1f75b

Browse files
committed
Merge pull request #395 from github/anchor
Add anchor.js
2 parents 7e6f3cd + fa21e7d commit 7f1f75b

File tree

10 files changed

+342
-2
lines changed

10 files changed

+342
-2
lines changed

_includes/footer.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
</footer>
2121

2222
<script src="/assets/js/bootstrap.min.js"></script>
23+
<script src="/assets/js/anchor.min.js"></script>
24+
<script>anchors.add('.org-table h4');</script>
2325
</body>
2426
</html>
2527
<!-- Proudly powered by GitHub Pages ~ Generated {{ site.time }} -->

_includes/org-table.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ <h6 class="no-matches" style="display: none;">No matches.</h6>
99
<tr class="table-header"><th>Avatar</th><th>Account</th><th>Affiliation</th></tr>
1010
{% for type_hash in include.orgs %}
1111
<tr class="type-block" id="{{ type_hash[0] | downcase | replace: ' ','_' }}">
12-
<td><h4>{{ type_hash[0] }} ({{ type_hash[1] | size }})</h4></td><td></td><td></td>
12+
{% assign slug = type_hash[0] | downcase | replace: ' ','-' | replace: '.','' %}
13+
<td><h4 id="{{ slug}}">{{ type_hash[0] }} ({{ type_hash[1] | size }})</h4></td><td></td><td></td>
1314
</tr>
1415
{% for org in type_hash[1] %}
1516
<tr>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "anchor-js",
3+
"version": "1.2.1",
4+
"authors": [
5+
"Bryan Braun"
6+
],
7+
"description": "A Javascript utility for adding deep anchor links to online docs.",
8+
"main": "anchor.js",
9+
"license": "MIT",
10+
"homepage": "https://github.com/bryanbraun/anchorjs",
11+
"ignore": [
12+
"**/.*",
13+
"node_modules",
14+
"bower_components",
15+
"test",
16+
"tests"
17+
],
18+
"_release": "1.2.1",
19+
"_resolution": {
20+
"type": "version",
21+
"tag": "1.2.1",
22+
"commit": "823c085d0057c3e2f9f50d379bf256a1c893b6a4"
23+
},
24+
"_source": "git://github.com/bryanbraun/anchorjs.git",
25+
"_target": "~1.2.1",
26+
"_originalSource": "anchor-js",
27+
"_direct": true
28+
}

_includes/vendor/anchor-js/README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# AnchorJS [![Build Status](https://img.shields.io/travis/bryanbraun/anchorjs/master.svg?style=flat)](https://travis-ci.org/bryanbraun/anchorjs)
2+
3+
A JavaScript utility for adding deep anchor links ([like these](http://ux.stackexchange.com/questions/36304/use-of-mouse-over-paragraph-marker-in-headlines-for-permalink)) to existing page content. AnchorJS is lightweight, accessible, and has no dependencies.
4+
5+
**[See Live Examples in the Documentation](http://bryanbraun.github.io/anchorjs#examples).**
6+
7+
![Anchoring links](http://bryanbraun.com/sites/default/files/anchoring-links_0.png)
8+
9+
## Installation
10+
11+
Download AnchorJS using npm,
12+
13+
```bash
14+
npm install anchor-js
15+
```
16+
17+
or bower:
18+
19+
```bash
20+
bower install anchor-js --save-dev
21+
```
22+
23+
(or just [download it from github](https://github.com/bryanbraun/anchorjs/releases)).
24+
25+
Then include the anchor.js file (or anchor.min.js) in your webpage.
26+
27+
```html
28+
<script src="anchor.js"></script>
29+
```
30+
31+
You could also include it via a CDN like [CDNJS](https://cdnjs.com/libraries/anchor-js).
32+
33+
##Usage
34+
See **[the Documentation](http://bryanbraun.github.io/anchorjs#basic-usage)** for detailed instructions.
35+
36+
## Compatibility
37+
Currently Supports: IE9+ and modern browsers
38+
39+
## Contributing [![devDependency Status](https://img.shields.io/david/dev/bryanbraun/anchorjs.svg?style=flat)](https://david-dm.org/bryanbraun/anchorjs#info=devDependencies)
40+
To contribute:
41+
42+
1. Fork/Clone the repo.
43+
2. Make your changes.
44+
3. Write tests as needed.
45+
4. Run tests locally to confirm everything is working:
46+
- Install phantomjs: `brew install phantomjs`
47+
- Install test modules: Run `npm install`
48+
- Run all tests: `npm test`
49+
5. Minify the code: `npm run release`
50+
6. Submit a Pull Request.
51+
52+
## License
53+
Licensed with the [MIT License](http://opensource.org/licenses/MIT).

_includes/vendor/anchor-js/anchor.js

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
/*!
2+
* AnchorJS - v1.2.1 - 2015-07-02
3+
* https://github.com/bryanbraun/anchorjs
4+
* Copyright (c) 2015 Bryan Braun; Licensed MIT
5+
*/
6+
7+
function AnchorJS(options) {
8+
'use strict';
9+
10+
this.options = options || {};
11+
12+
this._applyRemainingDefaultOptions = function(opts) {
13+
this.options.icon = this.options.hasOwnProperty('icon') ? opts.icon : '\ue9cb'; // Accepts characters (and also URLs?), like '#', '¶', '❡', or '§'.
14+
this.options.visible = this.options.hasOwnProperty('visible') ? opts.visible : 'hover'; // Also accepts 'always'
15+
this.options.placement = this.options.hasOwnProperty('placement') ? opts.placement : 'right'; // Also accepts 'left'
16+
this.options.class = this.options.hasOwnProperty('class') ? opts.class : ''; // Accepts any class name.
17+
};
18+
19+
this._applyRemainingDefaultOptions(options);
20+
21+
this.add = function(selector) {
22+
var elements,
23+
elsWithIds,
24+
idList,
25+
elementID,
26+
i,
27+
roughText,
28+
tidyText,
29+
index,
30+
count,
31+
newTidyText,
32+
readableID,
33+
anchor;
34+
35+
this._applyRemainingDefaultOptions(this.options);
36+
37+
// Provide a sensible default selector, if none is given.
38+
if (!selector) {
39+
selector = 'h1, h2, h3, h4, h5, h6';
40+
} else if (typeof selector !== 'string') {
41+
throw new Error('The selector provided to AnchorJS was invalid.');
42+
}
43+
44+
elements = document.querySelectorAll(selector);
45+
if (elements.length === 0) {
46+
return false;
47+
}
48+
49+
this._addBaselineStyles();
50+
51+
// We produce a list of existing IDs so we don't generate a duplicate.
52+
elsWithIds = document.querySelectorAll('[id]');
53+
idList = [].map.call(elsWithIds, function assign(el) {
54+
return el.id;
55+
});
56+
57+
for (i = 0; i < elements.length; i++) {
58+
59+
if (elements[i].hasAttribute('id')) {
60+
elementID = elements[i].getAttribute('id');
61+
} else {
62+
roughText = elements[i].textContent;
63+
64+
// Refine it so it makes a good ID. Strip out non-safe characters, replace
65+
// spaces with hyphens, truncate to 32 characters, and make toLowerCase.
66+
//
67+
// Example string: // '⚡⚡⚡ Unicode icons are cool--but they definitely don't belong in a URL fragment.'
68+
tidyText = roughText.replace(/[^\w\s-]/gi, '') // ' Unicode icons are cool--but they definitely dont belong in a URL fragment'
69+
.replace(/\s+/g, '-') // '-Unicode-icons-are-cool--but-they-definitely-dont-belong-in-a-URL-fragment'
70+
.replace(/-{2,}/g, '-') // '-Unicode-icons-are-cool-but-they-definitely-dont-belong-in-a-URL-fragment'
71+
.substring(0, 64) // '-Unicode-icons-are-cool-but-they-definitely-dont-belong-in-a-URL'
72+
.replace(/^-+|-+$/gm, '') // 'Unicode-icons-are-cool-but-they-definitely-dont-belong-in-a-URL'
73+
.toLowerCase(); // 'unicode-icons-are-cool-but-they-definitely-dont-belong-in-a-url'
74+
75+
// Compare our generated ID to existing IDs (and increment it if needed)
76+
// before we add it to the page.
77+
newTidyText = tidyText;
78+
count = 0;
79+
do {
80+
if (index !== undefined) {
81+
newTidyText = tidyText + '-' + count;
82+
}
83+
// .indexOf is supported in IE9+.
84+
index = idList.indexOf(newTidyText);
85+
count += 1;
86+
} while (index !== -1);
87+
index = undefined;
88+
idList.push(newTidyText);
89+
90+
// Assign it to our element.
91+
// Currently the setAttribute element is only supported in IE9 and above.
92+
elements[i].setAttribute('id', newTidyText);
93+
94+
elementID = newTidyText;
95+
}
96+
97+
readableID = elementID.replace(/-/g, ' ');
98+
99+
// The following code builds the following DOM structure in a more effiecient (albeit opaque) way.
100+
// '<a class="anchorjs-link ' + this.options.class + '" href="#' + elementID + '" aria-label="Anchor link for: ' + readableID + '" data-anchorjs-icon="' + this.options.icon + '"></a>';
101+
anchor = document.createElement('a');
102+
anchor.className = 'anchorjs-link ' + this.options.class;
103+
anchor.href = '#' + elementID;
104+
anchor.setAttribute('aria-label', 'Anchor link for: ' + readableID);
105+
anchor.setAttribute('data-anchorjs-icon', this.options.icon);
106+
107+
if (this.options.visible === 'always') {
108+
anchor.style.opacity = '1';
109+
}
110+
111+
if (this.options.icon === '\ue9cb') {
112+
anchor.style.fontFamily = 'anchorjs-icons';
113+
anchor.style.fontStyle = 'normal';
114+
anchor.style.fontVariant = 'normal';
115+
anchor.style.fontWeight = 'normal';
116+
anchor.style.lineHeight = 1;
117+
}
118+
119+
if (this.options.placement === 'left') {
120+
anchor.style.position = 'absolute';
121+
anchor.style.marginLeft = '-1em';
122+
anchor.style.paddingRight = '0.5em';
123+
elements[i].insertBefore(anchor, elements[i].firstChild);
124+
} else { // if the option provided is `right` (or anything else).
125+
anchor.style.paddingLeft = '0.375em';
126+
elements[i].appendChild(anchor);
127+
}
128+
}
129+
130+
return this;
131+
};
132+
133+
this.remove = function(selector) {
134+
var domAnchor,
135+
elements = document.querySelectorAll(selector);
136+
for (var i = 0; i < elements.length; i++) {
137+
domAnchor = elements[i].querySelector('.anchorjs-link');
138+
if (domAnchor) {
139+
elements[i].removeChild(domAnchor);
140+
}
141+
}
142+
return this;
143+
};
144+
145+
this._addBaselineStyles = function() {
146+
// We don't want to add global baseline styles if they've been added before.
147+
if (document.head.querySelector('style.anchorjs') !== null) {
148+
return;
149+
}
150+
151+
var style = document.createElement('style'),
152+
linkRule =
153+
' .anchorjs-link {' +
154+
' opacity: 0;' +
155+
' text-decoration: none;' +
156+
' -webkit-font-smoothing: antialiased;' +
157+
' -moz-osx-font-smoothing: grayscale;' +
158+
' }',
159+
hoverRule =
160+
' *:hover > .anchorjs-link,' +
161+
' .anchorjs-link:focus {' +
162+
' opacity: 1;' +
163+
' }',
164+
anchorjsLinkFontFace =
165+
' @font-face {' +
166+
' font-family: "anchorjs-icons";' +
167+
' font-style: normal;' +
168+
' font-weight: normal;' + // Icon from icomoon; 10px wide & 10px tall; 2 empty below & 4 above
169+
' src: url(data:application/x-font-ttf;charset=utf-8;base64,AAEAAAALAIAAAwAwT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AAAAAAAAAAAAAAAAAADgCuAAEAAAAAAAEADgAAAAEAAAAAAAIABwCfAAEAAAAAAAMADgBLAAEAAAAAAAQADgC0AAEAAAAAAAUACwAqAAEAAAAAAAYADgB1AAEAAAAAAAoAGgDeAAMAAQQJAAEAHAAOAAMAAQQJAAIADgCmAAMAAQQJAAMAHABZAAMAAQQJAAQAHADCAAMAAQQJAAUAFgA1AAMAAQQJAAYAHACDAAMAAQQJAAoANAD4YW5jaG9yanMtaWNvbnMAYQBuAGMAaABvAHIAagBzAC0AaQBjAG8AbgBzVmVyc2lvbiAxLjAAVgBlAHIAcwBpAG8AbgAgADEALgAwYW5jaG9yanMtaWNvbnMAYQBuAGMAaABvAHIAagBzAC0AaQBjAG8AbgBzYW5jaG9yanMtaWNvbnMAYQBuAGMAaABvAHIAagBzAC0AaQBjAG8AbgBzUmVndWxhcgBSAGUAZwB1AGwAYQByYW5jaG9yanMtaWNvbnMAYQBuAGMAaABvAHIAagBzAC0AaQBjAG8AbgBzRm9udCBnZW5lcmF0ZWQgYnkgSWNvTW9vbi4ARgBvAG4AdAAgAGcAZQBuAGUAcgBhAHQAZQBkACAAYgB5ACAASQBjAG8ATQBvAG8AbgAuAAAAAwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==) format("truetype");' +
170+
' }',
171+
pseudoElContent =
172+
' [data-anchorjs-icon]::after {' +
173+
' content: attr(data-anchorjs-icon);' +
174+
' }',
175+
firstStyleEl;
176+
177+
style.className = 'anchorjs';
178+
style.appendChild(document.createTextNode('')); // Necessary for Webkit.
179+
180+
// We place it in the head with the other style tags, if possible, so as to
181+
// not look out of place. We insert before the others so these styles can be
182+
// overridden if necessary.
183+
firstStyleEl = document.head.querySelector('[rel="stylesheet"], style');
184+
if (firstStyleEl === undefined) {
185+
document.head.appendChild(style);
186+
} else {
187+
document.head.insertBefore(style, firstStyleEl);
188+
}
189+
190+
style.sheet.insertRule(linkRule, style.sheet.cssRules.length);
191+
style.sheet.insertRule(hoverRule, style.sheet.cssRules.length);
192+
style.sheet.insertRule(pseudoElContent, style.sheet.cssRules.length);
193+
style.sheet.insertRule(anchorjsLinkFontFace, style.sheet.cssRules.length);
194+
};
195+
}
196+
197+
var anchors = new AnchorJS();

_includes/vendor/anchor-js/anchor.min.js

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

_includes/vendor/anchor-js/bower.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "anchor-js",
3+
"version": "1.1.1",
4+
"authors": [
5+
"Bryan Braun"
6+
],
7+
"description": "A Javascript utility for adding deep anchor links to online docs.",
8+
"main": "anchor.js",
9+
"license": "MIT",
10+
"homepage": "https://github.com/bryanbraun/anchorjs",
11+
"ignore": [
12+
"**/.*",
13+
"node_modules",
14+
"bower_components",
15+
"test",
16+
"tests"
17+
]
18+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "anchor-js",
3+
"version": "1.2.1",
4+
"author": "bryanbraun (https://github.com/bryanbraun)",
5+
"description": "A Javscript utility for adding deep anchor links to online docs.",
6+
"homepage": "https://github.com/bryanbraun/anchorjs",
7+
"repository": {
8+
"type": "git",
9+
"url": "https://github.com/bryanbraun/anchorjs.git"
10+
},
11+
"bugs": {
12+
"url": "https://github.com/bryanbraun/anchorjs/issues"
13+
},
14+
"license": "MIT",
15+
"scripts": {
16+
"lint": "eslint anchor.js",
17+
"jasmine": "karma start test/config.js --single-run",
18+
"release": "uglifyjs anchor.js --compress --mangle --screw-ie8 --comments \"/Copyright/\" -o anchor.min.js",
19+
"test": "npm run lint && npm run jasmine"
20+
},
21+
"devDependencies": {
22+
"eslint": "~0.24.0",
23+
"jasmine-core": "~2.3.4",
24+
"karma": "~0.12.37",
25+
"karma-cli": "~0.1.0",
26+
"karma-jasmine": "~0.3.6",
27+
"karma-phantomjs-launcher": "~0.2.0",
28+
"uglify-js": "~2.4.23"
29+
}
30+
}

assets/js/anchor.min.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
---
3+
4+
{% include vendor/anchor-js/anchor.min.js %}

bower.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"bootstrap": "~2.3.2",
1313
"jquery": "~1.11.0",
1414
"html5shiv": "~3.7.0",
15-
"respond": "~1.4.2"
15+
"respond": "~1.4.2",
16+
"anchor-js": "~1.2.1"
1617
}
1718
}

0 commit comments

Comments
 (0)