Skip to content

Commit dc74baf

Browse files
committed
fine tune comments
1 parent 90c6440 commit dc74baf

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

services/gitdiff/highlightdiff.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/sergi/go-diff/diffmatchpatch"
1313
)
1414

15+
// token is a html tag or entity, eg: "<span ...>", "</span>", "&lt;"
1516
func extractHTMLToken(s string) (before, token, after string, valid bool) {
1617
for pos1 := 0; pos1 < len(s); pos1++ {
1718
if s[pos1] == '<' {
@@ -33,9 +34,9 @@ func extractHTMLToken(s string) (before, token, after string, valid bool) {
3334

3435
// highlightCodeDiff is used to do diff with highlighted HTML code.
3536
// 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"
3738
// 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.
3940
// It's guaranteed that the tags in final diff result are paired correctly.
4041
type highlightCodeDiff struct {
4142
placeholderBegin rune
@@ -105,6 +106,7 @@ func (hcd *highlightCodeDiff) diffWithHighlight(filename, language, codeA, codeB
105106
return diffs
106107
}
107108

109+
// convertToPlaceholders totally depends on Chroma's valid HTML output and its structure, do not use these functions for other purposes.
108110
func (hcd *highlightCodeDiff) convertToPlaceholders(htmlCode string) string {
109111
var tagStack []string
110112
res := strings.Builder{}
@@ -120,7 +122,7 @@ func (hcd *highlightCodeDiff) convertToPlaceholders(htmlCode string) string {
120122
if !valid || token == "" {
121123
break
122124
}
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
124126
res.WriteString(beforeToken)
125127

126128
// the line wrapper tags should be removed before diff
@@ -149,7 +151,7 @@ func (hcd *highlightCodeDiff) convertToPlaceholders(htmlCode string) string {
149151
tokenInMap = token
150152
}
151153

152-
// remember the placeholder and tag in the map
154+
// remember the placeholder and token in the map
153155
placeholder, ok := hcd.tokenPlaceholderMap[tokenInMap]
154156
if !ok {
155157
placeholder = hcd.nextPlaceholder()
@@ -160,7 +162,7 @@ func (hcd *highlightCodeDiff) convertToPlaceholders(htmlCode string) string {
160162
}
161163

162164
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
164166
} else {
165167
// unfortunately, all private use runes has been exhausted, no more placeholder could be used, no more converting
166168
// 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) {
198200
} else if token[0] == '<' {
199201
tokenToRecover = token
200202
tagStack = append(tagStack, token)
201-
} else {
203+
} else { // html entity
202204
tokenToRecover = token
203205
}
204206
sb.WriteString(tokenToRecover)

0 commit comments

Comments
 (0)