@@ -194,7 +194,7 @@ func diffToHTML(hcd *HighlightCodeDiff, diffs []diffmatchpatch.Diff, lineType Di
194
194
buf := bytes .NewBuffer (nil )
195
195
if hcd != nil {
196
196
for _ , tag := range hcd .lineWrapperTags {
197
- buf .WriteString (tag )
197
+ buf .WriteString (tag ) // restore the line wrapper tags <span class="line"> and <span class="cl">
198
198
}
199
199
}
200
200
for _ , diff := range diffs {
@@ -345,40 +345,37 @@ func (hcd *HighlightCodeDiff) convertToPlaceholders(highlightCode string) string
345
345
}
346
346
347
347
var tagInMap string
348
- if tag [1 ] == '/' {
349
- // closed tag
348
+ if tag [1 ] == '/' { // for closed tag
350
349
if len (tagStack ) == 0 {
351
- break // error , no open tag but see close tag
350
+ break // invalid diff result , no open tag but see close tag
352
351
}
353
352
// make sure the closed tag in map is related to the open tag, to make the diff algorithm can match the open/closed tags
354
- // the closed tag should be "</span><!-- <span the-open> -->" for "<span the-open>"
353
+ // the closed tag will be recorded in the map by key "</span><!-- <span the-open> -->" for "<span the-open>"
355
354
tagInMap = tag + "<!-- " + tagStack [len (tagStack )- 1 ] + "-->"
356
355
tagStack = tagStack [:len (tagStack )- 1 ]
357
- } else {
356
+ } else { // for open tag
358
357
tagInMap = tag
359
358
tagStack = append (tagStack , tag )
360
359
}
360
+
361
+ // remember the placeholder and tag in the map
361
362
placeholder , ok := hcd .tagPlaceholderMap [tagInMap ]
362
363
if ! ok {
363
364
placeholder = hcd .nextPlaceholder ()
364
365
hcd .tagPlaceholderMap [tagInMap ] = placeholder
365
366
hcd .placeholderTagMap [placeholder ] = tagInMap
366
367
}
367
- res .WriteRune (placeholder )
368
+
369
+ res .WriteRune (placeholder ) // use the placeholder to replace the tag
368
370
}
369
371
res .WriteString (s )
370
372
return res .String ()
371
373
}
372
374
373
- func (hcd * HighlightCodeDiff ) recoverOneDiff (lastActiveTag string , diff * diffmatchpatch.Diff ) ( activeTag string ) {
375
+ func (hcd * HighlightCodeDiff ) recoverOneDiff (diff * diffmatchpatch.Diff ) {
374
376
sb := strings.Builder {}
375
377
var tagStack []string
376
378
377
- if lastActiveTag != "" {
378
- tagStack = append (tagStack , lastActiveTag )
379
- sb .WriteString (lastActiveTag )
380
- }
381
-
382
379
for _ , r := range diff .Text {
383
380
tag , ok := hcd .placeholderTagMap [r ]
384
381
if ! ok {
@@ -389,7 +386,7 @@ func (hcd *HighlightCodeDiff) recoverOneDiff(lastActiveTag string, diff *diffmat
389
386
if tag [1 ] == '/' {
390
387
tagToRecover = tag [:strings .IndexByte (tag , '>' )+ 1 ]
391
388
if len (tagStack ) == 0 {
392
- continue // if no open tag, skip the closed tag
389
+ continue // if no open tag yet , skip the closed tag
393
390
}
394
391
tagStack = tagStack [:len (tagStack )- 1 ]
395
392
} else {
@@ -400,8 +397,6 @@ func (hcd *HighlightCodeDiff) recoverOneDiff(lastActiveTag string, diff *diffmat
400
397
}
401
398
402
399
if len (tagStack ) > 0 {
403
- // at the moment, only one-level (non-nested) tag is supported, aka only the last level is used as active tag for next diff
404
- tagStack = tagStack [:len (tagStack )- 1 ]
405
400
// close all open tags
406
401
for i := len (tagStack ) - 1 ; i >= 0 ; i -- {
407
402
tagToClose := tagStack [i ]
@@ -416,13 +411,11 @@ func (hcd *HighlightCodeDiff) recoverOneDiff(lastActiveTag string, diff *diffmat
416
411
}
417
412
418
413
diff .Text = sb .String ()
419
- return activeTag
420
414
}
421
415
422
416
func (hcd * HighlightCodeDiff ) recoverFromPlaceholders (diffs []diffmatchpatch.Diff ) {
423
- var lastActiveTag string
424
417
for i := range diffs {
425
- lastActiveTag = hcd .recoverOneDiff (lastActiveTag , & diffs [i ])
418
+ hcd .recoverOneDiff (& diffs [i ])
426
419
}
427
420
}
428
421
0 commit comments