Skip to content

Commit 4ee9e8b

Browse files
cyrilfimyelo
authored andcommitted
Add the crossChapter option
1 parent 73329f4 commit 4ee9e8b

File tree

4 files changed

+31
-10
lines changed

4 files changed

+31
-10
lines changed

example/_sidebar.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,11 @@
22
- [Foo](foo.md#section-3)
33
- [Bar A Long Long Long Title](bar.md)
44
- [Baz](baz.md)
5+
- Chapter 2
6+
- [Foo](foo.md)
7+
- [Bar](bar.md)
8+
- [Baz](baz.md)
9+
- Chapter 3
10+
- [Foo](foo.md)
11+
- [Bar](bar.md)
12+
- [Baz](baz.md)

example/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
pagination: {
3939
previousText: '上一章节',
4040
nextText: '下一章节',
41+
crossChapter: true,
4142
}
4243
}
4344
</script>

readme.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
pagination: {
2424
previousText: '上一章节',
2525
nextText: '下一章节',
26+
crossChapter: true
2627
},
2728
}
2829
```
@@ -38,6 +39,11 @@
3839
* **Type:** ``String``
3940
* **Description:** The text of next label.
4041

42+
### pagination.crossChapter
43+
* **Default:** `false`
44+
* **Type:** ``Boolean``
45+
* **Description:** Allow navigation to previous/next chapter.
46+
4147
## Example
4248
- [example/index.html](example/index.html)
4349
- [Tina.js](https://tina.js.org/)

src/pagination.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import stylesheet from './stylesheet.css'
99
const DEFAULT_OPTIONS = {
1010
previousText: 'PREVIOUS',
1111
nextText: 'NEXT',
12+
crossChapter: false,
1213
}
1314
const CONTAINER_CLASSNAME = 'docsify-pagination-container'
1415

@@ -18,8 +19,8 @@ const CONTAINER_CLASSNAME = 'docsify-pagination-container'
1819
function toArray (elements) {
1920
return Array.prototype.slice.call(elements)
2021
}
21-
function findHyperlink (li) {
22-
return query('a', li)
22+
function findHyperlink (element) {
23+
return element.href ? element : query('a', element)
2324
}
2425
function isALinkTo (path, element) {
2526
if (arguments.length === 1) {
@@ -50,20 +51,25 @@ class Link {
5051
}
5152
}
5253

53-
function pagination (vm) {
54+
function pagination (vm, crossChapter) {
5455
try {
5556
const path = vm.route.path
5657
const all = toArray(query.all('.sidebar li a')).filter((element) => !matches(element, '.section-link'))
5758
const active = all.find(isALinkTo(path))
5859
const group = toArray((closest(active, 'ul') || {}).children)
5960
.filter((element) => element.tagName.toUpperCase() === 'LI')
60-
const index = group.findIndex((item) => {
61-
const hyperlink = findHyperlink(item)
62-
return hyperlink && isALinkTo(path, hyperlink)
63-
})
61+
const index = crossChapter
62+
? all.findIndex(isALinkTo(path))
63+
: group.findIndex((item) => {
64+
const hyperlink = findHyperlink(item)
65+
return hyperlink && isALinkTo(path, hyperlink)
66+
})
67+
68+
const links = crossChapter ? all : group
69+
6470
return {
65-
prev: new Link(group[index - 1]).toJSON(),
66-
next: new Link(group[index + 1]).toJSON(),
71+
prev: new Link(links[index - 1]).toJSON(),
72+
next: new Link(links[index + 1]).toJSON(),
6773
}
6874
} catch (error) {
6975
return {}
@@ -118,7 +124,7 @@ export function install (hook, vm) {
118124
if (!container) {
119125
return
120126
}
121-
container.innerHTML = template.inner(pagination(vm), options)
127+
container.innerHTML = template.inner(pagination(vm, options.crossChapter), options)
122128
}
123129

124130
hook.afterEach((html) => html + template.container())

0 commit comments

Comments
 (0)