-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
index.js
117 lines (101 loc) 路 3.14 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
import * as utils from './util'
import { scrollIntoView, activeLink } from './event'
import * as render from './render'
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
}, 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 = ''
}
// utils
window.$docsify = OPTIONS
window.Docsify = {
installed: true,
utils: utils.merge({}, utils)
}
// load options
render.init()
let cacheRoute = null
let cacheXhr = null
const mainRender = function (cb) {
const route = OPTIONS.basePath + utils.getRoute()
if (cacheRoute === route) return cb()
let basePath = cacheRoute = route
if (!/\//.test(basePath)) {
basePath = ''
} else if (basePath && !/\/$/.test(basePath)) {
basePath = basePath.match(/(\S*\/)[^\/]+$/)[1]
}
let page
if (!route) {
page = OPTIONS.homepage || 'README.md'
} else if (/\/$/.test(route)) {
page = `${route}README.md`
} else {
page = `${route}.md`
}
// Render Cover page
if (OPTIONS.coverpage && page === OPTIONS.homepage) {
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 () {
const dom = document.querySelector(OPTIONS.el) || document.body
const replace = dom !== document.body
const main = function () {
mainRender(_ => {
scrollIntoView()
activeLink('nav')
;[].concat(window.$docsify.plugins).forEach(fn => fn && fn())
})
}
// Render app
render.renderApp(dom, replace)
main()
if (!/^#\//.test(window.location.hash)) window.location.hash = '#/'
window.addEventListener('hashchange', main)
}
export default Docsify()