|
41 | 41 | 'dev.to': {
|
42 | 42 | contentSelector: 'article',
|
43 | 43 | scrollSmoothOffset: -56,
|
| 44 | + shouldShow() { |
| 45 | + return !location.pathname.startsWith('/search') |
| 46 | + }, |
44 | 47 | },
|
45 | 48 | zcfy: {
|
46 | 49 | contentSelector: '.markdown-body',
|
47 | 50 | },
|
48 | 51 | qq: {
|
49 | 52 | contentSelector: '.rich_media_content',
|
50 | 53 | },
|
| 54 | + 'medium.com': { |
| 55 | + contentSelector: 'article' |
| 56 | + }, |
51 | 57 | }
|
52 | 58 |
|
53 | 59 | function getSiteInfo() {
|
|
88 | 94 | }
|
89 | 95 |
|
90 | 96 | /**
|
91 |
| - * @param {Number} len |
| 97 | + * @param {String} content |
| 98 | + * @return {String} |
92 | 99 | */
|
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 | + } |
106 | 104 |
|
107 | 105 | // ---------------- TocBar ----------------------
|
108 | 106 | const TOC_BAR_STYLE = `
|
|
114 | 112 | width: 340px;
|
115 | 113 | font-size: 14px;
|
116 | 114 | box-sizing: border-box;
|
117 |
| - padding: 10px; |
| 115 | + padding: 10px 10px 10px 0; |
118 | 116 | background: #FEFEFE;
|
119 |
| - box-shadow: 0 1px 1px #DDD; |
| 117 | + box-shadow: 0 1px 3px #DDD; |
120 | 118 | border-radius: 4px;
|
121 |
| - border: 1px solid #DDD; |
122 | 119 | transition: width 0.2s ease;
|
123 | 120 | }
|
124 | 121 |
|
|
185 | 182 | margin-left: 5px;
|
186 | 183 | }
|
187 | 184 |
|
| 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 | +
|
188 | 193 | .flex {
|
189 | 194 | display: flex;
|
190 | 195 | }
|
|
198 | 203 | padding-left: 8px;
|
199 | 204 | }
|
200 | 205 |
|
201 |
| -.toc-list-item:hover > a { |
| 206 | +.toc-list-item > a:hover { |
202 | 207 | text-decoration: underline;
|
203 | 208 | }
|
204 | 209 | /* end override tocbot */
|
|
320 | 325 | scrollSmoothOffset: options.scrollSmoothOffset || 0,
|
321 | 326 | // hasInnerContainers: true,
|
322 | 327 | 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 |
324 | 329 | if (!ele.id) {
|
325 |
| - const newId = me.generateHeaderId() |
| 330 | + const newId = me.generateHeaderId(obj, ele) |
326 | 331 | ele.setAttribute('id', newId)
|
327 | 332 | obj.id = newId
|
328 | 333 | }
|
|
336 | 341 | // console.log('tocbotOptions', tocbotOptions);
|
337 | 342 | tocbot.init(tocbotOptions)
|
338 | 343 | },
|
339 |
| - generateHeaderId() { |
340 |
| - return `tocbar-${generateRandomStr(8)}` |
| 344 | + generateHeaderId(obj, ele) { |
| 345 | + return `tocbar-${doContentHash(obj.textContent)}` |
341 | 346 | },
|
342 | 347 | /**
|
343 | 348 | * @method TocBar
|
|
0 commit comments