-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdiff.go
41 lines (34 loc) · 988 Bytes
/
diff.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package main
import (
"bytes"
"html"
"strings"
diffLib "github.com/sergi/go-diff/diffmatchpatch"
)
var replacer *strings.Replacer
func init() {
replacer = strings.NewReplacer("\r", "", "\n", "↲<br>")
}
func diffHTML(text1, text2 string) string {
dmp := diffLib.New()
diffs := dmp.DiffMain(text1, text2, false)
var buff bytes.Buffer
for _, diff := range diffs {
text := html.EscapeString(diff.Text)
switch diff.Type {
case diffLib.DiffInsert:
_, _ = buff.WriteString("<span class='webix_docmanager_diff_insert'>")
_, _ = buff.WriteString(replacer.Replace(text))
_, _ = buff.WriteString("</span>")
case diffLib.DiffDelete:
_, _ = buff.WriteString("<span class='webix_docmanager_diff_remove'>")
_, _ = buff.WriteString(replacer.Replace(text))
_, _ = buff.WriteString("</span>")
case diffLib.DiffEqual:
_, _ = buff.WriteString("<span>")
_, _ = buff.WriteString(text)
_, _ = buff.WriteString("</span>")
}
}
return buff.String()
}