Skip to content

Commit

Permalink
Merge pull request #3 from fogo-sh/content-listing
Browse files Browse the repository at this point in the history
page listing, discover all pages instead of just 1
  • Loading branch information
jackharrhy authored Aug 27, 2023
2 parents 7d9bb35 + c7585b3 commit aebf5a4
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 25 deletions.
41 changes: 41 additions & 0 deletions pkg/content/discover.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package content

import (
"fmt"
"path/filepath"
"sort"
)

func DiscoverPages(path string) (map[string]Page, error) {
paths, error := filepath.Glob(filepath.Join(path, "*.md"))

if error != nil {
return nil, fmt.Errorf("failed to glob files: %w", error)
}

pages := make(map[string]Page)

for _, path := range paths {
page, error := ParsePageFile(path)
if error != nil {
return nil, fmt.Errorf("failed to parse page: %w", error)
}

pages[page.Title] = page
}

return pages, nil
}

func AllPageTitles(pages map[string]Page) []string {
allPageTitles := make([]string, 0, len(pages))
for key := range pages {
allPageTitles = append(allPageTitles, key)
}

sort.Slice(allPageTitles, func(i, j int) bool {
return allPageTitles[i] < allPageTitles[j]
})

return allPageTitles
}
20 changes: 13 additions & 7 deletions pkg/content/parsing.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"fmt"
"os"
"path/filepath"
"strings"

"github.com/yuin/goldmark"
"go.abhg.dev/goldmark/frontmatter"
Expand All @@ -12,27 +14,28 @@ import (
"github.com/fogo-sh/almanac/pkg/utils"
)

type File struct {
type Page struct {
Title string
Path string
ParsedContent []byte
}

func ParseFile(path string) (File, error) {
func ParsePageFile(path string) (Page, error) {
f, err := os.Open(path)
if err != nil {
return File{}, fmt.Errorf("failed to open file: %w", err)
return Page{}, fmt.Errorf("failed to open file: %w", err)
}
defer utils.DeferredClose(f)

stat, err := f.Stat()
if err != nil {
return File{}, fmt.Errorf("failed to stat file: %w", err)
return Page{}, fmt.Errorf("failed to stat file: %w", err)
}

content := make([]byte, stat.Size())
_, err = f.Read(content)
if err != nil {
return File{}, fmt.Errorf("failed to read file: %w", err)
return Page{}, fmt.Errorf("failed to read file: %w", err)
}

var buf bytes.Buffer
Expand All @@ -42,10 +45,13 @@ func ParseFile(path string) (File, error) {
Resolver: WikiLinkResolver{},
},
)).Convert(content, &buf); err != nil {
return File{}, fmt.Errorf("failed to parse markdown: %w", err)
return Page{}, fmt.Errorf("failed to parse markdown: %w", err)
}

return File{
pageTitle := strings.TrimSuffix(filepath.Base(path), filepath.Ext(path))

return Page{
Title: pageTitle,
Path: path,
ParsedContent: buf.Bytes(),
}, nil
Expand Down
27 changes: 13 additions & 14 deletions pkg/devserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"io"
"log/slog"
"net/http"
"os"
"path"

"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
Expand Down Expand Up @@ -56,25 +54,26 @@ func serveNotFound(c echo.Context) error {
}

func (s *Server) servePage(c echo.Context) error {
page := c.Param("page")
contentPath := path.Join(s.config.ContentDir, page+".md")
pageKey := c.Param("page")

pages, err := content.DiscoverPages(s.config.ContentDir)

_, err := os.Stat(contentPath)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return serveNotFound(c)
}
return fmt.Errorf("error checking file: %w", err)
return fmt.Errorf("error discovering pages: %w", err)
}

file, err := content.ParseFile(contentPath)
if err != nil {
return fmt.Errorf("error processing file: %w", err)
page, ok := pages[pageKey]

if !ok {
return serveNotFound(c)
}

allPageTitles := content.AllPageTitles(pages)

return c.Render(http.StatusOK, "page", templates.PageTemplateData{
Title: page,
Content: template.HTML(string(file.ParsedContent)),
AllPageTitles: allPageTitles,
Title: page.Title,
Content: template.HTML(string(page.ParsedContent)),
})
}

Expand Down
13 changes: 13 additions & 0 deletions pkg/static/static/assets/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,23 @@
}

body {
max-width: 80rem;
margin: 0 auto;
display: flex;
gap: 1rem;
background-color: var(--bg);
padding: 1rem;
}

body > :first-child {
margin-top: 0;
}

nav {
padding-top: 4rem;
width: 20rem;
}

main {
width: 100%;
}
18 changes: 14 additions & 4 deletions pkg/templates/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package templates
import "html/template"

type PageTemplateData struct {
Title string
Content template.HTML
AllPageTitles []string
Title string
Content template.HTML
}

var pageTemplateContent = `<!DOCTYPE html>
Expand All @@ -14,8 +15,17 @@ var pageTemplateContent = `<!DOCTYPE html>
<link rel="stylesheet" href="/assets/css/main.css">
</head>
<body>
<h1>{{ .Title }}</h1>
{{ .Content }}
<nav>
<ul>
{{ range .AllPageTitles }}
<li><a href="/{{ . }}">{{ . }}</a></li>
{{ end }}
</ul>
</nav>
<main>
<h1>{{ .Title }}</h1>
{{ .Content }}
</main>
</body>
</html>`

Expand Down

0 comments on commit aebf5a4

Please sign in to comment.