@@ -20,7 +20,7 @@ const messages = {
2020 } ,
2121 'fr' : {
2222 'copy' : 'Copier' ,
23- 'copy_to_clipboard' : 'Copié dans le presse-papier' ,
23+ 'copy_to_clipboard' : 'Copier dans le presse-papier' ,
2424 'copy_success' : 'Copié !' ,
2525 'copy_failure' : 'Échec de la copie' ,
2626 } ,
@@ -102,18 +102,25 @@ const clearSelection = () => {
102102 }
103103}
104104
105- // Changes tooltip text for two seconds, then changes it back
105+ // Changes tooltip text for a moment, then changes it back
106+ // We want the timeout of our `success` class to be a bit shorter than the
107+ // tooltip and icon change, so that we can hide the icon before changing back.
108+ var timeoutIcon = 2000 ;
109+ var timeoutSuccessClass = 1500 ;
110+
106111const temporarilyChangeTooltip = ( el , oldText , newText ) => {
107112 el . setAttribute ( 'data-tooltip' , newText )
108113 el . classList . add ( 'success' )
109- setTimeout ( ( ) => el . setAttribute ( 'data-tooltip' , oldText ) , 2000 )
110- setTimeout ( ( ) => el . classList . remove ( 'success' ) , 2000 )
114+ // Remove success a little bit sooner than we change the tooltip
115+ // So that we can use CSS to hide the copybutton first
116+ setTimeout ( ( ) => el . classList . remove ( 'success' ) , timeoutSuccessClass )
117+ setTimeout ( ( ) => el . setAttribute ( 'data-tooltip' , oldText ) , timeoutIcon )
111118}
112119
113120// Changes the copy button icon for two seconds, then changes it back
114121const temporarilyChangeIcon = ( el ) => {
115122 el . innerHTML = iconCheck ;
116- setTimeout ( ( ) => { el . innerHTML = iconCopy } , 2000 )
123+ setTimeout ( ( ) => { el . innerHTML = iconCopy } , timeoutIcon )
117124}
118125
119126const addCopyButtonToCodeCells = ( ) => {
@@ -125,7 +132,8 @@ const addCopyButtonToCodeCells = () => {
125132 }
126133
127134 // Add copybuttons to all of our code cells
128- const codeCells = document . querySelectorAll ( 'div.highlight pre' )
135+ const COPYBUTTON_SELECTOR = 'div.highlight pre' ;
136+ const codeCells = document . querySelectorAll ( COPYBUTTON_SELECTOR )
129137 codeCells . forEach ( ( codeCell , index ) => {
130138 const id = codeCellId ( index )
131139 codeCell . setAttribute ( 'id' , id )
@@ -141,10 +149,25 @@ function escapeRegExp(string) {
141149 return string . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, '\\$&' ) ; // $& means the whole matched string
142150}
143151
152+ /**
153+ * Removes excluded text from a Node.
154+ *
155+ * @param {Node } target Node to filter.
156+ * @param {string } exclude CSS selector of nodes to exclude.
157+ * @returns {DOMString } Text from `target` with text removed.
158+ */
159+ function filterText ( target , exclude ) {
160+ const clone = target . cloneNode ( true ) ; // clone as to not modify the live DOM
161+ if ( exclude ) {
162+ // remove excluded nodes
163+ clone . querySelectorAll ( exclude ) . forEach ( node => node . remove ( ) ) ;
164+ }
165+ return clone . innerText ;
166+ }
167+
144168// Callback when a copy button is clicked. Will be passed the node that was clicked
145169// should then grab the text and replace pieces of text that shouldn't be used in output
146170function formatCopyText ( textContent , copybuttonPromptText , isRegexp = false , onlyCopyPromptLines = true , removePrompts = true , copyEmptyLines = true , lineContinuationChar = "" , hereDocDelim = "" ) {
147-
148171 var regexp ;
149172 var match ;
150173
@@ -199,7 +222,12 @@ function formatCopyText(textContent, copybuttonPromptText, isRegexp = false, onl
199222
200223var copyTargetText = ( trigger ) => {
201224 var target = document . querySelector ( trigger . attributes [ 'data-clipboard-target' ] . value ) ;
202- return formatCopyText ( target . innerText , '' , false , true , true , true , '' , '' )
225+
226+ // get filtered text
227+ let exclude = '.linenos' ;
228+
229+ let text = filterText ( target , exclude ) ;
230+ return formatCopyText ( text , '' , false , true , true , true , '' , '' )
203231}
204232
205233 // Initialize with a callback so we can modify the text before copy
0 commit comments