Skip to content

Commit f0a083d

Browse files
rachmariheiskr
andauthored
set default platform when content doesnt include the os platform (github#19827)
* set default platform when content doesnt include the os platform * update source array * extract detect platforms into separate function * rename set to get * align names * add comments * Update display-platform-specific-content.ts * Update display-platform-specific-content.ts Co-authored-by: Kevin Heis <[email protected]>
1 parent c69a128 commit f0a083d

File tree

1 file changed

+34
-10
lines changed

1 file changed

+34
-10
lines changed

javascripts/display-platform-specific-content.ts

+34-10
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,36 @@ export default function displayPlatformSpecificContent() {
1111
if (!platform) platform = 'linux'
1212
if (platform === 'ios') platform = 'mac'
1313

14-
const platformsInContent = findPlatformSpecificContent(platform)
14+
const platformsInContent = getDetectedPlatforms()
15+
// when the `defaultPlatform` frontmatter isn't set and the article
16+
// does not define all platforms in the content, documentation is hidden
17+
// for users with the undefined platform. This sets a default
18+
// platform for those users to prevent unintentionally hiding content
19+
if (!platformsInContent.includes(platform)) {
20+
// uses the order of the supportedPlatforms array to
21+
// determine the default platform
22+
platform = supportedPlatforms
23+
.filter(elem => platformsInContent.includes(elem))[0]
24+
}
25+
26+
showPlatformSpecificContent(platform)
1527

1628
hideSwitcherLinks(platformsInContent)
1729

18-
showContentForPlatform(platform)
30+
setActiveSwitcherLinks(platform)
1931

2032
// configure links for switching platform content
2133
switcherLinks().forEach((link) => {
2234
link.addEventListener('click', (event) => {
2335
event.preventDefault()
2436
const target = event.target as HTMLElement
25-
showContentForPlatform(target.dataset.platform || '')
26-
findPlatformSpecificContent(target.dataset.platform || '')
37+
setActiveSwitcherLinks(target.dataset.platform || '')
38+
showPlatformSpecificContent(target.dataset.platform || '')
2739
})
2840
})
2941
}
3042

31-
function showContentForPlatform(platform: string) {
43+
function setActiveSwitcherLinks(platform: string) {
3244
// (de)activate switcher link appearances
3345
switcherLinks().forEach((link) => {
3446
link.dataset.platform === platform
@@ -37,7 +49,7 @@ function showContentForPlatform(platform: string) {
3749
})
3850
}
3951

40-
function findPlatformSpecificContent(platform: string) {
52+
function showPlatformSpecificContent(platform: string) {
4153
// find all platform-specific *block* elements and hide or show as appropriate
4254
// example: {{ #mac }} block content {{/mac}}
4355
const markdowns = Array.from(
@@ -46,7 +58,6 @@ function findPlatformSpecificContent(platform: string) {
4658
markdowns
4759
.filter((el) => supportedPlatforms.some((platform) => el.classList.contains(platform)))
4860
.forEach((el) => {
49-
detectPlatforms(el)
5061
el.style.display = el.classList.contains(platform) ? '' : 'none'
5162
})
5263

@@ -56,11 +67,8 @@ function findPlatformSpecificContent(platform: string) {
5667
document.querySelectorAll('.platform-mac, .platform-windows, .platform-linux')
5768
) as Array<HTMLElement>
5869
platforms.forEach((el) => {
59-
detectPlatforms(el)
6070
el.style.display = el.classList.contains('platform-' + platform) ? '' : 'none'
6171
})
62-
63-
return Array.from(detectedPlatforms) as Array<string>
6472
}
6573

6674
// hide links for any platform-specific sections that are not present
@@ -74,6 +82,22 @@ function hideSwitcherLinks(platformsInContent: Array<string>) {
7482
})
7583
}
7684

85+
// gets the list of detected platforms in the current article
86+
function getDetectedPlatforms (): Array<string> {
87+
// find all platform-specific *block* elements and hide or show as appropriate
88+
// example: {{ #mac }} block content {{/mac}}
89+
const allEls = Array.from(document.querySelectorAll('.extended-markdown')) as Array<HTMLElement>
90+
allEls.filter(el => supportedPlatforms.some(platform => el.classList.contains(platform)))
91+
.forEach(el => detectPlatforms(el))
92+
93+
// find all platform-specific *inline* elements and hide or show as appropriate
94+
// example: <span class="platform-mac">inline content</span>
95+
const platformEls = Array.from(document.querySelectorAll('.platform-mac, .platform-windows, .platform-linux')) as Array<HTMLElement>
96+
platformEls.forEach(el => detectPlatforms(el))
97+
98+
return Array.from(detectedPlatforms) as Array<string>
99+
}
100+
77101
function detectPlatforms(el: HTMLElement) {
78102
el.classList.forEach((elClass) => {
79103
const value = elClass.replace(/platform-/, '')

0 commit comments

Comments
 (0)