From 5656684589000b276b29d48c2da175c43765aec1 Mon Sep 17 00:00:00 2001 From: sad Date: Fri, 9 Feb 2024 19:27:04 -0700 Subject: [PATCH] implement dynamic pages --- README.md | 9 ++++---- assets/_layout.html | 11 +++++++++- assets/main.css | 14 ++++++++++++- src/main.go | 42 ++++++++++++++++++++++++++++++------- src/render.go | 50 ++++++++++++++++++++++++--------------------- 5 files changed, 89 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index dae3666..0666a8e 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,7 @@ change `main` to your specific repo's branch and you should be good to go! ## TODO +- [ ] config files - [ ] MANY FUCKING THINGS - [ ] Webhook support for auto pull on push/update of the git repo - [x] Git Branch support @@ -75,10 +76,8 @@ change `main` to your specific repo's branch and you should be good to go! - [ ] reply to replies - [ ] set security controls per page - [ ] auto refresh on post -- [ ] dynamically generated links for all avaiable pages +- [x] dynamically generated links for all avaiable pages - [ ] sitemap - [ ] anti robot shit here - - [ ] acual working pages!? -- [ ] post quantum intergration and verification -- [ ] BUILD UP THAT MARKDOWN SUPPORT -- [ ] fix whatever i did to fuck up design/layout/css??? \ No newline at end of file + - [x] acual working pages!? + - [ ] image support diff --git a/assets/_layout.html b/assets/_layout.html index 645863f..5985075 100644 --- a/assets/_layout.html +++ b/assets/_layout.html @@ -39,7 +39,8 @@

{{ .Author }} at {{ .Date }}

{{ end }} - + +
@@ -53,5 +54,13 @@
+ diff --git a/assets/main.css b/assets/main.css index 16c05e1..4b24ecf 100644 --- a/assets/main.css +++ b/assets/main.css @@ -99,7 +99,6 @@ section h2 { color: #999; } -/* Added styles */ .page-info { font-size: 0.8em; display: flex; @@ -118,3 +117,16 @@ section h2 { background-color: #555; margin-left: 5px; } + +.sidebar-toggle button { + background-color: #333; + color: #14ee00; + border: 1px solid #14ee00; + padding: 5px 10px; + cursor: pointer; +} +.sidebar-toggle button:hover { + background-color: #14ee00; + color: #131313; +} + diff --git a/src/main.go b/src/main.go index b63639a..7f3bfe8 100644 --- a/src/main.go +++ b/src/main.go @@ -5,6 +5,7 @@ import ( "fmt" "log" "net/http" + "path/filepath" "os" "os/signal" "strings" @@ -33,6 +34,8 @@ func main() { } defer commentsDB.Close() + fs := http.FileServer(http.Dir("./assets")) + http.Handle("/assets/", http.StripPrefix("/assets/", fs)) http.HandleFunc("/", handler) http.HandleFunc("/submit_comment", submitCommentHandler) @@ -61,12 +64,10 @@ func main() { } func handler(w http.ResponseWriter, r *http.Request) { - //for debugging + // For debugging log.Printf("LOCAL PATH: %q", localPath) - //... - - if r.URL.Path == "assets/favicon.ico" { + if r.URL.Path == "./assets/favicon.ico" { return } @@ -85,10 +86,37 @@ func handler(w http.ResponseWriter, r *http.Request) { csp := "default-src 'self'; img-src 'self'; script-src 'self'; style-src 'self';" w.Header().Set("Content-Security-Policy", csp) - err = renderPage(w, r, localPath, filePath, commentsDB) - if err != nil { - log.Printf("Comment loading? %q", commentsDB.Path()) + markdownFiles, err := listMarkdownFiles(localPath) + if err != nil { + log.Printf("Error listing markdown files: %v", err) + http.Error(w, "Internal Server Error", http.StatusInternalServerError) + return + } + err = renderPage(w, r, localPath, filePath, commentsDB, markdownFiles) + if err != nil { + log.Printf("Failed to render page: %v", err) http.Error(w, "File not found", http.StatusNotFound) } } + + +func listMarkdownFiles(localPath string) ([]string, error) { + var files []string + err := filepath.Walk(localPath, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + if !info.IsDir() && strings.HasSuffix(path, ".md") { + relPath, err := filepath.Rel(localPath, path) + if err != nil { + return err + } + // Ensure the path uses web-friendly slashes + relPath = strings.Replace(relPath, string(os.PathSeparator), "/", -1) + files = append(files, relPath) + } + return nil + }) + return files, err +} diff --git a/src/render.go b/src/render.go index f71f27c..e840730 100644 --- a/src/render.go +++ b/src/render.go @@ -24,29 +24,30 @@ type Page struct { AuthoredDate time.Time LastModifier string LastModifiedDate time.Time + Pages []string } -func renderPage(w http.ResponseWriter, r *http.Request, localPath, filePath string, commentsDB *bitcask.Bitcask) error { - content, err := readFileFromRepo(localPath, filePath) - if err != nil { - return err - } +func renderPage(w http.ResponseWriter, r *http.Request, localPath, filePath string, commentsDB *bitcask.Bitcask, pages []string) error { + content, err := readFileFromRepo(localPath, filePath) + if err != nil { + return err + } - //log.Printf("Read file content: %s", content) - - ext := filepath.Ext(filePath) - switch ext { - case ".md": - renderMarkdown(w, r, content, commentsDB, localPath, filePath) // Updated the call to include localPath and filePath - case ".html", ".css": - renderStatic(w, content, ext) - default: - return fmt.Errorf("unsupported file format") - } - return nil + ext := filepath.Ext(filePath) + switch ext { + case ".md": + renderMarkdown(w, r, content, commentsDB, localPath, filePath, pages) // Now correctly includes `pages` + case ".html", ".css": + renderStatic(w, content, ext) + default: + return fmt.Errorf("unsupported file format") + } + return nil } -func renderMarkdown(w http.ResponseWriter, r *http.Request, content []byte, commentsDB *bitcask.Bitcask, localPath, filePath string) { + +func renderMarkdown(w http.ResponseWriter, r *http.Request, content []byte, commentsDB *bitcask.Bitcask, localPath, filePath string, pages []string) { + md := goldmark.New( goldmark.WithExtensions( extension.GFM, // GitHub Flavored Markdown @@ -69,11 +70,13 @@ func renderMarkdown(w http.ResponseWriter, r *http.Request, content []byte, comm return } - layout, err := ioutil.ReadFile(filepath.Join(localPath, "assets/_layout.html")) - if err != nil { - http.Error(w, "Layout not found", http.StatusInternalServerError) - return - } + + layout, err := ioutil.ReadFile("assets/_layout.html") + if err != nil { + log.Printf("Error reading _layout.html: %v", err) + http.Error(w, "Layout not found", http.StatusInternalServerError) + return + } comments, err := getComments(commentsDB, r.URL.Path) if err != nil && err != bitcask.ErrKeyNotFound { @@ -89,6 +92,7 @@ func renderMarkdown(w http.ResponseWriter, r *http.Request, content []byte, comm AuthoredDate: authoredDate, LastModifier: lastModifier, LastModifiedDate: lastModifiedDate, + Pages: pages, } t, err := template.New("layout").Parse(string(layout)) if err != nil {