-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathpagination.js
178 lines (160 loc) · 4.93 KB
/
pagination.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
import query from 'component-query'
import closest from 'component-closest'
import matches from 'component-matches-selector'
import './stylesheet.css'
/**
* constants
*/
const DEFAULT_OPTIONS = (config) => ({
previousText: 'PREVIOUS',
nextText: 'NEXT',
crossChapter: false,
crossChapterText: false,
})
const CONTAINER_CLASSNAME = 'docsify-pagination-container'
/**
* basic utilities
*/
function toArray (elements) {
return Array.prototype.slice.call(elements)
}
function findChapter (element) {
const container = closest(element, 'div > ul > li')
return query('p', container)
}
function findHyperlink (element) {
return element.href ? element : query('a', element)
}
function normailizeLink (path) {
if (path && path.toUpperCase() === '#/README') {
return '#/'
}
return path
}
function isALinkTo (path, element) {
if (arguments.length === 1) {
return (element) => isALinkTo(path, element)
}
return normailizeLink(decodeURIComponent(element.getAttribute('href').split('?')[0])) === normailizeLink(decodeURIComponent(path))
}
/**
* core renderer
*/
class Link {
constructor (element) {
if (!element) {
return
}
this.chapter = findChapter(element)
this.hyperlink = findHyperlink(element)
}
toJSON () {
if (!this.hyperlink) {
return
}
return {
name: this.hyperlink.innerText,
href: this.hyperlink.getAttribute('href'),
chapterName: this.chapter && this.chapter.innerText || '',
isExternal: this.hyperlink.getAttribute('target') === '_blank',
}
}
}
function pagination (vm, { crossChapter }) {
try {
const path = vm.router.toURL(vm.route.path)
const all = toArray(query.all('.sidebar-nav li a')).filter((element) => !matches(element, '.section-link'))
const active = all.find(isALinkTo(path))
const group = toArray((closest(active, 'ul') || {}).children)
.filter((element) => element.tagName.toUpperCase() === 'LI')
const index = crossChapter
? all.findIndex(isALinkTo(path))
: group.findIndex((item) => {
const hyperlink = findHyperlink(item)
return hyperlink && isALinkTo(path, hyperlink)
})
const links = crossChapter ? all : group
return {
route: vm.route,
prev: new Link(links[index - 1]).toJSON(),
next: new Link(links[index + 1]).toJSON(),
}
} catch (error) {
return {
route: {}
}
}
}
const template = {
container () {
return `<div class="${CONTAINER_CLASSNAME}"></div>`
},
inner (data, options) {
const { previousText, nextText } = getLocalizationTexts(options, data.route.path)
return [
data.prev && `
<div class="pagination-item pagination-item--previous">
<a href="${data.prev.href}" ${data.prev.isExternal ? 'target="_blank"' : ''}>
<div class="pagination-item-label">
<svg width="10" height="16" viewBox="0 0 10 16" xmlns="http://www.w3.org/2000/svg">
<polyline fill="none" vector-effect="non-scaling-stroke" points="8,2 2,8 8,14"/>
</svg>
<span>${previousText}</span>
</div>
<div class="pagination-item-title">${data.prev.name}</div>
`,
data.prev && options.crossChapterText && `<div class="pagination-item-subtitle">${data.prev.chapterName}</div>`,
data.prev && `</a>
</div>
`,
data.next && `
<div class="pagination-item pagination-item--next">
<a href="${data.next.href}" ${data.next.isExternal ? 'target="_blank"' : ''}>
<div class="pagination-item-label">
<span>${nextText}</span>
<svg width="10" height="16" viewBox="0 0 10 16" xmlns="http://www.w3.org/2000/svg">
<polyline fill="none" vector-effect="non-scaling-stroke" points="2,2 8,8 2,14"/>
</svg>
</div>
<div class="pagination-item-title">${data.next.name}</div>
`,
data.next && options.crossChapterText && `<div class="pagination-item-subtitle">${data.next.chapterName}</div>`,
data.next && `</a>
</div>
`
].filter(Boolean).join('')
},
}
function getLocalizationTexts (options, path) {
const texts = {}
;['previousText', 'nextText'].forEach(key => {
const text = options[key]
if (typeof text === 'string') {
texts[key] = text
} else {
Object.keys(text).some(local => {
const isMatch = path && path.indexOf(local) > -1
texts[key] = isMatch ? text[local] : text
return isMatch
})
}
})
return texts
}
/**
* installation
*/
export function install (hook, vm) {
const options = Object.assign(
{},
DEFAULT_OPTIONS(vm.config),
vm.config.pagination || {}
)
function render () {
const container = query(`.${CONTAINER_CLASSNAME}`)
if (!container) return
container.innerHTML = template.inner(pagination(vm, options), options)
}
hook.afterEach((html) => html + template.container())
hook.doneEach(() => render())
}