Skip to content

Commit bb37969

Browse files
committed
feat: don't generate 'toc**' header id for github.com
1 parent 98ee81b commit bb37969

File tree

1 file changed

+54
-9
lines changed

1 file changed

+54
-9
lines changed

toc-bar.user.js

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,20 @@
4646
// ==/UserScript==
4747

4848
(function () {
49+
/**
50+
* @typedef {Object} SiteSetting
51+
* @property {string} contentSelector
52+
* @property {string} siteName
53+
* @property {Object} style
54+
* @property {Number} scrollSmoothOffset
55+
* @property {Number} initialTop
56+
* @property {Number} headingsOffset
57+
* @property {() => Boolean} shouldShow
58+
* @property {(ele) => HTMLElement} findHeaderId
59+
* @property {(e) => void} onClick
60+
*/
61+
62+
/** @type {{[key: string]: Partial<SiteSetting>}} */
4963
const SITE_SETTINGS = {
5064
jianshu: {
5165
contentSelector: '.ouvJEz',
@@ -108,11 +122,9 @@
108122
const WIKI_CONTENT_SEL = '#wiki-body'
109123
const ISSUE_CONTENT_SEL = '.comment .comment-body'
110124

111-
let matchedContainer
112125
const matchedSel = [README_SEL, ISSUE_CONTENT_SEL, WIKI_CONTENT_SEL].find((sel) => {
113126
const c = document.querySelector(sel)
114127
if (c) {
115-
matchedContainer = c
116128
return true
117129
}
118130
})
@@ -145,12 +157,24 @@
145157
}: null
146158

147159
return {
160+
siteName: 'github.com',
148161
contentSelector: matchedSel,
149162
hasInnerContainers: isIssueDetail ? true: false,
150163
scrollSmoothOffset: isIssueDetail ? -ISSUE_DETAIL_HEADING_OFFSET: 0,
151164
headingsOffset: isIssueDetail ? ISSUE_DETAIL_HEADING_OFFSET: 0,
152165
initialTop: 500,
153166
onClick,
167+
findHeaderId(ele) {
168+
let id
169+
let anchor = ele.querySelector('.anchor')
170+
if (anchor) id = anchor.getAttribute('id')
171+
172+
if (!anchor) {
173+
anchor = ele.querySelector('a')
174+
if (anchor) id = anchor.hash.replace(/^#/, '')
175+
}
176+
return id
177+
},
154178
}
155179
},
156180
'developer.mozilla.org': {
@@ -461,8 +485,15 @@ a.toc-link {
461485

462486
const DARKMODE_KEY = 'tocbar-darkmode'
463487

488+
/**
489+
* @typedef {Object} TocBarOptions
490+
* @property {String} [siteName]
491+
* @property {Number} [initialTop]
492+
*/
493+
464494
/**
465495
* @class
496+
* @param {TocBarOptions} options
466497
*/
467498
function TocBar(options={}) {
468499
this.options = options
@@ -486,6 +517,7 @@ a.toc-link {
486517
tocElement.classList.add(TOCBOT_CONTAINTER_CLASS)
487518
this.element.appendChild(tocElement)
488519

520+
POSITION_STORAGE.checkCache()
489521
const cachedPosition = POSITION_STORAGE.get(options.siteName)
490522
if (!isEmpty(cachedPosition)) {
491523
this.element.style.top = `${Math.max(0, cachedPosition.top)}px`
@@ -588,7 +620,11 @@ a.toc-link {
588620

589621
const refreshElement = header.querySelector('.toc-bar__refresh')
590622
refreshElement.addEventListener('click', () => {
591-
tocbot.refresh()
623+
try {
624+
tocbot.refresh()
625+
} catch (error) {
626+
console.warn('error in tocbot.refresh', error)
627+
}
592628
})
593629

594630
const toggleSchemeElement = header.querySelector('.toc-bar__scheme')
@@ -623,7 +659,7 @@ a.toc-link {
623659
this.element.style.top = `${newTop}px`
624660
}
625661

626-
const onMouseUp = (e) => {
662+
const onMouseUp = () => {
627663
Object.assign(dragState, {
628664
isDragging: false,
629665
})
@@ -671,13 +707,18 @@ a.toc-link {
671707
{
672708
tocSelector: `.${TOCBOT_CONTAINTER_CLASS}`,
673709
scrollSmoothOffset: options.scrollSmoothOffset || 0,
674-
// hasInnerContainers: true,
675710
headingObjectCallback(obj, ele) {
676711
// if there is no id on the header element, add one that derived from hash of header title
677712
if (!ele.id) {
678-
const newId = me.generateHeaderId(obj, ele)
679-
ele.setAttribute('id', newId)
680-
obj.id = newId
713+
let newId
714+
if (options.findHeaderId) {
715+
newId = options.findHeaderId(ele)
716+
}
717+
if (!newId) {
718+
newId = me.generateHeaderId(obj, ele)
719+
ele.setAttribute('id', newId)
720+
}
721+
if (newId) obj.id = newId
681722
}
682723
return obj
683724
},
@@ -687,7 +728,11 @@ a.toc-link {
687728
options
688729
)
689730
// console.log('tocbotOptions', tocbotOptions);
690-
tocbot.init(tocbotOptions)
731+
try {
732+
tocbot.init(tocbotOptions)
733+
} catch (error) {
734+
console.warn('error in tocbot.init', error)
735+
}
691736
},
692737
generateHeaderId(obj, ele) {
693738
const hash = doContentHash(obj.textContent)

0 commit comments

Comments
 (0)