1
+ // NOTE(brentvatne): the package:version meta tag isn't available when this code
2
+ // first executes....
1
3
const DEFAULT_PLATFORM = 'android' ;
2
4
const DEPS_VERSIONS = {
3
5
'4' : [
@@ -23,16 +25,12 @@ const DEPS_VERSIONS = {
23
25
next : [ ] ,
24
26
} ;
25
27
26
- function getVersion ( ) {
27
- const navBarItems = document . getElementsByClassName ( 'navbar__items' ) ;
28
- if ( navBarItems [ 0 ] && navBarItems [ 0 ] . children [ 2 ] && navBarItems [ 0 ] . children [ 2 ] . textContent ) {
29
- return navBarItems [ 0 ] . children [ 2 ] . textContent ;
28
+ function getVersion ( ) {
29
+ if ( window . __reactNavigationVersion ) {
30
+ return window . __reactNavigationVersion ;
30
31
}
31
- return 'next' ;
32
32
}
33
- // todo: should get the version somewhere, maybe within the page html,
34
- // and match the appropriate version of react-navigation/stack/tabs/drawer
35
- // based on that
33
+
36
34
function getSnackUrl ( options ) {
37
35
let currentVersion = getVersion ( ) ;
38
36
let label = options . label || document . title ;
@@ -59,24 +57,20 @@ function getSnackUrl(options) {
59
57
}
60
58
}
61
59
62
- function findNearestPreElement ( node ) {
60
+ function findNearestCodeBlock ( node ) {
63
61
let nextElement = node . nextElementSibling ;
64
62
if ( ! nextElement && node . parentElement . tagName === 'P' ) {
65
63
nextElement = node . parentElement . nextElementSibling ;
66
64
}
67
65
68
66
while ( nextElement ) {
69
- if ( nextElement . tagName === 'DIV' && nextElement . className . includes ( 'mdxCodeBlock_node_modules-@docusaurus-theme-classic-src-theme-MDXComponents-' ) ) {
70
- nextElement = nextElement . firstChild ;
71
- }
72
67
if (
73
- nextElement . tagName === 'PRE' &&
74
- nextElement . children [ 1 ] &&
75
- nextElement . children [ 1 ] . tagName === 'CODE'
68
+ nextElement . tagName === 'DIV' &&
69
+ nextElement . className . includes (
70
+ 'mdxCodeBlock_node_modules-@docusaurus-theme-classic-src-theme-MDXComponents-'
71
+ )
76
72
) {
77
- return nextElement . children [ 1 ] ;
78
- } else {
79
- nextElement = nextElement . nextElementSibling ;
73
+ return nextElement ;
80
74
}
81
75
}
82
76
}
@@ -92,20 +86,19 @@ function appendSnackLink() {
92
86
}
93
87
94
88
samples . forEach ( samp => {
95
- let pre = findNearestPreElement ( samp ) ;
89
+ let codeBlock = findNearestCodeBlock ( samp ) ;
96
90
97
- if ( ! pre ) {
91
+ if ( ! codeBlock ) {
98
92
console . log (
99
- `<pre> tag with <code> child not found for <samp> element ${ samp . innerText } `
93
+ `Code block not found for <samp> element ${ samp . innerText } `
100
94
) ;
101
95
return ;
102
96
}
103
97
104
- let code = pre . innerText ;
98
+ let code = codeBlock . innerText ;
105
99
let label = samp . innerText ;
106
100
let templateId = samp . getAttribute ( 'id' ) ;
107
101
108
-
109
102
let link = document . createElement ( 'a' ) ;
110
103
link . className = 'snack-sample-link' ;
111
104
link . dataset . snack = true ;
@@ -117,7 +110,6 @@ function appendSnackLink() {
117
110
link . innerHTML = `Try this example on Snack ${ openIcon } ` ;
118
111
}
119
112
120
-
121
113
// Add the href and append the link element if we have some code
122
114
let href = getSnackUrl ( { code, templateId, label } ) ;
123
115
@@ -127,7 +119,7 @@ function appendSnackLink() {
127
119
128
120
link . href = href ;
129
121
130
- pre . insertAdjacentElement ( 'afterend' , link ) ;
122
+ codeBlock . insertAdjacentElement ( 'afterend' , link ) ;
131
123
132
124
// Don't try to add the link more than once!
133
125
samp . remove ( ) ;
0 commit comments