@@ -19,108 +19,139 @@ import toCommaSeparatedList from 'utils/toCommaSeparatedList';
19
19
import { sharedStyles } from 'theme' ;
20
20
import createOgUrl from 'utils/createOgUrl' ;
21
21
22
- const MarkdownPage = ( {
23
- authors,
24
- createLink,
25
- date,
26
- enableScrollSync,
27
- ogDescription,
28
- location,
29
- markdownRemark,
30
- sectionList,
31
- titlePostfix = '' ,
32
- } ) => {
33
- const hasAuthors = authors . length > 0 ;
34
- const titlePrefix = markdownRemark . frontmatter . title || '' ;
35
-
36
- return (
37
- < Flex
38
- direction = "column"
39
- grow = "1"
40
- shrink = "0"
41
- halign = "stretch"
42
- css = { {
43
- width : '100%' ,
44
- flex : '1 0 auto' ,
45
- position : 'relative' ,
46
- zIndex : 0 ,
47
- } } >
48
- < TitleAndMetaTags
49
- ogDescription = { ogDescription }
50
- ogUrl = { createOgUrl ( markdownRemark . fields . slug ) }
51
- title = { `${ titlePrefix } ${ titlePostfix } ` }
52
- />
53
- < div css = { { flex : '1 0 auto' } } >
54
- < Container >
55
- < div css = { sharedStyles . articleLayout . container } >
56
- < Flex type = "article" direction = "column" grow = "1" halign = "stretch" >
57
- < MarkdownHeader title = { titlePrefix } />
58
-
59
- { ( date || hasAuthors ) && (
60
- < div css = { { marginTop : 15 } } >
61
- { date } { ' ' }
62
- { hasAuthors && (
63
- < span >
64
- by{ ' ' }
65
- { toCommaSeparatedList ( authors , author => (
66
- < a
67
- css = { sharedStyles . link }
68
- href = { author . frontmatter . url }
69
- key = { author . frontmatter . name } >
70
- { author . frontmatter . name }
71
- </ a >
72
- ) ) }
73
- </ span >
74
- ) }
75
- </ div >
76
- ) }
22
+ class MarkdownPage extends React . Component {
23
+ componentDidMount ( ) {
24
+ const codeSandboxLinks = document . querySelectorAll (
25
+ "a[href^='https://codesandbox.io']" ,
26
+ ) ;
77
27
78
- < div css = { sharedStyles . articleLayout . content } >
79
- < div
80
- css = { [ sharedStyles . markdown ] }
81
- dangerouslySetInnerHTML = { { __html : markdownRemark . html } }
82
- />
28
+ codeSandboxLinks . forEach ( link => {
29
+ link . onclick = e => {
30
+ const [ codeBlock ] = Array . prototype . filter . call (
31
+ link . parentNode . children ,
32
+ node => node . className === 'gatsby-highlight' ,
33
+ ) ;
34
+
35
+ if ( codeBlock ) {
36
+ const [ child ] = codeBlock . children ;
37
+ if ( child && child . tagName !== 'IFRAME' ) {
38
+ e . preventDefault ( ) ;
39
+
40
+ link . textContent = 'Open in CodeSandbox.' ;
41
+ codeBlock . innerHTML = `<iframe src="${ link . href } &fontsize=14&view=split" style="width: calc(100% + 28px); margin: 0 0 -7px -13px; height:${ codeBlock . getBoundingClientRect ( )
42
+ . height } px; border:0; border-radius: 12px; overflow:hidden;" sandbox="allow-modals allow-forms allow-popups allow-scripts allow-same-origin"></iframe>`;
43
+ }
44
+ }
45
+ } ;
46
+ } ) ;
47
+ }
83
48
84
- { markdownRemark . fields . path && (
85
- < div css = { { marginTop : 80 } } >
86
- < a
87
- css = { sharedStyles . articleLayout . editLink }
88
- href = { `https://github.com/reactjs/reactjs.org/tree/master/content/${ markdownRemark
89
- . fields . path } `} >
90
- Edit this page
91
- </ a >
49
+ render ( ) {
50
+ const {
51
+ authors,
52
+ createLink,
53
+ date,
54
+ enableScrollSync,
55
+ ogDescription,
56
+ location,
57
+ markdownRemark,
58
+ sectionList,
59
+ titlePostfix = '' ,
60
+ } = this . props ;
61
+
62
+ const hasAuthors = authors . length > 0 ;
63
+ const titlePrefix = markdownRemark . frontmatter . title || '' ;
64
+
65
+ return (
66
+ < Flex
67
+ direction = "column"
68
+ grow = "1"
69
+ shrink = "0"
70
+ halign = "stretch"
71
+ css = { {
72
+ width : '100%' ,
73
+ flex : '1 0 auto' ,
74
+ position : 'relative' ,
75
+ zIndex : 0 ,
76
+ } } >
77
+ < TitleAndMetaTags
78
+ ogDescription = { ogDescription }
79
+ ogUrl = { createOgUrl ( markdownRemark . fields . slug ) }
80
+ title = { `${ titlePrefix } ${ titlePostfix } ` }
81
+ />
82
+ < div css = { { flex : '1 0 auto' } } >
83
+ < Container >
84
+ < div css = { sharedStyles . articleLayout . container } >
85
+ < Flex type = "article" direction = "column" grow = "1" halign = "stretch" >
86
+ < MarkdownHeader title = { titlePrefix } />
87
+
88
+ { ( date || hasAuthors ) && (
89
+ < div css = { { marginTop : 15 } } >
90
+ { date } { ' ' }
91
+ { hasAuthors && (
92
+ < span >
93
+ by{ ' ' }
94
+ { toCommaSeparatedList ( authors , author => (
95
+ < a
96
+ css = { sharedStyles . link }
97
+ href = { author . frontmatter . url }
98
+ key = { author . frontmatter . name } >
99
+ { author . frontmatter . name }
100
+ </ a >
101
+ ) ) }
102
+ </ span >
103
+ ) }
92
104
</ div >
93
105
) }
106
+
107
+ < div css = { sharedStyles . articleLayout . content } >
108
+ < div
109
+ css = { [ sharedStyles . markdown ] }
110
+ dangerouslySetInnerHTML = { { __html : markdownRemark . html } }
111
+ />
112
+
113
+ { markdownRemark . fields . path && (
114
+ < div css = { { marginTop : 80 } } >
115
+ < a
116
+ css = { sharedStyles . articleLayout . editLink }
117
+ href = { `https://github.com/reactjs/reactjs.org/tree/master/content/${ markdownRemark
118
+ . fields . path } `} >
119
+ Edit this page
120
+ </ a >
121
+ </ div >
122
+ ) }
123
+ </ div >
124
+ </ Flex >
125
+
126
+ < div css = { sharedStyles . articleLayout . sidebar } >
127
+ < StickyResponsiveSidebar
128
+ enableScrollSync = { enableScrollSync }
129
+ createLink = { createLink }
130
+ defaultActiveSection = { findSectionForPath (
131
+ location . pathname ,
132
+ sectionList ,
133
+ ) }
134
+ location = { location }
135
+ sectionList = { sectionList }
136
+ />
94
137
</ div >
95
- </ Flex >
96
-
97
- < div css = { sharedStyles . articleLayout . sidebar } >
98
- < StickyResponsiveSidebar
99
- enableScrollSync = { enableScrollSync }
100
- createLink = { createLink }
101
- defaultActiveSection = { findSectionForPath (
102
- location . pathname ,
103
- sectionList ,
104
- ) }
105
- location = { location }
106
- sectionList = { sectionList }
107
- />
108
138
</ div >
109
- </ div >
110
- </ Container >
111
- </ div >
112
-
113
- { /* TODO Read prev/next from index map, not this way */ }
114
- { ( markdownRemark . frontmatter . next || markdownRemark . frontmatter . prev ) && (
115
- < NavigationFooter
116
- location = { location }
117
- next = { markdownRemark . frontmatter . next }
118
- prev = { markdownRemark . frontmatter . prev }
119
- />
120
- ) }
121
- </ Flex >
122
- ) ;
123
- } ;
139
+ </ Container >
140
+ </ div >
141
+
142
+ { /* TODO Read prev/next from index map, not this way */ }
143
+ { ( markdownRemark . frontmatter . next ||
144
+ markdownRemark . frontmatter . prev ) && (
145
+ < NavigationFooter
146
+ location = { location }
147
+ next = { markdownRemark . frontmatter . next }
148
+ prev = { markdownRemark . frontmatter . prev }
149
+ />
150
+ ) }
151
+ </ Flex >
152
+ ) ;
153
+ }
154
+ }
124
155
125
156
MarkdownPage . defaultProps = {
126
157
authors : [ ] ,
0 commit comments