server rendering markdown with live reload on file update working

This commit is contained in:
arnaucube
2019-05-07 21:07:45 +02:00
parent 6e3b9cbf68
commit a0b3d071e5
7 changed files with 302 additions and 0 deletions

76
template.go Normal file
View File

@@ -0,0 +1,76 @@
package main
// html templates are here instead of an .html file to avoid depending on external files
// in this way, everything is inside the binary
const dirTemplate = `
<!DOCTYPE html>
<html>
<title>{{.Title}}</title>
<style>
body {
font-family: Arial, Helvetica, sans-serif;
background:#000000;
color:#cccccc;
}
</style>
<body>
{{.Content}}
</body>
</html>
`
const htmlTemplate = `
<!DOCTYPE html>
<html>
<title>{{.Title}}</title>
<style>
body {
font-family: Arial, Helvetica, sans-serif;
background:#000000;
color:#cccccc;
}
pre, code{
padding-left: 3px;
padding-right: 3px;
background: #333333;
border-radius: 3px;
}
pre>code {
color: #c0fffa;
}
h1:after{
content:' ';
display:block;
border:1px solid #cccccc;
}
h2:after{
content:' ';
display:block;
border:0.7px solid #cccccc;
}
</style>
<body>
{{.Content}}
<script>
(function() {
var conn = new WebSocket("ws://127.0.0.1:8080/ws/{{.Title}}");
conn.onclose = function(evt) {
console.log('Connection closed');
alert('Connection closed');
}
conn.onmessage = function(evt) {
console.log('file updated');
// location.reload();
console.log("evt", evt);
document.getElementsByTagName("BODY")[0].innerHTML = evt.data;
}
})();
</script>
</body>
</html>
`