Skip to content

Commit

Permalink
basic frontmatter handling, youtube embed, root
Browse files Browse the repository at this point in the history
  • Loading branch information
jackharrhy committed Aug 28, 2023
1 parent c8f3edf commit fc09e37
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 31 deletions.
2 changes: 1 addition & 1 deletion example-content/Creation of Repo.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
+++
catagories = ["Events"]
categories = ["Events"]
date = "2023-08-23"
+++

Expand Down
2 changes: 1 addition & 1 deletion example-content/Fogo.sh.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
+++
catagories = ["Groups"]
categories = ["Groups"]
+++

The group / GitHub organization around [[Almanac]].
2 changes: 1 addition & 1 deletion example-content/Jack.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
+++
catagories = ["Contributors"]
categories = ["Contributors"]
+++
2 changes: 1 addition & 1 deletion example-content/Opening of Fogo Island Inn.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
+++
catagories = ["Events"]
categories = ["Events"]
date = "2023-06-25"
+++

Expand Down
2 changes: 1 addition & 1 deletion example-content/POBBLES - We Wish You.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
+++
catagories = ["Videos"]
categories = ["Videos"]
youtube_id = "i5YDcT6db7g"
+++
2 changes: 1 addition & 1 deletion example-content/Riley.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
+++
catagories = ["Contributors"]
categories = ["Contributors"]
+++
20 changes: 20 additions & 0 deletions pkg/content/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,23 @@ func AllPageTitles(pages map[string]*Page) []string {

return allPageTitles
}

func FindRootPage(pages map[string]*Page) (*Page, error) {
var rootPage *Page

for _, page := range pages {
if page.Meta.Root {
if rootPage != nil {
return &Page{}, fmt.Errorf("multiple root pages found")
}

rootPage = page
}
}

if rootPage == nil {
return &Page{}, fmt.Errorf("no root page found")
}

return rootPage, nil
}
32 changes: 29 additions & 3 deletions pkg/content/parsing.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,27 @@ import (
"strings"

"github.com/yuin/goldmark"
"github.com/yuin/goldmark/parser"
"go.abhg.dev/goldmark/frontmatter"
"go.abhg.dev/goldmark/wikilink"

"github.com/fogo-sh/almanac/pkg/utils"
)

type PageMeta struct {
Categories []string `toml:"categories"`
Date string `toml:"date"`
Redirect string `toml:"redirect"`
Root bool `toml:"root"`
YoutubeId string `toml:"youtube_id"`
}

type Page struct {
Title string
Path string
LinksTo []string
Backlinks []string
Meta PageMeta
ParsedContent []byte
}

Expand All @@ -42,8 +52,7 @@ func ParsePageFile(path string) (Page, error) {

var linksTo = make([]string, 0)

var buf bytes.Buffer
if err := goldmark.New(goldmark.WithExtensions(
md := goldmark.New(goldmark.WithExtensions(
&frontmatter.Extender{},
&wikilink.Extender{
Resolver: WikiLinkResolver{
Expand All @@ -53,16 +62,33 @@ func ParsePageFile(path string) (Page, error) {
},
},
},
)).Convert(content, &buf); err != nil {
))

ctx := parser.NewContext()

var buf bytes.Buffer
err = md.Convert(content, &buf, parser.WithContext(ctx))
if err != nil {
return Page{}, fmt.Errorf("failed to parse markdown: %w", err)
}

var pageMeta PageMeta

data := frontmatter.Get(ctx)

if data != nil {
if err := data.Decode(&pageMeta); err != nil {
return Page{}, fmt.Errorf("failed to decode frontmatter: %w", err)
}
}

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

return Page{
Title: pageTitle,
LinksTo: linksTo,
Path: path,
Meta: pageMeta,
ParsedContent: buf.Bytes(),
}, nil
}
25 changes: 19 additions & 6 deletions pkg/devserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ func (r *Renderer) Render(w io.Writer, name string, data interface{}, c echo.Con

func serveNotFound(c echo.Context) error {
return c.Render(http.StatusNotFound, "page", templates.PageTemplateData{
Title: "Not found!",
Content: template.HTML("<p>Looks like this page doesn't exist yet</p>"),
Page: &content.Page{
Title: "Not Found",
},
})
}

Expand All @@ -62,19 +64,29 @@ func (s *Server) servePage(c echo.Context) error {
return fmt.Errorf("error discovering pages: %w", err)
}

page, ok := pages[pageKey]
var page *content.Page

if !ok {
return serveNotFound(c)
if pageKey == "" {
page, err = content.FindRootPage(pages)

if err != nil {
return serveNotFound(c)
}
} else {
var ok bool
page, ok = pages[pageKey]

if !ok {
return serveNotFound(c)
}
}

allPageTitles := content.AllPageTitles(pages)

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

Expand Down Expand Up @@ -108,6 +120,7 @@ func NewServer(config Config) *Server {
}

echoInst.GET("/:page", server.servePage)
echoInst.GET("/", server.servePage)

echoInst.GET("*", serveNotFound)

Expand Down
3 changes: 1 addition & 2 deletions pkg/templates/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ func OutputAllPagesToDisk(pages map[string]*content.Page, outputDir string) erro

err = PageTemplate.Execute(w, PageTemplateData{
AllPageTitles: allPageTitles,
Title: page.Title,
Page: page,
Content: template.HTML(string(page.ParsedContent)),
Backlinks: page.Backlinks,
})
if err != nil {
return fmt.Errorf("failed to execute template: %w", err)
Expand Down
43 changes: 29 additions & 14 deletions pkg/templates/page.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package templates

import "html/template"
import (
"html/template"

"github.com/fogo-sh/almanac/pkg/content"
)

type PageTemplateData struct {
AllPageTitles []string
Title string
Page *content.Page
Content template.HTML
Backlinks []string
}

var pageTemplateContent = `<!DOCTYPE html>
<html>
<head>
<title>{{ .Title }}</title>
<title>{{ .Page.Title }}</title>
<link rel="stylesheet" href="/assets/css/main.css">
<link rel="icon" type="image/svg+xml" href="/favicon.svg">
</head>
Expand All @@ -25,17 +28,29 @@ var pageTemplateContent = `<!DOCTYPE html>
</ul>
</nav>
<main>
<h1>{{ .Title }}</h1>
<h1>{{ .Page.Title }}</h1>
{{ if .Page.Meta.YoutubeId }}
<iframe
width="100%"
height="600px"
src="https://www.youtube.com/embed/{{ .Page.Meta.YoutubeId }}"
frameborder="0"
allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen></iframe>
{{ end }}
{{ .Content }}
{{ if .Backlinks }}
<section>
<h2>Backlinks</h2>
<ul>
{{ range .Backlinks }}
<li><a href="/{{ . }}">{{ . }}</a></li>
{{ end }}
</ul>
</section>
{{ if .Page.Backlinks }}
<section>
<h2>Backlinks</h2>
<ul>
{{ range .Page.Backlinks }}
<li><a href="/{{ . }}">{{ . }}</a></li>
{{ end }}
</ul>
</section>
{{ end }}
</main>
</body>
Expand Down

0 comments on commit fc09e37

Please sign in to comment.