This repository was archived by the owner on Mar 22, 2019. It is now read-only.
forked from webpack/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbindToIntraLinks.js
139 lines (122 loc) · 3.66 KB
/
bindToIntraLinks.js
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
var linkToTitle = require("../lib/linkToTitle");
var downloadWiki = require("./downloadWiki");
var LRU = require("lru-cache");
var INTRA_LINK = /^([a-z0-9\-\.]+)\.html$/i;
function hasModifier(event) {
return event.metaKey || event.shiftKey || event.altKey || event.ctrlKey;
}
function bindIntraLinks() {
document.body.addEventListener("click", function(event) {
if(event.target.tagName === "A" && !hasModifier(event) && (event.which === 1) && INTRA_LINK.test(event.target.getAttribute("href"))) {
var href = event.target.getAttribute("href");
INTRA_LINK.lastIndex = 0;
var wiki = INTRA_LINK.exec(href)[1];
document.title = linkToTitle(wiki);
currentPage = wiki;
history.pushState(null, null, href);
loadPage(wiki, false);
event.preventDefault();
}
}, false);
}
function highlightIntraLinks() {
if(document.querySelectorAll) {
var elements = document.querySelectorAll('a[href$=".html"]');
for(var i = 0; i < elements.length; i++) {
var element = elements[i];
var href = element.getAttribute("href");
if(element.classList && INTRA_LINK.test(href)) {
INTRA_LINK.lastIndex = 0;
var wiki = INTRA_LINK.exec(href)[1];
if((currentPage === wiki) ^ (element.classList.contains("active"))) {
element.classList.toggle("active");
}
}
}
}
}
var contentElement = document.getElementById("wiki");
var titleElement = document.getElementById("wikititle");
var editElements = document.getElementsByClassName("wikieditlink");
var pagesCache = LRU({
max: 10,
maxAge: 5 * 60 * 1000 // 5m
});
var currentPage = "";
window.addEventListener("popstate", function() {
loadCurrentPage(false);
});
highlightIntraLinks();
loadCurrentPage(true);
function loadCurrentPage(initial) {
var match = /\/([a-z0-9\-\.]+)\.html$/i.exec(location.pathname);
if(match) {
var wiki = match[1];
if(wiki !== currentPage || initial) {
currentPage = wiki;
loadPage(wiki, initial);
}
}
}
function reportAnalytics() {
var ga = require("./googleAnalytics");
var location = window.location.protocol +
'//' + window.location.hostname +
window.location.pathname +
window.location.search;
ga('set', 'location', location);
ga('set', 'title', document.title);
ga('send', 'pageview');
}
function updateDisqus() {
var disqus = require("./disqus");
disqus.update(currentPage);
}
var EDIT_LINK = "https://github.com/webpack/docs/wiki/XXX/_edit";
function setPage(wiki, content) {
Array.prototype.forEach.call(editElements, function(editElement) {
editElement.setAttribute("href", EDIT_LINK.replace(/XXX/g, wiki));
});
titleElement.innerHTML = linkToTitle(wiki);
contentElement.innerHTML = content;
}
function loadPage(wiki, initial) {
var cacheEntry = pagesCache.get(wiki);
if(cacheEntry) {
setPage(wiki, cacheEntry);
if(!initial) {
if(!window.location.hash) window.scrollTo(0, 0);
reportAnalytics();
updateDisqus();
}
highlightIntraLinks();
return;
}
if(!initial && document.body.classList) document.body.classList.add("loading");
downloadWiki(wiki, function(err, result) {
if(err) {
if(initial) {
if(document.body.classList) document.body.classList.remove("loading");
return;
}
window.location.reload();
} else {
require(["../lib/renderMarkdown"], function(renderMarkdown) {
next(renderMarkdown(result));
});
}
function next(result) {
pagesCache.set(wiki, result);
if(document.body.classList) document.body.classList.remove("loading");
setPage(wiki, result);
if(!initial) {
if(!window.location.hash) window.scrollTo(0, 0);
reportAnalytics();
updateDisqus();
}
if(initial) bindIntraLinks();
highlightIntraLinks();
}
});
require(["../lib/renderMarkdown"]); // Start loading in parallel
}