Merge pull request 'dynamic-pages' (#1) from dynamic-pages into main

Reviewed-on: https://git.tcp.direct/s4d/tcp-wiki/pulls/1
This commit is contained in:
sad 2024-02-10 02:53:40 +00:00
commit 147ce945c3
5 changed files with 89 additions and 38 deletions

View File

@ -57,7 +57,7 @@ change `main` to your specific repo's branch and you should be good to go!
## TODO
- [ ] MANY FUCKING THINGS
- [ ] config files
- [ ] Webhook support for auto pull on push/update of the git repo
- [x] Git Branch support
- [ ] add a star/upvote/like feature for pages
@ -75,10 +75,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???
- [x] acual working pages!?
- [ ] image support

View File

@ -40,6 +40,7 @@
</div>
{{ end }}
</div>
</main>
<form method="POST" action="/submit_comment">
<input type="hidden" name="path" value="{{ .Path }}">
@ -53,5 +54,13 @@
</div>
<button type="submit">Submit</button>
</form>
<div class="sidebar">
<h2>Pages</h2>
<ul>
{{ range .Pages }}
<li><a href="/{{ . }}">{{ . }}</a></li>
{{ end }}
</ul>
</div>
</body>
</html>

View File

@ -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;
}

View File

@ -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)
markdownFiles, err := listMarkdownFiles(localPath)
if err != nil {
log.Printf("Comment loading? %q", commentsDB.Path())
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
}

View File

@ -24,20 +24,19 @@ 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 {
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
renderMarkdown(w, r, content, commentsDB, localPath, filePath, pages) // Now correctly includes `pages`
case ".html", ".css":
renderStatic(w, content, ext)
default:
@ -46,7 +45,9 @@ func renderPage(w http.ResponseWriter, r *http.Request, localPath, filePath stri
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,8 +70,10 @@ func renderMarkdown(w http.ResponseWriter, r *http.Request, content []byte, comm
return
}
layout, err := ioutil.ReadFile(filepath.Join(localPath, "assets/_layout.html"))
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
}
@ -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 {