@@ -11,24 +11,36 @@ export default function displayPlatformSpecificContent() {
11
11
if ( ! platform ) platform = 'linux'
12
12
if ( platform === 'ios' ) platform = 'mac'
13
13
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 )
15
27
16
28
hideSwitcherLinks ( platformsInContent )
17
29
18
- showContentForPlatform ( platform )
30
+ setActiveSwitcherLinks ( platform )
19
31
20
32
// configure links for switching platform content
21
33
switcherLinks ( ) . forEach ( ( link ) => {
22
34
link . addEventListener ( 'click' , ( event ) => {
23
35
event . preventDefault ( )
24
36
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 || '' )
27
39
} )
28
40
} )
29
41
}
30
42
31
- function showContentForPlatform ( platform : string ) {
43
+ function setActiveSwitcherLinks ( platform : string ) {
32
44
// (de)activate switcher link appearances
33
45
switcherLinks ( ) . forEach ( ( link ) => {
34
46
link . dataset . platform === platform
@@ -37,7 +49,7 @@ function showContentForPlatform(platform: string) {
37
49
} )
38
50
}
39
51
40
- function findPlatformSpecificContent ( platform : string ) {
52
+ function showPlatformSpecificContent ( platform : string ) {
41
53
// find all platform-specific *block* elements and hide or show as appropriate
42
54
// example: {{ #mac }} block content {{/mac}}
43
55
const markdowns = Array . from (
@@ -46,7 +58,6 @@ function findPlatformSpecificContent(platform: string) {
46
58
markdowns
47
59
. filter ( ( el ) => supportedPlatforms . some ( ( platform ) => el . classList . contains ( platform ) ) )
48
60
. forEach ( ( el ) => {
49
- detectPlatforms ( el )
50
61
el . style . display = el . classList . contains ( platform ) ? '' : 'none'
51
62
} )
52
63
@@ -56,11 +67,8 @@ function findPlatformSpecificContent(platform: string) {
56
67
document . querySelectorAll ( '.platform-mac, .platform-windows, .platform-linux' )
57
68
) as Array < HTMLElement >
58
69
platforms . forEach ( ( el ) => {
59
- detectPlatforms ( el )
60
70
el . style . display = el . classList . contains ( 'platform-' + platform ) ? '' : 'none'
61
71
} )
62
-
63
- return Array . from ( detectedPlatforms ) as Array < string >
64
72
}
65
73
66
74
// hide links for any platform-specific sections that are not present
@@ -74,6 +82,22 @@ function hideSwitcherLinks(platformsInContent: Array<string>) {
74
82
} )
75
83
}
76
84
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
+
77
101
function detectPlatforms ( el : HTMLElement ) {
78
102
el . classList . forEach ( ( elClass ) => {
79
103
const value = elClass . replace ( / p l a t f o r m - / , '' )
0 commit comments