@@ -98,6 +98,8 @@ document.querySelectorAll('.copy-to-clipboard-button').forEach(el => {
9898 for ( let i = 0 ; i < children . length ; i ++ ) {
9999 if ( children [ i ] . tagName . toLowerCase ( ) === 'pre' ) {
100100 return filterTextToCopy ( children [ i ] , children [ i ] . textContent ) ;
101+ } else {
102+ console . error ( "Error obtaining text, could not find PRE element." ) ;
101103 }
102104 }
103105 return undefined ;
@@ -122,11 +124,10 @@ document.querySelectorAll('.copy-to-clipboard-button').forEach(el => {
122124const NEW_LINE_EXP = / \n (? ! $ ) / g;
123125
124126function filterTextToCopy ( preElement , code ) {
125- // Filter out output lines.
126- // This may arise from the command-line or line-number plugins.
127+ // Filter out output lines from the command-line or line-number plugins.
127128 const lineNumbersToSkip = preElement . getAttribute ( 'data-output' ) ;
129+ const lines = code . split ( NEW_LINE_EXP ) ;
128130 if ( lineNumbersToSkip !== null ) {
129- const lines = code . split ( NEW_LINE_EXP ) ;
130131 const linesNum = lines . length ;
131132
132133 lineNumbersToSkip . split ( ',' ) . forEach ( function ( section ) {
@@ -150,13 +151,30 @@ function filterTextToCopy(preElement, code) {
150151 }
151152 } ) ;
152153
153- code = lines . filter ( s => s !== undefined ) . join ( '\n' ) ;
154+ code = lines . filter ( s => s !== undefined ) ;
154155 } else if ( preElement . classList . contains ( 'command-line' ) ) {
155156 const nonOutputLines = preElement . querySelectorAll ( '.token.command' ) ;
156157 code = Array . prototype . slice . call ( nonOutputLines )
157- . map ( e => e . textContent )
158- . join ( '\n' ) ;
158+ . map ( e => e . textContent ) ;
159+ } else {
160+ code = lines ;
161+ }
162+
163+ // Filter out diff lines from the diff-highlight plugin.
164+ let isDiffBlock = false ;
165+ for ( let i = 0 ; i < preElement . classList . length ; i ++ )
166+ if ( preElement . classList [ i ] . startsWith ( 'language-diff' ) ) {
167+ isDiffBlock = true ;
168+ break ;
169+ }
170+
171+ if ( isDiffBlock ) {
172+ code = code
173+ . map ( line => line . startsWith ( '-' ) ? undefined : line . slice ( 1 ) )
174+ . filter ( l => l !== undefined ) ;
159175 }
160176
177+ code = code . join ( '\n' ) ;
178+
161179 return code ;
162180}
0 commit comments