Created
November 22, 2019 03:57
-
-
Save kemokemo/13fa468fb358666f5db90e25fb1594be to your computer and use it in GitHub Desktop.
html diff sample using "documize/html-diff"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
htmldiff "github.com/documize/html-diff" | |
) | |
var ( | |
previousHTML = `<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | |
<title>Document</title> | |
</head> | |
<body> | |
<b>hoge</b> | |
foo foo | |
</body> | |
</html>` | |
latestHTML = `<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | |
<title>Document</title> | |
</head> | |
<body> | |
hoge | |
foo bar | |
</body> | |
</html>` | |
) | |
func main() { | |
var cfg = &htmldiff.Config{ | |
Granularity: 5, | |
InsertedSpan: []htmldiff.Attribute{{Key: "style", Val: "background-color: palegreen;"}}, | |
DeletedSpan: []htmldiff.Attribute{{Key: "style", Val: "background-color: lightpink;"}}, | |
ReplacedSpan: []htmldiff.Attribute{{Key: "style", Val: "background-color: lightskyblue;"}}, | |
CleanTags: []string{""}, | |
} | |
res, err := cfg.HTMLdiff([]string{previousHTML, latestHTML}) | |
if err != nil { | |
log.Println("failed to HTMLdiff", err) | |
return | |
} | |
mergedHTML := res[0] | |
fmt.Println(mergedHTML) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment