-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
index.js
146 lines (126 loc) Β· 3.8 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
import * as utils from './util'
import { scrollIntoView, activeLink } from './event'
import * as render from './render'
import Hook from './hook'
const OPTIONS = utils.merge({
el: '#app',
repo: '',
maxLevel: 6,
subMaxLevel: 0,
loadSidebar: null,
loadNavbar: null,
homepage: 'README.md',
coverpage: '',
basePath: '',
auto2top: false,
name: '',
themeColor: '',
nameLink: window.location.pathname,
ga: ''
}, window.$docsify)
const script = document.currentScript || [].slice.call(document.getElementsByTagName('script')).pop()
// load configuration for script attribute
if (script) {
for (const prop in OPTIONS) {
const val = script.getAttribute('data-' + utils.camel2kebab(prop))
OPTIONS[prop] = utils.isNil(val) ? OPTIONS[prop] : (val || true)
}
if (OPTIONS.loadSidebar === true) OPTIONS.loadSidebar = '_sidebar.md'
if (OPTIONS.loadNavbar === true) OPTIONS.loadNavbar = '_navbar.md'
if (OPTIONS.coverpage === true) OPTIONS.coverpage = '_coverpage.md'
if (OPTIONS.repo === true) OPTIONS.repo = ''
if (OPTIONS.name === true) OPTIONS.name = ''
}
const hook = new Hook()
// utils
window.$docsify = OPTIONS
window.Docsify = {
installed: true,
utils: utils.merge({}, utils),
hook
}
// load options
render.init()
let cacheRoute = null
let cacheXhr = null
const getAlias = function (route) {
if (OPTIONS.alias[route]) {
return getAlias(OPTIONS.alias[route])
} else {
return route
}
}
const mainRender = function (cb) {
let page
let route = utils.getRoute()
if (cacheRoute === route) return cb()
let basePath = cacheRoute = OPTIONS.basePath + route
if (!/\//.test(basePath)) {
basePath = ''
} else if (basePath && !/\/$/.test(basePath)) {
basePath = basePath.match(/(\S*\/)[^\/]+$/)[1]
}
// replace route
route = '/' + route
if (OPTIONS.alias && OPTIONS.alias[route]) {
route = getAlias(route)
} else {
route = (OPTIONS.basePath + route).replace(/\/+/, '/')
}
if (!route) {
page = OPTIONS.homepage || 'README.md'
} else if (/\.md$/.test(route)) {
page = route
} else if (/\/$/.test(route)) {
page = `${route}README.md`
} else {
page = `${route}.md`
}
// Render Cover page
if (OPTIONS.coverpage && page === ('/' + OPTIONS.homepage).replace(/\/+/, '/')) {
utils.load(OPTIONS.coverpage).then(render.renderCover)
}
cacheXhr && cacheXhr.abort && cacheXhr.abort()
// Render markdown file
cacheXhr = utils.load(page, 'GET', render.renderLoading)
cacheXhr.then(result => {
render.renderArticle(result)
// clear cover
if (OPTIONS.coverpage && page !== OPTIONS.homepage) render.renderCover()
// render sidebar
if (OPTIONS.loadSidebar) {
const renderSidebar = result => { render.renderSidebar(result); cb() }
utils.load(basePath + OPTIONS.loadSidebar).then(renderSidebar,
_ => utils.load(OPTIONS.loadSidebar).then(renderSidebar))
} else {
cb()
}
}, _ => render.renderArticle(null))
// Render navbar
if (OPTIONS.loadNavbar) {
utils.load(basePath + OPTIONS.loadNavbar).then(render.renderNavbar,
_ => utils.load(OPTIONS.loadNavbar).then(render.renderNavbar))
}
}
const Docsify = function () {
setTimeout(_ => {
;[].concat(OPTIONS.plugins).forEach(fn => typeof fn === 'function' && fn(hook))
window.Docsify.hook.emit('init')
const dom = document.querySelector(OPTIONS.el) || document.body
const replace = dom !== document.body
const main = function () {
mainRender(_ => {
scrollIntoView()
activeLink('nav')
window.Docsify.hook.emit('doneEach')
})
}
// Render app
render.renderApp(dom, replace)
main()
if (!/^#\//.test(window.location.hash)) window.location.hash = '#/'
window.addEventListener('hashchange', main)
window.Docsify.hook.emit('ready')
}, 0)
}
export default Docsify()