Skip to content

Commit ee7cfad

Browse files
kirkyoderimyelo
andcommitted
feat: implement support for chapter names in links (#19)
* implement chapter text with each pagination item * support conditional visibility of chapter text * support variable chapter name visibility * remove unnecessary var * default to true * demo changes * feat: keep default rendering consistent as the previous version * docs: add corssChapterText option Co-authored-by: yelo <zhihuzeye@gmail.com>
1 parent 6349ae8 commit ee7cfad

File tree

5 files changed

+43
-14
lines changed

5 files changed

+43
-14
lines changed

example/_sidebar.md

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
- [Home](/)
2-
- [Foo](foo.md#section-3)
3-
- [Bar A Long Long Long Title](bar.md)
4-
- [Baz](baz.md)
1+
- Chapter 1
2+
3+
- [Home](/)
4+
- [Foo](foo.md#section-3)
5+
- [Bar A Long Long Long Title](bar.md)
6+
- [Baz](baz.md)
7+
58
- Chapter 2
6-
- [Foo](foo.md)
7-
- [Bar](bar.md)
8-
- [Baz](baz.md)
9+
10+
- [Foo](foo.md)
11+
- [Bar](bar.md)
12+
- [Baz](baz.md)
13+
914
- Chapter 3
10-
- [Foo](foo.md)
11-
- [Bar](bar.md)
12-
- [Baz](baz.md)
15+
16+
- [Foo](foo.md)
17+
- [Bar](bar.md)
18+
- [Baz](baz.md)

example/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
previousText: '上一章节',
4040
nextText: '下一章节',
4141
crossChapter: true,
42+
crossChapterText: true,
4243
}
4344
}
4445
</script>

readme.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
pagination: {
2424
previousText: '上一章节',
2525
nextText: '下一章节',
26-
crossChapter: true
26+
crossChapter: true,
27+
crossChapterText: true,
2728
},
2829
}
2930
```
@@ -44,6 +45,11 @@
4445
* **Type:** ``Boolean``
4546
* **Description:** Allow navigation to previous/next chapter.
4647

48+
### pagination.crossChapterText
49+
* **Default:** `false`
50+
* **Type:** ``Boolean``
51+
* **Description:** Display chapter name.
52+
4753
## Example
4854
- [example/index.html](example/index.html)
4955
- [Tina.js](https://tina.js.org/)

src/pagination.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const DEFAULT_OPTIONS = {
1010
previousText: 'PREVIOUS',
1111
nextText: 'NEXT',
1212
crossChapter: false,
13+
crossChapterText: false,
1314
}
1415
const CONTAINER_CLASSNAME = 'docsify-pagination-container'
1516

@@ -19,6 +20,10 @@ const CONTAINER_CLASSNAME = 'docsify-pagination-container'
1920
function toArray (elements) {
2021
return Array.prototype.slice.call(elements)
2122
}
23+
function findChapter (element) {
24+
const container = closest(element, 'div > ul > li')
25+
return query('p', container)
26+
}
2227
function findHyperlink (element) {
2328
return element.href ? element : query('a', element)
2429
}
@@ -38,6 +43,7 @@ class Link {
3843
if (!element) {
3944
return
4045
}
46+
this.chapter = findChapter(element)
4147
this.hyperlink = findHyperlink(element)
4248
}
4349
toJSON () {
@@ -47,6 +53,7 @@ class Link {
4753
return {
4854
name: this.hyperlink.innerText,
4955
href: this.hyperlink.getAttribute('href'),
56+
chapterName: this.chapter && this.chapter.innerText || ''
5057
}
5158
}
5259
}
@@ -93,7 +100,9 @@ const template = {
93100
<span>${options.previousText}</span>
94101
</div>
95102
<div class="pagination-item-title">${data.prev.name}</div>
96-
</a>
103+
`,
104+
data.prev && options.crossChapterText && `<div class="pagination-item-subtitle">${data.prev.chapterName}</div>`,
105+
data.prev && `</a>
97106
</div>
98107
`,
99108
data.next && `
@@ -106,7 +115,9 @@ const template = {
106115
</svg>
107116
</div>
108117
<div class="pagination-item-title">${data.next.name}</div>
109-
</a>
118+
`,
119+
data.next && options.crossChapterText && `<div class="pagination-item-subtitle">${data.next.chapterName}</div>`,
120+
data.next && `</a>
110121
</div>
111122
`
112123
].filter(Boolean).join('')

src/stylesheet.css

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
text-decoration: underline;
2121
}
2222
.pagination-item:not(:last-child) a .pagination-item-label,
23-
.pagination-item:not(:last-child) a .pagination-item-title {
23+
.pagination-item:not(:last-child) a .pagination-item-title,
24+
.pagination-item:not(:last-child) a .pagination-item-subtitle {
2425
opacity: 0.3;
2526
transition: all 200ms;
2627
}
@@ -59,3 +60,7 @@
5960
.pagination-item-title {
6061
font-size: 1.6em;
6162
}
63+
.pagination-item-subtitle {
64+
text-transform: uppercase;
65+
opacity: 0.3;
66+
}

0 commit comments

Comments
 (0)