Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit 3d73e2d

Browse files
devversionThomasBurleson
authored andcommitted
docs(): title elements will be auto anchored. (#9681)
* Very often I want to link a user to a specific topic in the docs, but it is just not possible, and they needed to search for the topic I meant manually * Having a directive, which automatically creates anchors for all title elements does make this SUPER easy, and we can just send the user a easy (accessible) link. > Note that the GitHub Readme works kind of the same.
1 parent 01a7a78 commit 3d73e2d

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

docs/app/css/style.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ body.docs-body {
2929
height: auto;
3030
}
3131

32+
.docs-anchor {
33+
color: inherit;
34+
}
35+
3236
#license-footer {
3337
align-self: flex-end;
3438
padding: 16px 32px;

docs/app/js/anchor.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
(function() {
2+
angular
3+
.module('docsApp')
4+
.directive('h4', MdAnchorDirective)
5+
.directive('h3', MdAnchorDirective)
6+
.directive('h2', MdAnchorDirective)
7+
.directive('h1', MdAnchorDirective);
8+
9+
function MdAnchorDirective($mdUtil) {
10+
11+
/** @const @type {RegExp} */
12+
var unsafeCharRegex = /[&\s+$,:;=?@"#{}|^~[`%!'\].\/()*\\]/g;
13+
14+
return {
15+
restrict: 'E',
16+
scope: {},
17+
template: '<a class="docs-anchor" ng-href="#{{ name }}" name="{{ name }}" ng-transclude></a>',
18+
transclude: true,
19+
link: postLink
20+
};
21+
22+
function postLink(scope, element) {
23+
24+
// Delay the URL creation, because the inner text might be not interpolated yet.
25+
$mdUtil.nextTick(createContentURL);
26+
27+
/**
28+
* Creates URL from the text content of the element and writes it into the scope.
29+
*/
30+
function createContentURL() {
31+
scope.name = element.text()
32+
.trim() // Trim text due to browsers extra whitespace.
33+
.replace(/'/g, '') // Transform apostrophes words to a single one.
34+
.replace(unsafeCharRegex, '-') // Replace unsafe chars with a dash symbol.
35+
.replace(/-{2,}/g, '-') // Remove repeating dash symbols.
36+
.replace(/^-|-$/g, '') // Remove preceding or ending dashes.
37+
.toLowerCase(); // Link should be lower-case for accessible URL.
38+
}
39+
}
40+
}
41+
42+
// Manually specify $inject because Strict DI is enabled.
43+
MdAnchorDirective.$inject = ['$mdUtil'];
44+
45+
})();

docs/app/js/app.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ function(SERVICES, COMPONENTS, DEMOS, PAGES,
127127
});
128128

129129
$routeProvider.otherwise('/');
130+
131+
// Change hash prefix of the Angular router, because we use the hash symbol for anchor links.
132+
// The hash will be not used by the docs, because we use the HTML5 mode for our links.
133+
$locationProvider.hashPrefix('!');
134+
130135
}])
131136

132137
.config(['AngularyticsProvider', function(AngularyticsProvider) {

docs/gulpfile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ gulp.task('docs-js', ['docs-app', 'docs-html2js', 'demos', 'build', 'docs-js-dep
125125
return series(
126126
gulp.src([
127127
'node_modules/angularytics/dist/angularytics.js',
128+
'dist/docs/js/app.js', // Load the Angular module initialization at first.
128129
'dist/docs/js/**/*.js'
129130
])
130131
.pipe(concat('docs.js'))

0 commit comments

Comments
 (0)