Skip to content

Commit 970dd0a

Browse files
committed
feat: Add header hash functionality
Change-Id: Icb7419cd92d2fc9bee9db9ba037ecc111470611e
1 parent cd878ed commit 970dd0a

File tree

3 files changed

+33
-23
lines changed

3 files changed

+33
-23
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22

33
A user script that adds floating widget displaying table of content of current page.
44

5-
Use [tocbot](https://tscanlin.github.io/tocbot) for toc generation.
5+
Currently only tailored for some popular sites. Feel free to tweak the settings or open a PR.
6+
7+
## ✨Features
8+
9+
- Use [tocbot](https://tscanlin.github.io/tocbot) for toc generation.
10+
- For some sites, there is no id on header elements so it would be impossible to navigate by clicking the toc link. Toc Bar will generate ids - which are derived from a simple hash of the header textContent, and prefixed with `tocbar-` - for these headers.
611

712
## Acknowledgements
813

images/screenshot-1.jpg

387 KB
Loading

toc-bar.user.js

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,19 @@
4141
'dev.to': {
4242
contentSelector: 'article',
4343
scrollSmoothOffset: -56,
44+
shouldShow() {
45+
return !location.pathname.startsWith('/search')
46+
},
4447
},
4548
zcfy: {
4649
contentSelector: '.markdown-body',
4750
},
4851
qq: {
4952
contentSelector: '.rich_media_content',
5053
},
54+
'medium.com': {
55+
contentSelector: 'article'
56+
},
5157
}
5258

5359
function getSiteInfo() {
@@ -88,21 +94,13 @@
8894
}
8995

9096
/**
91-
* @param {Number} len
97+
* @param {String} content
98+
* @return {String}
9299
*/
93-
const generateRandomStr = (function () {
94-
const ALPHABET =
95-
'1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
96-
const ALPHABET_LENGTH = ALPHABET.length
97-
return function (len) {
98-
const chars = []
99-
for (let i = 0; i < len; i++) {
100-
const index = Math.floor(Math.random() * ALPHABET_LENGTH)
101-
chars.push(ALPHABET[index])
102-
}
103-
return chars.join('')
104-
}
105-
})()
100+
function doContentHash(content) {
101+
const val = content.split('').reduce((prevHash, currVal) => (((prevHash << 5) - prevHash) + currVal.charCodeAt(0))|0, 0);
102+
return val.toString(32)
103+
}
106104

107105
// ---------------- TocBar ----------------------
108106
const TOC_BAR_STYLE = `
@@ -114,11 +112,10 @@
114112
width: 340px;
115113
font-size: 14px;
116114
box-sizing: border-box;
117-
padding: 10px;
115+
padding: 10px 10px 10px 0;
118116
background: #FEFEFE;
119-
box-shadow: 0 1px 1px #DDD;
117+
box-shadow: 0 1px 3px #DDD;
120118
border-radius: 4px;
121-
border: 1px solid #DDD;
122119
transition: width 0.2s ease;
123120
}
124121
@@ -185,6 +182,14 @@
185182
margin-left: 5px;
186183
}
187184
185+
.toc-bar a.toc-link {
186+
overflow: hidden;
187+
text-overflow: ellipsis;
188+
white-space: nowrap;
189+
display: inline-block;
190+
line-height: 1.4;
191+
}
192+
188193
.flex {
189194
display: flex;
190195
}
@@ -198,7 +203,7 @@
198203
padding-left: 8px;
199204
}
200205
201-
.toc-list-item:hover > a {
206+
.toc-list-item > a:hover {
202207
text-decoration: underline;
203208
}
204209
/* end override tocbot */
@@ -320,9 +325,9 @@
320325
scrollSmoothOffset: options.scrollSmoothOffset || 0,
321326
// hasInnerContainers: true,
322327
headingObjectCallback(obj, ele) {
323-
// if there is no id on the header element, add a random one
328+
// if there is no id on the header element, add one that derived from hash of header title
324329
if (!ele.id) {
325-
const newId = me.generateHeaderId()
330+
const newId = me.generateHeaderId(obj, ele)
326331
ele.setAttribute('id', newId)
327332
obj.id = newId
328333
}
@@ -336,8 +341,8 @@
336341
// console.log('tocbotOptions', tocbotOptions);
337342
tocbot.init(tocbotOptions)
338343
},
339-
generateHeaderId() {
340-
return `tocbar-${generateRandomStr(8)}`
344+
generateHeaderId(obj, ele) {
345+
return `tocbar-${doContentHash(obj.textContent)}`
341346
},
342347
/**
343348
* @method TocBar

0 commit comments

Comments
 (0)