Skip to content

Commit 27ce1b0

Browse files
committed
feat: optimize for github issues, related #2
1 parent 4706ed5 commit 27ce1b0

File tree

1 file changed

+55
-20
lines changed

1 file changed

+55
-20
lines changed

toc-bar.user.js

Lines changed: 55 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -100,25 +100,55 @@
100100
'web.dev': {
101101
contentSelector: '#content'
102102
},
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+
}
111122

112-
if (matchedSel) return matchedSel
123+
const isIssueDetail = /\/issues\//.test(location.pathname)
124+
const ISSUE_DETAIL_HEADING_OFFSET = 60
113125

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+
}
122152
},
123153
'developer.mozilla.org': {
124154
contentSelector: '#content'
@@ -159,7 +189,11 @@
159189
function getPageTocOptions() {
160190
let siteInfo = getSiteInfo()
161191
if (siteInfo) {
162-
let siteSetting = siteInfo.siteSetting
192+
if (typeof siteInfo.siteSetting === 'function') {
193+
return siteInfo.siteSetting()
194+
}
195+
196+
let siteSetting = { ...siteInfo.siteSetting }
163197
if (siteSetting.shouldShow && !siteSetting.shouldShow()) {
164198
return
165199
}
@@ -169,8 +203,9 @@
169203
siteSetting = {...siteSetting, contentSelector}
170204
}
171205
if (typeof siteSetting.scrollSmoothOffset === 'function') {
172-
siteSetting.scrollSmoothOffset = {...siteSetting, scrollSmoothOffset: siteSetting.scrollSmoothOffset()}
206+
siteSetting.scrollSmoothOffset = siteSetting.scrollSmoothOffset()
173207
}
208+
174209
console.log('[toc-bar] found site info for', siteInfo.siteName)
175210
return siteSetting
176211
}
@@ -613,8 +648,8 @@ a.toc-link {
613648

614649
function main() {
615650
const options = getPageTocOptions()
616-
if (options) {
617651

652+
if (options) {
618653
const tocBar = new TocBar(options)
619654
tocBar.initTocbot(options)
620655
tocBar.refreshStyle()

0 commit comments

Comments
 (0)