forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisplay-tool-specific-content.js
More file actions
82 lines (68 loc) · 2.34 KB
/
display-tool-specific-content.js
File metadata and controls
82 lines (68 loc) · 2.34 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const supportedTools = ['cli', 'desktop', 'webui']
const detectedTools = new Set()
export default function displayToolSpecificContent () {
let tool = getDefaultTool()
if (!tool) tool = 'webui'
const toolsInContent = findToolSpecificContent(tool)
hideSwitcherLinks(toolsInContent)
showContentForTool(tool)
// configure links for switching tool content
switcherLinks().forEach(link => {
link.addEventListener('click', (event) => {
event.preventDefault()
showContentForTool(event.target.dataset.tool)
findToolSpecificContent(event.target.dataset.tool)
})
})
}
function showContentForTool (tool) {
// (de)activate switcher link appearances
switcherLinks().forEach(link => {
(link.dataset.tool === tool)
? link.classList.add('selected')
: link.classList.remove('selected')
})
}
function findToolSpecificContent (tool) {
// find all tool-specific *block* elements and hide or show as appropriate
// example: {{ #cli }} block content {{/cli}}
Array.from(document.querySelectorAll('.extended-markdown'))
.filter(el => supportedTools.some(tool => el.classList.contains(tool)))
.forEach(el => {
detectTools(el)
el.style.display = el.classList.contains(tool)
? ''
: 'none'
})
// find all tool-specific *inline* elements and hide or show as appropriate
// example: <span class="tool-cli">inline content</span>
Array.from(document.querySelectorAll('.tool-cli, .tool-desktop, .tool-webui'))
.forEach(el => {
detectTools(el)
el.style.display = el.classList.contains('tool-' + tool)
? ''
: 'none'
})
return Array.from(detectedTools)
}
// hide links for any tool-specific sections that are not present
function hideSwitcherLinks (toolsInContent) {
Array.from(document.querySelectorAll('a.tool-switcher'))
.forEach(link => {
if (toolsInContent.includes(link.dataset.tool)) return
link.style.display = 'none'
})
}
function detectTools (el) {
el.classList.forEach(elClass => {
const value = elClass.replace(/tool-/, '')
if (supportedTools.includes(value)) detectedTools.add(value)
})
}
function getDefaultTool () {
const el = document.querySelector('[data-default-tool]')
if (el) return el.dataset.defaultTool
}
function switcherLinks () {
return Array.from(document.querySelectorAll('a.tool-switcher'))
}