1- import Prism , { Languages } from 'prismjs'
1+ import Prism from 'prismjs'
22import * as Diff from 'diff'
33
44import type { Change } from 'diff'
@@ -22,6 +22,7 @@ const MODIFIED_START_TAG = '<span class="token modified">'
2222const MODIFIED_CLOSE_TAG = '</span>'
2323
2424function getDiffType ( diff : Change ) {
25+ if ( ! diff . count ) return 'disabled'
2526 return diff . added ? 'added' : diff . removed ? 'removed' : 'equal'
2627}
2728
@@ -37,37 +38,33 @@ const renderLine = (diffWords: Array<Change>) => {
3738 } ) . join ( '' )
3839}
3940
40- const getLines = ( diffsMap : Array < Diffs > ) => {
41- const result : any = [ ] // Array(0): prev, Array(1): current
41+ const getSplitLines = ( diffsMap : Array < Diffs > ) : Array < Lines > => {
42+ const result : Array < Lines > = [ ] // Array(0): prev, Array(1): current
4243 const lineNum = {
4344 prev : 0 ,
4445 current : 0
4546 }
4647
47- diffsMap . map ( ( diff ) => {
48- if ( diff . length < 2 ) {
49- diff . push ( { ...diff [ 0 ] } )
50- }
51-
52- const prevLines = diff [ 0 ] . value . replace ( / \n $ / , '' ) . split ( '\n' )
53- const currentLines = diff [ 1 ] . value . replace ( / \n $ / , '' ) . split ( '\n' )
48+ diffsMap . map ( ( diffs ) => {
49+ const prevLines = diffs [ 0 ] . value . replace ( / \n $ / , '' ) . split ( '\n' )
50+ const currentLines = diffs [ 1 ] . value . replace ( / \n $ / , '' ) . split ( '\n' )
5451 const loopCount = Math . max ( prevLines . length , currentLines . length )
5552
5653 for ( let i = 0 ; i < loopCount ; i ++ ) {
57- const hasPrevLine = typeof prevLines [ i ] !== 'undefined '
58- const hasCurrentLine = typeof currentLines [ i ] !== 'undefined '
54+ const hasPrevLine = getDiffType ( diffs [ 0 ] ) !== 'disabled '
55+ const hasCurrentLine = getDiffType ( diffs [ 1 ] ) !== 'disabled '
5956
6057 if ( hasPrevLine ) lineNum . prev = lineNum . prev + 1
6158 if ( hasCurrentLine ) lineNum . current = lineNum . current + 1
6259
6360 result . push ( [
6461 {
65- type : hasPrevLine ? getDiffType ( diff [ 0 ] ) : 'disabled' ,
62+ type : getDiffType ( diffs [ 0 ] ) ,
6663 lineNum : hasPrevLine ? lineNum . prev : undefined ,
6764 value : hasPrevLine ? prevLines [ i ] : undefined
6865 } ,
6966 {
70- type : hasCurrentLine ? getDiffType ( diff [ 1 ] ) : 'disabled' ,
67+ type : getDiffType ( diffs [ 1 ] ) ,
7168 lineNum : hasCurrentLine ? lineNum . current : undefined ,
7269 value : hasCurrentLine ? currentLines [ i ] : undefined
7370 }
@@ -78,7 +75,49 @@ const getLines = (diffsMap: Array<Diffs>) => {
7875 return result
7976}
8077
81- const renderLines = ( prev : string , current : string ) : ( ) => Array < Lines > => {
78+ const getUnifiedLines = ( diffsMap : Array < Diffs > ) : Array < Lines > => {
79+ const result : Array < Lines > = [ ] // Array(0)
80+ let lineNum = 0
81+
82+ diffsMap . map ( ( diffs ) => {
83+ const prevLines = diffs [ 0 ] . value . replace ( / \n $ / , '' ) . split ( '\n' )
84+ const currentLines = diffs [ 1 ] . value . replace ( / \n $ / , '' ) . split ( '\n' )
85+
86+ prevLines . map ( value => {
87+ const type = getDiffType ( diffs [ 0 ] )
88+
89+ if ( type !== 'removed' ) return
90+
91+ result . push ( [
92+ {
93+ type : getDiffType ( diffs [ 0 ] ) ,
94+ lineNum : undefined ,
95+ value : value
96+ }
97+ ] )
98+ } )
99+
100+ currentLines . map ( value => {
101+ const type = getDiffType ( diffs [ 1 ] )
102+
103+ if ( type === 'disabled' ) return
104+
105+ lineNum = lineNum + 1
106+
107+ result . push ( [
108+ {
109+ type : getDiffType ( diffs [ 1 ] ) ,
110+ lineNum,
111+ value : value
112+ }
113+ ] )
114+ } )
115+ } )
116+
117+ return result
118+ }
119+
120+ const renderLines = ( mode : Mode , prev : string , current : string ) : Array < Lines > => {
82121 const diffsMap = Diff . diffLines ( prev , current ) . reduce ( ( acc : Array < Diffs > , curr ) => {
83122 const type = getDiffType ( curr )
84123
@@ -101,7 +140,27 @@ const renderLines = (prev: string, current: string): () => Array<Lines> => {
101140 return acc
102141 } , [ ] )
103142
104- return getLines ( diffsMap )
143+ diffsMap . map ( ( diffs ) => {
144+ if ( diffs . length > 1 ) return
145+
146+ const type = getDiffType ( diffs [ 0 ] )
147+
148+ if ( type === 'added' ) {
149+ diffs . unshift ( { value : '' } )
150+ } else if ( type === 'removed' ) {
151+ diffs . push ( { value : '' } )
152+ } else if ( type === 'equal' ) {
153+ diffs . push ( { ...diffs [ 0 ] } )
154+ }
155+ } )
156+
157+ if ( mode === 'split' ) {
158+ return getSplitLines ( diffsMap )
159+ } else if ( mode === 'unified' ) {
160+ return getUnifiedLines ( diffsMap )
161+ } else {
162+ return [ ]
163+ }
105164}
106165
107166export { MODIFIED_START_TAG , MODIFIED_CLOSE_TAG , renderLines }
0 commit comments