|
100 | 100 | 'web.dev': {
|
101 | 101 | contentSelector: '#content'
|
102 | 102 | },
|
103 |
| - 'github.com': { |
104 |
| - contentSelector() { |
105 |
| - const README_SEL = '#readme' |
106 |
| - const WIKI_CONTENT_SEL = '#wiki-body' |
107 |
| - const ISSUE_CONTENT_SEL = '.comment' |
108 |
| - const matchedSel = [README_SEL, ISSUE_CONTENT_SEL, WIKI_CONTENT_SEL].find((sel) => { |
109 |
| - return !!document.querySelector(sel) |
110 |
| - }) |
| 103 | + 'github.com': function () { |
| 104 | + const README_SEL = '#readme' |
| 105 | + const WIKI_CONTENT_SEL = '#wiki-body' |
| 106 | + const ISSUE_CONTENT_SEL = '.comment .comment-body' |
| 107 | + |
| 108 | + let matchedContainer |
| 109 | + const matchedSel = [README_SEL, ISSUE_CONTENT_SEL, WIKI_CONTENT_SEL].find((sel) => { |
| 110 | + const c = document.querySelector(sel) |
| 111 | + if (c) { |
| 112 | + matchedContainer = c |
| 113 | + return true |
| 114 | + } |
| 115 | + }) |
| 116 | + |
| 117 | + if (!matchedSel) { |
| 118 | + return { |
| 119 | + contentSelect: false, |
| 120 | + } |
| 121 | + } |
111 | 122 |
|
112 |
| - if (matchedSel) return matchedSel |
| 123 | + const isIssueDetail = /\/issues\//.test(location.pathname) |
| 124 | + const ISSUE_DETAIL_HEADING_OFFSET = 60 |
113 | 125 |
|
114 |
| - return false |
115 |
| - }, |
116 |
| - scrollSmoothOffset() { |
117 |
| - const isIssueDetail = /\/issues\//.test(location.pathname) |
118 |
| - if (isIssueDetail) return -60 |
119 |
| - return 0 |
120 |
| - }, |
121 |
| - initialTop: 500, |
| 126 | + /** Ugly hack for github issues */ |
| 127 | + const onClick = isIssueDetail ? function (e) { |
| 128 | + const href = e.target.getAttribute('href') |
| 129 | + const header = document.body.querySelector(href) |
| 130 | + if (header) { |
| 131 | + const rect = header.getBoundingClientRect() |
| 132 | + const currentWindowScrollTop = document.documentElement.scrollTop |
| 133 | + const scrollY = rect.y + currentWindowScrollTop - ISSUE_DETAIL_HEADING_OFFSET |
| 134 | + |
| 135 | + window.scrollTo(0, scrollY) |
| 136 | + |
| 137 | + location.hash = href |
| 138 | + |
| 139 | + e.preventDefault() |
| 140 | + e.stopPropagation() |
| 141 | + } |
| 142 | + }: null |
| 143 | + |
| 144 | + return { |
| 145 | + contentSelector: matchedSel, |
| 146 | + hasInnerContainers: isIssueDetail ? true: false, |
| 147 | + scrollSmoothOffset: isIssueDetail ? -ISSUE_DETAIL_HEADING_OFFSET: 0, |
| 148 | + headingsOffset: isIssueDetail ? ISSUE_DETAIL_HEADING_OFFSET: 0, |
| 149 | + initialTop: 500, |
| 150 | + onClick, |
| 151 | + } |
122 | 152 | },
|
123 | 153 | 'developer.mozilla.org': {
|
124 | 154 | contentSelector: '#content'
|
|
159 | 189 | function getPageTocOptions() {
|
160 | 190 | let siteInfo = getSiteInfo()
|
161 | 191 | if (siteInfo) {
|
162 |
| - let siteSetting = siteInfo.siteSetting |
| 192 | + if (typeof siteInfo.siteSetting === 'function') { |
| 193 | + return siteInfo.siteSetting() |
| 194 | + } |
| 195 | + |
| 196 | + let siteSetting = { ...siteInfo.siteSetting } |
163 | 197 | if (siteSetting.shouldShow && !siteSetting.shouldShow()) {
|
164 | 198 | return
|
165 | 199 | }
|
|
169 | 203 | siteSetting = {...siteSetting, contentSelector}
|
170 | 204 | }
|
171 | 205 | if (typeof siteSetting.scrollSmoothOffset === 'function') {
|
172 |
| - siteSetting.scrollSmoothOffset = {...siteSetting, scrollSmoothOffset: siteSetting.scrollSmoothOffset()} |
| 206 | + siteSetting.scrollSmoothOffset = siteSetting.scrollSmoothOffset() |
173 | 207 | }
|
| 208 | + |
174 | 209 | console.log('[toc-bar] found site info for', siteInfo.siteName)
|
175 | 210 | return siteSetting
|
176 | 211 | }
|
@@ -613,8 +648,8 @@ a.toc-link {
|
613 | 648 |
|
614 | 649 | function main() {
|
615 | 650 | const options = getPageTocOptions()
|
616 |
| - if (options) { |
617 | 651 |
|
| 652 | + if (options) { |
618 | 653 | const tocBar = new TocBar(options)
|
619 | 654 | tocBar.initTocbot(options)
|
620 | 655 | tocBar.refreshStyle()
|
|
0 commit comments