add a simple layout html wrapper for the markdown pages with css support

This commit is contained in:
legitnull 2023-04-02 12:55:48 -06:00
parent db5ff4650a
commit 944cf9fb8b
3 changed files with 95 additions and 1 deletions

24
assets/_layout.html Normal file
View File

@ -0,0 +1,24 @@
<!-- _layout.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>TCP Wiki</title>
<link rel="stylesheet" href="/assets/main.css">
</head>
<body>
<header>
<h1>TCP Wiki</h1>
<nav>
<ul>
<li><a href="/">Home</a></li>
<!-- placeholder -->
</ul>
</nav>
</header>
<div id="content">
{{.Content}}
</div>
</body>
</html>

42
assets/main.css Normal file
View File

@ -0,0 +1,42 @@
/* assets/main.css */
/* placeholder for better sytle */
body {
font-family: Arial, sans-serif;
line-height: 1.6;
margin: 20px;
}
h1, h2, h3, h4, h5, h6 {
font-weight: bold;
}
a {
color: #333;
text-decoration: none;
}
a:hover {
color: #777;
}
header {
padding: 1em;
background-color: #f1f1f1;
border-bottom: 1px solid #ccc;
}
header h1 {
display: inline-block;
margin-right: 1em;
}
nav ul {
list-style: none;
padding: 0;
}
nav ul li {
display: inline;
margin-right: 1em;
}

View File

@ -1,13 +1,20 @@
package main
import (
"bytes"
"fmt"
"html/template"
"io/ioutil"
"net/http"
"path/filepath"
"github.com/russross/blackfriday/v2"
)
type Page struct {
Content template.HTML
}
func renderPage(w http.ResponseWriter, localPath, filePath string) error {
content, err := readFileFromRepo(localPath, filePath)
if err != nil {
@ -28,8 +35,29 @@ func renderPage(w http.ResponseWriter, localPath, filePath string) error {
func renderMarkdown(w http.ResponseWriter, content []byte) {
md := blackfriday.Run(content)
layout, err := ioutil.ReadFile(filepath.Join(localPath, "assets/_layout.html"))
if err != nil {
http.Error(w, "Layout not found", http.StatusInternalServerError)
return
}
page := &Page{Content: template.HTML(md)}
t, err := template.New("layout").Parse(string(layout))
if err != nil {
http.Error(w, "Error parsing layout", http.StatusInternalServerError)
return
}
var buf bytes.Buffer
err = t.Execute(&buf, page)
if err != nil {
http.Error(w, "Error rendering layout", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write(md)
w.Write(buf.Bytes())
}
func renderStatic(w http.ResponseWriter, content []byte, ext string) {