@@ -12,6 +12,7 @@ import (
12
12
"github.com/sergi/go-diff/diffmatchpatch"
13
13
)
14
14
15
+ // token is a html tag or entity, eg: "<span ...>", "</span>", "<"
15
16
func extractHTMLToken (s string ) (before , token , after string , valid bool ) {
16
17
for pos1 := 0 ; pos1 < len (s ); pos1 ++ {
17
18
if s [pos1 ] == '<' {
@@ -33,9 +34,9 @@ func extractHTMLToken(s string) (before, token, after string, valid bool) {
33
34
34
35
// highlightCodeDiff is used to do diff with highlighted HTML code.
35
36
// It totally depends on Chroma's valid HTML output and its structure, do not use these functions for other purposes.
36
- // The HTML tags will be replaced by Unicode placeholders: "<span>{TEXT}</span>" => "\uE000{TEXT}\uE001"
37
+ // The HTML tags and entities will be replaced by Unicode placeholders: "<span>{TEXT}</span>" => "\uE000{TEXT}\uE001"
37
38
// These Unicode placeholders are friendly to the diff.
38
- // Then after diff, the placeholders in diff result will be recovered to the HTML tags.
39
+ // Then after diff, the placeholders in diff result will be recovered to the HTML tags and entities .
39
40
// It's guaranteed that the tags in final diff result are paired correctly.
40
41
type highlightCodeDiff struct {
41
42
placeholderBegin rune
@@ -105,6 +106,7 @@ func (hcd *highlightCodeDiff) diffWithHighlight(filename, language, codeA, codeB
105
106
return diffs
106
107
}
107
108
109
+ // convertToPlaceholders totally depends on Chroma's valid HTML output and its structure, do not use these functions for other purposes.
108
110
func (hcd * highlightCodeDiff ) convertToPlaceholders (htmlCode string ) string {
109
111
var tagStack []string
110
112
res := strings.Builder {}
@@ -120,7 +122,7 @@ func (hcd *highlightCodeDiff) convertToPlaceholders(htmlCode string) string {
120
122
if ! valid || token == "" {
121
123
break
122
124
}
123
- // write the content before the tag into result string, and consume the tag in the string
125
+ // write the content before the token into result string, and consume the token in the string
124
126
res .WriteString (beforeToken )
125
127
126
128
// the line wrapper tags should be removed before diff
@@ -149,7 +151,7 @@ func (hcd *highlightCodeDiff) convertToPlaceholders(htmlCode string) string {
149
151
tokenInMap = token
150
152
}
151
153
152
- // remember the placeholder and tag in the map
154
+ // remember the placeholder and token in the map
153
155
placeholder , ok := hcd .tokenPlaceholderMap [tokenInMap ]
154
156
if ! ok {
155
157
placeholder = hcd .nextPlaceholder ()
@@ -160,7 +162,7 @@ func (hcd *highlightCodeDiff) convertToPlaceholders(htmlCode string) string {
160
162
}
161
163
162
164
if placeholder != 0 {
163
- res .WriteRune (placeholder ) // use the placeholder to replace the tag
165
+ res .WriteRune (placeholder ) // use the placeholder to replace the token
164
166
} else {
165
167
// unfortunately, all private use runes has been exhausted, no more placeholder could be used, no more converting
166
168
// usually, the exhausting won't occur in real cases, the magnitude of used placeholders is not larger than that of the CSS classes outputted by chroma.
@@ -198,7 +200,7 @@ func (hcd *highlightCodeDiff) recoverOneDiff(diff *diffmatchpatch.Diff) {
198
200
} else if token [0 ] == '<' {
199
201
tokenToRecover = token
200
202
tagStack = append (tagStack , token )
201
- } else {
203
+ } else { // html entity
202
204
tokenToRecover = token
203
205
}
204
206
sb .WriteString (tokenToRecover )
0 commit comments