forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay-platform-specific-content.ts
117 lines (99 loc) · 4.36 KB
/
display-platform-specific-content.ts
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import parseUserAgent from './user-agent'
const supportedPlatforms = ['mac', 'windows', 'linux']
const detectedPlatforms = new Set()
// Emphasize content for the visitor's OS (inferred from user agent string)
export default function displayPlatformSpecificContent() {
let platform = getDefaultPlatform() || parseUserAgent().os
// adjust platform names to fit existing mac/windows/linux scheme
if (!platform) platform = 'linux'
if (platform === 'ios') platform = 'mac'
const platformsInContent = getDetectedPlatforms()
// when the `defaultPlatform` frontmatter isn't set and the article
// does not define all platforms in the content, documentation is hidden
// for users with the undefined platform. This sets a default
// platform for those users to prevent unintentionally hiding content
if (!platformsInContent.includes(platform)) {
// uses the order of the supportedPlatforms array to
// determine the default platform
platform = supportedPlatforms.filter((elem) => platformsInContent.includes(elem))[0]
}
showPlatformSpecificContent(platform)
hideSwitcherLinks(platformsInContent)
setActiveSwitcherLinks(platform)
// configure links for switching platform content
switcherLinks().forEach((link) => {
link.addEventListener('click', (event) => {
event.preventDefault()
const target = event.target as HTMLElement
setActiveSwitcherLinks(target.dataset.platform || '')
showPlatformSpecificContent(target.dataset.platform || '')
})
})
}
function setActiveSwitcherLinks(platform: string) {
// (de)activate switcher link appearances
switcherLinks().forEach((link) => {
link.dataset.platform === platform
? link.classList.add('selected')
: link.classList.remove('selected')
})
}
function showPlatformSpecificContent(platform: string) {
// find all platform-specific *block* elements and hide or show as appropriate
// example: {{ #mac }} block content {{/mac}}
const markdowns = Array.from(
document.querySelectorAll('.extended-markdown')
) as Array<HTMLElement>
markdowns
.filter((el) => supportedPlatforms.some((platform) => el.classList.contains(platform)))
.forEach((el) => {
el.style.display = el.classList.contains(platform) ? '' : 'none'
})
// find all platform-specific *inline* elements and hide or show as appropriate
// example: <span class="platform-mac">inline content</span>
const platforms = Array.from(
document.querySelectorAll('.platform-mac, .platform-windows, .platform-linux')
) as Array<HTMLElement>
platforms.forEach((el) => {
el.style.display = el.classList.contains('platform-' + platform) ? '' : 'none'
})
}
// hide links for any platform-specific sections that are not present
function hideSwitcherLinks(platformsInContent: Array<string>) {
const links = Array.from(
document.querySelectorAll('a.platform-switcher')
) as Array<HTMLAnchorElement>
links.forEach((link) => {
if (platformsInContent.includes(link.dataset.platform || '')) return
link.style.display = 'none'
})
}
// gets the list of detected platforms in the current article
function getDetectedPlatforms(): Array<string> {
// find all platform-specific *block* elements and hide or show as appropriate
// example: {{ #mac }} block content {{/mac}}
const allEls = Array.from(document.querySelectorAll('.extended-markdown')) as Array<HTMLElement>
allEls
.filter((el) => supportedPlatforms.some((platform) => el.classList.contains(platform)))
.forEach((el) => detectPlatforms(el))
// find all platform-specific *inline* elements and hide or show as appropriate
// example: <span class="platform-mac">inline content</span>
const platformEls = Array.from(
document.querySelectorAll('.platform-mac, .platform-windows, .platform-linux')
) as Array<HTMLElement>
platformEls.forEach((el) => detectPlatforms(el))
return Array.from(detectedPlatforms) as Array<string>
}
function detectPlatforms(el: HTMLElement) {
el.classList.forEach((elClass) => {
const value = elClass.replace(/platform-/, '')
if (supportedPlatforms.includes(value)) detectedPlatforms.add(value)
})
}
function getDefaultPlatform() {
const el = document.querySelector('[data-default-platform]') as HTMLElement
if (el) return el.dataset.defaultPlatform
}
function switcherLinks(): Array<HTMLAnchorElement> {
return Array.from(document.querySelectorAll('a.platform-switcher'))
}