Skip to content

Commit

Permalink
Merge pull request #7 from adxy/404
Browse files Browse the repository at this point in the history
feat: add support for custom 404 page
  • Loading branch information
barelyhuman authored Sep 14, 2023
2 parents 2fcf995 + 56848b7 commit 90f6798
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
1 change: 1 addition & 0 deletions docs/pages/01-basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Yeah... and no.
- `_head.html` - will add the header section to the final HTML (deprecated in v0.2.7)
- `_tail.html` - will add the footer section to the final HTML (deprecated in v0.2.7)
- `_layout.html` - defines a common layout for all files that'll be rendered.
- `_404.html` - alvu will serve this file whenever the requested page is not found. (Nested within `_layout.html`, if exists)

The `_head.html` and `_tail.html` files were used as placeholders for
repeated layout across your markdown files, this has now been replaced
Expand Down
7 changes: 7 additions & 0 deletions docs/pages/404.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<div>
<h1>404</h1>
<p>You seem lost...</p>
<p>
<a href="{{.Meta.BaseURL}}"> &larr; Go Back? </a>
</p>
</div>
32 changes: 28 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,12 @@ var hardWraps bool
var hookCollection HookCollection
var reloadCh = []chan bool{}
var serveFlag *bool
var notFoundPageExists bool

//go:embed .commitlog.release
var release string

var reservedFiles []string = []string{"_head.html", "_tail.html", "_layout.html"}
var layoutFiles []string = []string{"_head.html", "_tail.html", "_layout.html"}

type SiteMeta struct {
BaseURL string
Expand Down Expand Up @@ -173,6 +174,7 @@ func main() {
headFilePath := path.Join(pagesPath, "_head.html")
baseFilePath := path.Join(pagesPath, "_layout.html")
tailFilePath := path.Join(pagesPath, "_tail.html")
notFoundFilePath := path.Join(pagesPath, "404.html")
outPath = path.Join(*outPathFlag)
hooksPath := path.Join(*basePathFlag, *hooksPathFlag)
hardWraps = *hardWrapsFlag
Expand Down Expand Up @@ -230,6 +232,17 @@ func main() {
fmt.Println(headTailDeprecationWarning.String())
}

onDebug(func() {
debugInfo("Checking if _404.html exists")
memuse()
})
if _, err := os.Stat(notFoundFilePath); errors.Is(err, os.ErrNotExist) {
notFoundPageExists = false
log.Println("no _404.html found, skipping")
} else {
notFoundPageExists = true
}

alvuApp.CopyPublic()

onDebug(func() {
Expand Down Expand Up @@ -337,7 +350,7 @@ func CollectFilesToProcess(basepath string) []string {
for _, pathInfo := range pathstoprocess {
_path := path.Join(basepath, pathInfo.Name())

if Contains(reservedFiles, pathInfo.Name()) {
if Contains(layoutFiles, pathInfo.Name()) {
continue
}

Expand Down Expand Up @@ -876,8 +889,19 @@ func normalizeFilePath(path string) string {
}

func notFoundHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
fmt.Fprint(w, "404, Page not found....")
if notFoundPageExists {
compiledNotFoundFile := filepath.Join(outPath, "404.html")
notFoundFile, err := os.ReadFile(compiledNotFoundFile)
if err != nil {
http.Error(w, "404, Page not found....", http.StatusNotFound)
return
}
w.WriteHeader(http.StatusNotFound)
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write(notFoundFile)
return
}
http.Error(w, "404, Page not found....", http.StatusNotFound)
}

func Contains(collection []string, item string) bool {
Expand Down

0 comments on commit 90f6798

Please sign in to comment.