@@ -84,7 +84,7 @@ class App extends Component {
8484
8585 updateCodeSyntaxHighlighting = ( ) => {
8686 document . querySelectorAll ( "pre code" ) . forEach ( ( block ) => {
87- hljs . highlightBlock ( block ) ;
87+ hljs . highlightElement ( block ) ;
8888 } ) ;
8989 } ;
9090
@@ -105,42 +105,45 @@ class App extends Component {
105105 } ;
106106
107107 addCopyButtons = ( clipboard ) => {
108- document . querySelectorAll ( "pre code" ) . forEach ( function ( codeBlock ) {
108+ const codeBlockElems = document . querySelectorAll ( "pre code" ) ;
109+
110+ if ( codeBlockElems . length === 0 ) {
111+ return ;
112+ }
113+ codeBlockElems . forEach ( function ( codeBlock ) {
109114 var pre = codeBlock . parentNode ;
110115 var prevElem = pre . previousElementSibling ;
111116
112- if ( prevElem . type === "button" ) {
113- return ;
114- }
115-
116- var button = document . createElement ( "button" ) ;
117- button . className = "copy-code-button" ;
118- button . setAttribute ( "id" , "copy-code-button" ) ;
119- button . type = "button" ;
120- button . innerText = "Copy" ;
117+ if ( prevElem . type !== "button" ) {
118+ var button = document . createElement ( "button" ) ;
119+ button . className = "copy-code-button" ;
120+ button . setAttribute ( "id" , "copy-code-button" ) ;
121+ button . type = "button" ;
122+ button . innerText = "Copy" ;
121123
122- button . addEventListener ( "click" , function ( ) {
123- clipboard . writeText ( codeBlock . innerText ) . then (
124- function ( ) {
125- /* Chrome doesn't seem to blur automatically,
124+ button . addEventListener ( "click" , function ( ) {
125+ clipboard . writeText ( codeBlock . innerText ) . then (
126+ function ( ) {
127+ /* Chrome doesn't seem to blur automatically,
126128 leaving the button in a focused state. */
127- button . blur ( ) ;
128- button . innerText = "Copied!" ;
129- setTimeout ( function ( ) {
130- button . innerText = "Copy" ;
131- } , 2000 ) ;
132- } ,
133- function ( error ) {
134- button . innerText = "Error" ;
135- }
136- ) ;
137- } ) ;
129+ button . blur ( ) ;
130+ button . innerText = "Copied!" ;
131+ setTimeout ( function ( ) {
132+ button . innerText = "Copy" ;
133+ } , 2000 ) ;
134+ } ,
135+ function ( error ) {
136+ button . innerText = "Error" ;
137+ }
138+ ) ;
139+ } ) ;
138140
139- if ( pre . parentNode . classList . contains ( "highlight" ) ) {
140- var highlight = pre . parentNode ;
141- highlight . parentNode . insertBefore ( button , highlight ) ;
142- } else {
143- pre . parentNode . insertBefore ( button , pre ) ;
141+ if ( pre . parentNode . classList . contains ( "highlight" ) ) {
142+ var highlight = pre . parentNode ;
143+ highlight . parentNode . insertBefore ( button , highlight ) ;
144+ } else {
145+ pre . parentNode . insertBefore ( button , pre ) ;
146+ }
144147 }
145148 } ) ;
146149 } ;
0 commit comments