Skip to content

Commit

Permalink
feat: implement support for chapter names in links (#19)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
kirkyoder and imyelo committed Jan 9, 2020
1 parent 6349ae8 commit ee7cfad
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 14 deletions.
26 changes: 16 additions & 10 deletions example/_sidebar.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
- [Home](/)
- [Foo](foo.md#section-3)
- [Bar A Long Long Long Title](bar.md)
- [Baz](baz.md)
- Chapter 1

- [Home](/)
- [Foo](foo.md#section-3)
- [Bar A Long Long Long Title](bar.md)
- [Baz](baz.md)

- Chapter 2
- [Foo](foo.md)
- [Bar](bar.md)
- [Baz](baz.md)

- [Foo](foo.md)
- [Bar](bar.md)
- [Baz](baz.md)

- Chapter 3
- [Foo](foo.md)
- [Bar](bar.md)
- [Baz](baz.md)

- [Foo](foo.md)
- [Bar](bar.md)
- [Baz](baz.md)
1 change: 1 addition & 0 deletions example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
previousText: '上一章节',
nextText: '下一章节',
crossChapter: true,
crossChapterText: true,
}
}
</script>
Expand Down
8 changes: 7 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
pagination: {
previousText: '上一章节',
nextText: '下一章节',
crossChapter: true
crossChapter: true,
crossChapterText: true,
},
}
```
Expand All @@ -44,6 +45,11 @@
* **Type:** ``Boolean``
* **Description:** Allow navigation to previous/next chapter.

### pagination.crossChapterText
* **Default:** `false`
* **Type:** ``Boolean``
* **Description:** Display chapter name.

## Example
- [example/index.html](example/index.html)
- [Tina.js](https://tina.js.org/)
Expand Down
15 changes: 13 additions & 2 deletions src/pagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const DEFAULT_OPTIONS = {
previousText: 'PREVIOUS',
nextText: 'NEXT',
crossChapter: false,
crossChapterText: false,
}
const CONTAINER_CLASSNAME = 'docsify-pagination-container'

Expand All @@ -19,6 +20,10 @@ const CONTAINER_CLASSNAME = 'docsify-pagination-container'
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)
}
Expand All @@ -38,6 +43,7 @@ class Link {
if (!element) {
return
}
this.chapter = findChapter(element)
this.hyperlink = findHyperlink(element)
}
toJSON () {
Expand All @@ -47,6 +53,7 @@ class Link {
return {
name: this.hyperlink.innerText,
href: this.hyperlink.getAttribute('href'),
chapterName: this.chapter && this.chapter.innerText || ''
}
}
}
Expand Down Expand Up @@ -93,7 +100,9 @@ const template = {
<span>${options.previousText}</span>
</div>
<div class="pagination-item-title">${data.prev.name}</div>
</a>
`,
data.prev && options.crossChapterText && `<div class="pagination-item-subtitle">${data.prev.chapterName}</div>`,
data.prev && `</a>
</div>
`,
data.next && `
Expand All @@ -106,7 +115,9 @@ const template = {
</svg>
</div>
<div class="pagination-item-title">${data.next.name}</div>
</a>
`,
data.next && options.crossChapterText && `<div class="pagination-item-subtitle">${data.next.chapterName}</div>`,
data.next && `</a>
</div>
`
].filter(Boolean).join('')
Expand Down
7 changes: 6 additions & 1 deletion src/stylesheet.css
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
text-decoration: underline;
}
.pagination-item:not(:last-child) a .pagination-item-label,
.pagination-item:not(:last-child) a .pagination-item-title {
.pagination-item:not(:last-child) a .pagination-item-title,
.pagination-item:not(:last-child) a .pagination-item-subtitle {
opacity: 0.3;
transition: all 200ms;
}
Expand Down Expand Up @@ -59,3 +60,7 @@
.pagination-item-title {
font-size: 1.6em;
}
.pagination-item-subtitle {
text-transform: uppercase;
opacity: 0.3;
}

0 comments on commit ee7cfad

Please sign in to comment.