@@ -25,6 +25,25 @@ function stripQuotes(string) {
2525 return string ;
2626}
2727
28+ function diffInfo ( args , codeblock ) {
29+ if ( args ) {
30+ let lines = codeblock . split ( '\n' ) ;
31+
32+ args . forEach ( pD => {
33+ let operator = pD [ 0 ] ;
34+ let lineNo = + ( pD . replace ( operator , '' ) ) ;
35+ let text = lines [ lineNo - 1 ] ;
36+ if ( operator === '+' ) {
37+ lines [ lineNo - 1 ] = `<span class="diff-insertion"><span class="diff-operator">+</span>${ text } </span>` ;
38+ } else {
39+ lines [ lineNo - 1 ] = `<span class="diff-deletion"><span class="diff-operator">-</span>${ text } </span>` ;
40+ }
41+ } ) ;
42+ codeblock = lines . join ( '\n' ) ;
43+ }
44+ return codeblock ;
45+ }
46+
2847export function initialize ( /* application */ ) {
2948 showdown . subParser ( 'githubCodeBlocks' , function ( text , options , globals ) {
3049 // early exit if option is not enabled
@@ -70,6 +89,8 @@ export function initialize(/* application */) {
7089 let lineNumbersHTML = getLineNumbersHTML ( idCounter , codeblock ) ;
7190 idCounter ++ ;
7291
92+ let diffInfoArgs = attributes [ 'data-diff' ] ?. split ( ',' ) ;
93+
7394 assert ( `Language "${ language } " not found. Have you configured Prism correctly?` , ! language || Prism . languages [ language ] ) ;
7495
7596 if ( language && Prism . languages [ language ] ) {
@@ -79,6 +100,7 @@ export function initialize(/* application */) {
79100 codeblock = codeblock . replace ( / ¨ T / g, '¨' ) ;
80101
81102 let highlightedCodeBlock = Prism . highlight ( codeblock , Prism . languages [ language ] , language ) + end ;
103+ highlightedCodeBlock = diffInfo ( diffInfoArgs , highlightedCodeBlock ) ;
82104 codeblock = `<pre class="language-${ language } line-numbers"><code ${ language ? `class="${ language } language-${ language } "` : '' } >${ highlightedCodeBlock } ${ lineNumbersHTML } </code></pre>` ;
83105
84106 // Convert to the special characters Showdown uses again
@@ -89,27 +111,10 @@ export function initialize(/* application */) {
89111 codeblock = `<div class="filename ${ language } "><div class="ribbon"></div><span>${ attributes [ 'data-filename' ] || '' } </span>${ codeblock } </div>` ;
90112 }
91113 } else {
114+ codeblock = diffInfo ( diffInfoArgs , codeblock ) ;
92115 codeblock = `<pre class="language-none line-numbers"><code class="language-none">${ codeblock } ${ lineNumbersHTML } </code></pre>` ;
93116 }
94117
95- const diffInfo = attributes [ 'data-diff' ] ?. split ( ',' ) ;
96-
97- if ( diffInfo ) {
98- let lines = codeblock . split ( '\n' ) ;
99-
100- diffInfo . forEach ( pD => {
101- let operator = pD [ 0 ] ;
102- let lineNo = + ( pD . replace ( operator , '' ) ) ;
103- let text = lines [ lineNo - 1 ] ;
104- if ( operator === '+' ) {
105- lines [ lineNo - 1 ] = `<span class="diff-insertion"><span class="diff-operator">+</span>${ text } </span>` ;
106- } else {
107- lines [ lineNo - 1 ] = `<span class="diff-deletion"><span class="diff-operator">-</span>${ text } </span>` ;
108- }
109- } ) ;
110- codeblock = lines . join ( '\n' ) ;
111- }
112-
113118 codeblock = showdown . subParser ( 'hashBlock' ) ( codeblock , options , globals ) ;
114119
115120 // Since GHCodeblocks can be false positives, we need to
0 commit comments