-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
Copy pathindex.js
277 lines (235 loc) Β· 7.51 KB
/
index.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/* eslint-disable no-unused-vars */
import { getParentPath, stringifyQuery } from '../router/util.js';
import { noop, isExternal } from '../util/core.js';
import { get } from '../util/ajax.js';
/** @typedef {import('../Docsify.js').Constructor} Constructor */
/**
* @template {!Constructor} T
* @param {T} Base - The class to extend
*/
export function Fetch(Base) {
return class Fetch extends Base {
#loadNested(path, qs, file, next, vm, first) {
path = first ? path : path.replace(/\/$/, '');
path = getParentPath(path);
if (!path) {
return;
}
get(
vm.router.getFile(path + file) + qs,
false,
vm.config.requestHeaders,
).then(next, _error => this.#loadNested(path, qs, file, next, vm));
}
#last;
#abort = () => this.#last && this.#last.abort && this.#last.abort();
#request = (url, requestHeaders) => {
this.#abort();
this.#last = get(url, true, requestHeaders);
return this.#last;
};
#get404Path = (path, config) => {
const { notFoundPage, ext } = config;
const defaultPath = '_404' + (ext || '.md');
let key;
let path404;
switch (typeof notFoundPage) {
case 'boolean':
path404 = defaultPath;
break;
case 'string':
path404 = notFoundPage;
break;
case 'object':
key = Object.keys(notFoundPage)
.sort((a, b) => b.length - a.length)
.filter(k => path.match(new RegExp('^' + k)))[0];
path404 = (key && notFoundPage[key]) || defaultPath;
break;
default:
break;
}
return path404;
};
_loadSideAndNav(path, qs, loadSidebar, cb) {
return () => {
if (!loadSidebar) {
return cb();
}
const fn = result => {
this._renderSidebar(result);
cb();
};
// Load sidebar
this.#loadNested(path, qs, loadSidebar, fn, this, true);
};
}
_fetch(cb = noop) {
const { query } = this.route;
const { path } = this.route;
// Prevent loading remote content via URL hash
// Ex: https://foo.com/#//bar.com/file.md
if (isExternal(path)) {
history.replaceState(null, '', '#');
this.router.normalize();
} else {
const qs = stringifyQuery(query, ['id']);
const { loadNavbar, requestHeaders, loadSidebar } = this.config;
// Abort last request
const file = this.router.getFile(path);
this.isRemoteUrl = isExternal(file);
// Current page is html
this.isHTML = /\.html$/g.test(file);
// create a handler that should be called if content was fetched successfully
const contentFetched = (text, opt, response) => {
this.route.response = response;
this._renderMain(
text,
opt,
this._loadSideAndNav(path, qs, loadSidebar, cb),
);
};
// and a handler that is called if content failed to fetch
const contentFailedToFetch = (_error, response) => {
this.route.response = response;
this._fetchFallbackPage(path, qs, cb) || this._fetch404(file, qs, cb);
};
// attempt to fetch content from a virtual route, and fallback to fetching the actual file
if (!this.isRemoteUrl) {
this.matchVirtualRoute(path).then(contents => {
if (typeof contents === 'string') {
contentFetched(contents);
} else {
this.#request(file + qs, requestHeaders).then(
contentFetched,
contentFailedToFetch,
);
}
});
} else {
// if the requested url is not local, just fetch the file
this.#request(file + qs, requestHeaders).then(
contentFetched,
contentFailedToFetch,
);
}
// Load nav
loadNavbar &&
this.#loadNested(
path,
qs,
loadNavbar,
text => this._renderNav(text),
this,
true,
);
}
}
_fetchCover(cb = noop) {
const { coverpage, requestHeaders } = this.config;
const query = this.route.query;
const root = getParentPath(this.route.path);
if (coverpage) {
let path = null;
const routePath = this.route.path;
if (typeof coverpage === 'string') {
if (routePath === '/') {
path = coverpage;
}
} else if (Array.isArray(coverpage)) {
path = coverpage.indexOf(routePath) > -1 && '_coverpage';
} else {
const cover = coverpage[routePath];
path = cover === true ? '_coverpage' : cover;
}
const coverOnly = Boolean(path) && this.config.onlyCover;
const next = () => cb(coverOnly);
if (path) {
path = this.router.getFile(root + path);
this.coverIsHTML = /\.html$/g.test(path);
get(path + stringifyQuery(query, ['id']), false, requestHeaders).then(
text => this._renderCover(text, coverOnly, next),
(event, response) => {
this.coverIsHTML = false;
this._renderCover(
`# ${response.status} - ${response.statusText}`,
coverOnly,
next,
);
},
);
} else {
this._renderCover(null, coverOnly, next);
}
} else {
cb(false);
}
}
$fetch(cb = noop, onNavigate = this.onNavigate.bind(this)) {
const done = () => {
this.callHook('doneEach');
cb();
};
this._fetchCover(onlyCover => {
if (onlyCover) {
done();
} else {
this._fetch(() => {
onNavigate();
done();
});
}
});
}
_fetchFallbackPage(path, qs, cb = noop) {
const { requestHeaders, fallbackLanguages, loadSidebar } = this.config;
if (!fallbackLanguages) {
return false;
}
const local = path.split('/')[1];
if (fallbackLanguages.indexOf(local) === -1) {
return false;
}
const newPath = this.router.getFile(
path.replace(new RegExp(`^/${local}`), ''),
);
const req = this.#request(newPath + qs, requestHeaders);
req.then(
(text, opt) =>
this._renderMain(
text,
opt,
this._loadSideAndNav(path, qs, loadSidebar, cb),
),
_error => this._fetch404(path, qs, cb),
);
return true;
}
/**
* Load the 404 page
* @param {String} path URL to be loaded
* @param {*} qs TODO: define
* @param {Function} cb Callback
* @returns {Boolean} True if the requested page is not found
* @private
*/
_fetch404(path, qs, cb = noop) {
const { loadSidebar, requestHeaders, notFoundPage } = this.config;
const fnLoadSideAndNav = this._loadSideAndNav(path, qs, loadSidebar, cb);
if (notFoundPage) {
const path404 = this.#get404Path(path, this.config);
this.#request(this.router.getFile(path404), requestHeaders).then(
(text, opt) => this._renderMain(text, opt, fnLoadSideAndNav),
_error => this._renderMain(null, {}, fnLoadSideAndNav),
);
return true;
}
this._renderMain(null, {}, fnLoadSideAndNav);
return false;
}
initFetch() {
const { loadSidebar } = this.config;
this.$fetch(_ => this.callHook('ready'));
}
};
}