forked from electron/electronjs.org-old
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docs-table-of-contents.js
52 lines (46 loc) · 1.91 KB
/
docs-table-of-contents.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const tocbot = require('tocbot')
function generateTableOfContents() {
const tocElement = document.querySelector('.docs__table-of-contents')
if (!!tocElement) {
tocbot.init({
tocSelector: '.docs__table-of-contents',
linkClass: 'table-of-contents__link',
activeLinkClass: 'table-of-contents__link--active',
listClass: 'table-of-contents__list',
listItemClass: 'table-of-contents__list-item',
contentSelector: '.markdown-body',
headingSelector: 'h2, h3, h4, h5, h6', // avoid h1 because there's only one h1
hasInnerContainers: true,
collapseDepth: 6, // don't collapse any elements (looks bad with large lists)
disableTocScrollSync: true, // don't scroll TOC with page scroll (looks bad with large lists)
headingLabelCallback: (str) => {
/**
* Clean up table of content strings using regex. We want to avoid
* having long and repetitive strings in the API section like the following:
* * ipcMain.removeListener(channel, listener)
* * ipcMain.removeAllListeners([channel])
* * ipcMain.handle(channel, listener)
* * ...
*/
if (!window.location.pathname.startsWith('/docs/api/')) {
return str
}
// For events: `Event: 'close'` becomes `close`
const eventsMatch = str.match(/Event: '([a-z]*(?:-[a-z]+)*)'/)
// For properties / methods: `win.previewFile(path[, displayName]) macOS` becomes `previewFile`
const propsMethodsMatch = str.match(/^[a-zA-Z]+\.((?:[a-zA-Z]+[\.]?)+)/)
if (eventsMatch) {
return eventsMatch[1]
} else if (propsMethodsMatch) {
return propsMethodsMatch[1]
}
return str
},
})
// The ToC is empty so we remove it
if (!tocElement.hasChildNodes()) {
tocElement.closest('.docs__nav-wrapper').remove()
}
}
}
module.exports = generateTableOfContents