-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
index.js
593 lines (490 loc) Β· 17.2 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
import tinydate from 'tinydate';
import * as dom from '../util/dom.js';
import { getPath, isAbsolutePath } from '../router/util.js';
import { isMobile } from '../util/env.js';
import { isPrimitive } from '../util/core.js';
import { Compiler } from './compiler.js';
import * as tpl from './tpl.js';
import { prerenderEmbed } from './embed.js';
/** @typedef {import('../Docsify.js').Constructor} Constructor */
/**
* @template {!Constructor} T
* @param {T} Base - The class to extend
*/
export function Render(Base) {
return class Render extends Base {
#vueGlobalData;
#addTextAsTitleAttribute(cssSelector) {
dom.findAll(cssSelector).forEach(elm => {
if (!elm.title && elm.innerText) {
elm.title = elm.innerText;
}
});
}
#executeScript() {
const script = dom
.findAll('.markdown-section>script')
.filter(s => !/template/.test(s.type))[0];
if (!script) {
return false;
}
const code = script.innerText.trim();
if (!code) {
return false;
}
new Function(code)();
}
#formatUpdated(html, updated, fn) {
updated =
typeof fn === 'function'
? fn(updated)
: typeof fn === 'string'
? tinydate(fn)(new Date(updated))
: updated;
return html.replace(/{docsify-updated}/g, updated);
}
#renderMain(html) {
const docsifyConfig = this.config;
const markdownElm = dom.find('.markdown-section');
const vueVersion =
'Vue' in window &&
window.Vue.version &&
Number(window.Vue.version.charAt(0));
const isMountedVue = elm => {
const isVue2 = Boolean(elm.__vue__ && elm.__vue__._isVue);
const isVue3 = Boolean(elm._vnode && elm._vnode.__v_skip);
return isVue2 || isVue3;
};
if ('Vue' in window) {
const mountedElms = dom
.findAll('.markdown-section > *')
.filter(elm => isMountedVue(elm));
// Destroy/unmount existing Vue instances
for (const mountedElm of mountedElms) {
if (vueVersion === 2) {
mountedElm.__vue__.$destroy();
} else if (vueVersion === 3) {
mountedElm.__vue_app__.unmount();
}
}
}
dom.setHTML(markdownElm, html);
// Execute markdown <script>
if (
docsifyConfig.executeScript ||
('Vue' in window && docsifyConfig.executeScript !== false)
) {
this.#executeScript();
}
// Handle Vue content not mounted by markdown <script>
if ('Vue' in window) {
const vueGlobalOptions = docsifyConfig.vueGlobalOptions || {};
const vueMountData = [];
const vueComponentNames = Object.keys(
docsifyConfig.vueComponents || {},
);
// Register global vueComponents
if (vueVersion === 2 && vueComponentNames.length) {
vueComponentNames.forEach(name => {
const isNotRegistered = !window.Vue.options.components[name];
if (isNotRegistered) {
window.Vue.component(name, docsifyConfig.vueComponents[name]);
}
});
}
// Store global data() return value as shared data object
if (
!this.#vueGlobalData &&
vueGlobalOptions.data &&
typeof vueGlobalOptions.data === 'function'
) {
this.#vueGlobalData = vueGlobalOptions.data();
}
// vueMounts
vueMountData.push(
...Object.keys(docsifyConfig.vueMounts || {})
.map(cssSelector => [
dom.find(markdownElm, cssSelector),
docsifyConfig.vueMounts[cssSelector],
])
.filter(([elm, vueConfig]) => elm),
);
// Template syntax, vueComponents, vueGlobalOptions ...
const reHasBraces = /{{2}[^{}]*}{2}/;
// Matches Vue full and shorthand syntax as attributes in HTML tags.
//
// Full syntax examples:
// v-foo, v-foo[bar], v-foo-bar, v-foo:bar-baz.prop
//
// Shorthand syntax examples:
// @foo, @foo.bar, @foo.bar.baz, @[foo], :foo, :[foo]
//
// Markup examples:
// <div v-html>{{ html }}</div>
// <div v-text="msg"></div>
// <div v-bind:text-content.prop="text">
// <button v-on:click="doThis"></button>
// <button v-on:click.once="doThis"></button>
// <button v-on:[event]="doThis"></button>
// <button @click.stop.prevent="doThis">
// <a :href="url">
// <a :[key]="url">
const reHasDirective = /<[^>/]+\s([@:]|v-)[\w-:.[\]]+[=>\s]/;
vueMountData.push(
...dom
.findAll('.markdown-section > *')
// Remove duplicates
.filter(elm => !vueMountData.some(([e, c]) => e === elm))
// Detect Vue content
.filter(elm => {
const isVueMount =
// is a component
elm.tagName.toLowerCase() in
(docsifyConfig.vueComponents || {}) ||
// has a component(s)
elm.querySelector(vueComponentNames.join(',') || null) ||
// has curly braces
reHasBraces.test(elm.outerHTML) ||
// has content directive
reHasDirective.test(elm.outerHTML);
return isVueMount;
})
.map(elm => {
// Clone global configuration
const vueConfig = {
...vueGlobalOptions,
};
// Replace vueGlobalOptions data() return value with shared data object.
// This provides a global store for all Vue instances that receive
// vueGlobalOptions as their configuration.
if (this.#vueGlobalData) {
vueConfig.data = () => this.#vueGlobalData;
}
return [elm, vueConfig];
}),
);
// Not found mounts but import Vue resource
if (vueMountData.length === 0) {
return;
}
// Mount
for (const [mountElm, vueConfig] of vueMountData) {
const isVueAttr = 'data-isvue';
const isSkipElm =
// Is an invalid tag
mountElm.matches('pre, :not([v-template]):has(pre), script') ||
// Is a mounted instance
isMountedVue(mountElm) ||
// Has mounted instance(s)
mountElm.querySelector(`[${isVueAttr}]`);
if (!isSkipElm) {
mountElm.setAttribute(isVueAttr, '');
if (vueVersion === 2) {
vueConfig.el = undefined;
new window.Vue(vueConfig).$mount(mountElm);
} else if (vueVersion === 3) {
const app = window.Vue.createApp(vueConfig);
// Register global vueComponents
vueComponentNames.forEach(name => {
const config = docsifyConfig.vueComponents[name];
app.component(name, config);
});
app.mount(mountElm);
}
}
}
}
}
#renderNameLink(vm) {
const el = dom.getNode('.app-name-link');
const nameLink = vm.config.nameLink;
const path = vm.route.path;
if (!el) {
return;
}
if (isPrimitive(vm.config.nameLink)) {
el.setAttribute('href', nameLink);
} else if (typeof nameLink === 'object') {
const match = Object.keys(nameLink).filter(
key => path.indexOf(key) > -1,
)[0];
el.setAttribute('href', nameLink[match]);
}
}
#renderSkipLink(vm) {
const { skipLink } = vm.config;
if (skipLink !== false) {
const el = dom.getNode('#skip-to-content');
let skipLinkText =
typeof skipLink === 'string' ? skipLink : 'Skip to main content';
if (skipLink?.constructor === Object) {
const matchingPath = Object.keys(skipLink).find(path =>
vm.route.path.startsWith(path.startsWith('/') ? path : `/${path}`),
);
const matchingText = matchingPath && skipLink[matchingPath];
skipLinkText = matchingText || skipLinkText;
}
if (el) {
el.innerHTML = skipLinkText;
} else {
const html = `<button type="button" id="skip-to-content" class="primary">${skipLinkText}</button>`;
dom.body.insertAdjacentHTML('afterbegin', html);
}
}
}
_renderSidebar(text) {
const { maxLevel, subMaxLevel, loadSidebar, hideSidebar } = this.config;
const sidebarEl = dom.getNode('aside.sidebar');
const sidebarNavEl = dom.getNode('.sidebar-nav');
const sidebarToggleEl = dom.getNode('button.sidebar-toggle');
if (hideSidebar) {
sidebarEl?.remove(sidebarEl);
sidebarToggleEl?.remove(sidebarToggleEl);
return null;
}
dom.setHTML('.sidebar-nav', this.compiler.sidebar(text, maxLevel));
sidebarToggleEl.setAttribute('aria-expanded', !isMobile());
const activeElmHref = this.router.toURL(this.route.path);
const activeEl = dom.find(`.sidebar-nav a[href="${activeElmHref}"]`);
this.#addTextAsTitleAttribute('.sidebar-nav a');
if (loadSidebar && activeEl) {
activeEl.parentNode.innerHTML +=
this.compiler.subSidebar(subMaxLevel) || '';
} else {
this.compiler.resetToc();
}
// Bind event
this._bindEventOnRendered(activeEl);
// Mark page links and groups
const pageLinks = dom.findAll(
sidebarNavEl,
'a:is(li > a, li > p > a):not(.section-link, [target="_blank"])',
);
const pageLinkGroups = dom
// NOTE: Using filter() method as a replacement for :has() selector. It
// would be preferable to use only 'li:not(:has(> a, > p > a))' selector
// but the :has() selector is not supported by our Jest test environment
// See: https://github.com/jsdom/jsdom/issues/3506#issuecomment-1769782333
.findAll(sidebarEl, 'li')
.filter(
elm =>
elm.querySelector(':scope > ul') &&
!elm.querySelectorAll(':scope > a, :scope > p > a').length,
);
pageLinks.forEach(elm => {
elm.classList.add('page-link');
});
pageLinkGroups.forEach(elm => {
elm.classList.add('group');
elm
.querySelector(':scope > p:not(:has(> *))')
?.classList.add('group-title');
});
}
_bindEventOnRendered(activeEl) {
const { autoHeader } = this.config;
this.onRender();
if (autoHeader && activeEl) {
const main = dom.getNode('#main');
const firstNode = main.children[0];
if (firstNode && firstNode.tagName !== 'H1') {
const h1 = this.compiler.header(activeEl.innerText, 1);
const wrapper = dom.create('div', h1);
dom.before(main, wrapper.children[0]);
}
}
}
_renderNav(text) {
if (!text) {
return;
}
const html = this.compiler.compile(text);
['.app-nav', '.app-nav-merged'].forEach(selector => {
dom.setHTML(selector, html);
this.#addTextAsTitleAttribute(`${selector} a`);
});
}
_renderMain(text, opt = {}, next) {
const { response } = this.route;
// Note: It is possible for the response to be undefined in environments
// where XMLHttpRequest has been modified or mocked
if (response && !response.ok && (!text || response.status !== 404)) {
text = `# ${response.status} - ${response.statusText}`;
}
this.callHook('beforeEach', text, result => {
let html;
const callback = () => {
if (opt.updatedAt) {
html = this.#formatUpdated(
html,
opt.updatedAt,
this.config.formatUpdated,
);
}
this.callHook('afterEach', html, hookData => {
this.#renderMain(hookData);
next();
});
};
if (this.isHTML) {
html = this.result = text;
callback();
} else {
prerenderEmbed(
{
compiler: this.compiler,
raw: result,
},
tokens => {
html = this.compiler.compile(tokens);
callback();
},
);
}
});
}
_renderCover(text, coverOnly) {
const el = dom.getNode('.cover');
const rootElm = document.documentElement;
const coverBg = getComputedStyle(rootElm).getPropertyValue('--cover-bg');
dom.toggleClass(
dom.getNode('main'),
coverOnly ? 'add' : 'remove',
'hidden',
);
if (!text) {
dom.toggleClass(el, 'remove', 'show');
return;
}
dom.toggleClass(el, 'add', 'show');
let html = this.coverIsHTML ? text : this.compiler.cover(text);
if (!coverBg) {
const mdBgMatch = html
.trim()
.match(
'<p><img.*?data-origin="(.*?)".*?alt="(.*?)"[^>]*?>([^<]*?)</p>$',
);
let mdCoverBg;
if (mdBgMatch) {
const [bgMatch, bgValue, bgType] = mdBgMatch;
// Color
if (bgType === 'color') {
mdCoverBg = bgValue;
}
// Image
else {
const path = !isAbsolutePath(bgValue)
? getPath(this.router.getBasePath(), bgValue)
: bgValue;
mdCoverBg = `center center / cover url(${path})`;
}
html = html.replace(bgMatch, '');
}
// Gradient background
else {
const degrees = Math.round((Math.random() * 120) / 2);
let hue1 = Math.round(Math.random() * 360);
let hue2 = Math.round(Math.random() * 360);
// Ensure hue1 and hue2 are at least 50 degrees apart
if (Math.abs(hue1 - hue2) < 50) {
const hueShift = Math.round(Math.random() * 25) + 25;
hue1 = Math.max(hue1, hue2) + hueShift;
hue2 = Math.min(hue1, hue2) - hueShift;
}
// OKLCH color
if (window?.CSS?.supports('color', 'oklch(0 0 0 / 1%)')) {
const l = 90; // Lightness
const c = 20; // Chroma
// prettier-ignore
mdCoverBg = `linear-gradient(
${degrees}deg,
oklch(${l}% ${c}% ${hue1}) 0%,
oklch(${l}% ${c}% ${hue2}) 100%
)`.replace(/\s+/g, ' ');
}
// HSL color (Legacy)
else {
const s = 100; // Saturation
const l = 85; // Lightness
const o = 100; // Opacity
// prettier-ignore
mdCoverBg = `linear-gradient(
${degrees}deg,
hsl(${hue1} ${s}% ${l}% / ${o}%) 0%,
hsl(${hue2} ${s}% ${l}% / ${o}%) 100%
)`.replace(/\s+/g, ' ');
}
}
rootElm.style.setProperty('--cover-bg', mdCoverBg);
}
dom.setHTML('.cover-main', html);
// Button styles
dom
.findAll('.cover-main > p:last-of-type > a:not([class])')
.forEach(elm => {
const buttonType = elm.matches(':first-child')
? 'primary'
: 'secondary';
elm.classList.add('button', buttonType);
});
}
_updateRender() {
// Render name link
this.#renderNameLink(this);
// Render skip link
this.#renderSkipLink(this);
}
initRender() {
const config = this.config;
// Init markdown compiler
this.compiler = new Compiler(config, this.router);
window.__current_docsify_compiler__ = this.compiler;
const id = config.el || '#app';
const el = dom.find(id);
if (el) {
let html = '';
if (config.repo) {
html += tpl.corner(config.repo, config.cornerExternalLinkTarget);
}
if (config.coverpage) {
html += tpl.cover();
}
if (config.logo) {
const isBase64 = /^data:image/.test(config.logo);
const isExternal = /(?:http[s]?:)?\/\//.test(config.logo);
const isRelative = /^\./.test(config.logo);
if (!isBase64 && !isExternal && !isRelative) {
config.logo = getPath(this.router.getBasePath(), config.logo);
}
}
html += tpl.main(config);
// Render main app
dom.setHTML(el, html, true);
} else {
this.rendered = true;
}
// Add nav
if (config.loadNavbar) {
const navEl = dom.find('nav') || dom.create('nav');
const isMergedSidebar = config.mergeNavbar;
navEl.classList.add('app-nav');
navEl.setAttribute('aria-label', 'secondary');
dom.body.prepend(navEl);
if (isMergedSidebar) {
const mergedNavEl = dom.create('div');
const sidebarEl = dom.find('.sidebar');
const sidebarNavEl = dom.find('.sidebar-nav');
mergedNavEl?.classList.add('app-nav-merged');
sidebarEl?.insertBefore(mergedNavEl, sidebarNavEl);
}
}
if (config.themeColor) {
dom.$.head.appendChild(
dom.create('div', tpl.theme(config.themeColor)).firstElementChild,
);
}
this._updateRender();
dom.toggleClass(dom.body, 'ready');
}
};
}