-
Notifications
You must be signed in to change notification settings - Fork 668
Expand file tree
/
Copy pathmarkdown-service.js
More file actions
55 lines (47 loc) · 1.62 KB
/
markdown-service.js
File metadata and controls
55 lines (47 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
angular
.module('gcloud.markdown', [])
.config(function ($anchorScrollProvider) {
$anchorScrollProvider.disableAutoScrolling();
})
.factory('mdConverter', function ($location, $interpolate) {
var template = '<a gcloud-scroll="{{id}}" href="#{{href}}">{{text}}</a>';
var anchor = $interpolate(template);
return new showdown.Converter({
extensions: [function () {
return [{
type: 'lang',
// unescaped version \[([^\]]+)\]\(([\/|\#][^)]+)\)
regex: '\\[([^\\]]+)\\]\\(([\\/|\\#][^)]+)\\)',
replace: function (match, text, href) {
var isDeeplink = /^\#/.test(href);
var data = { text: text };
if (isDeeplink) {
data.id = href.replace('#', '');
data.href = $location.path() + '?section=' + data.id;
} else {
data.href = href.replace('readme.md', '');
}
return anchor(data);
}
}];
}]
});
})
.factory('mdDeeplink', function ($window, $location) {
// retrieve amount to offset the scroll by (height of the page header)
function offset () {
if (!offset._value) {
var el = document.getElementsByClassName('page-header')[0];
offset._value = el ? el.offsetHeight : 0;
}
return offset._value;
}
return function deeplink (section) {
if (!section) return;
// showdown doesn't insert hyphens like ghfm does
var id = section.split('-').join('');
var el = document.getElementById(id);
if (!el) return;
$window.scrollTo(0, el.offsetTop - offset());
};
});