Skip to content

Commit

Permalink
feat: access deep data merge in all layouts
Browse files Browse the repository at this point in the history
Co-authored-by: Aditya Hegde <aditya.mh@outlook.com>
  • Loading branch information
anirudhsudhir and bwaklog committed Apr 11, 2024
1 parent 24d5303 commit 8da851b
Show file tree
Hide file tree
Showing 14 changed files with 73 additions and 58 deletions.
18 changes: 8 additions & 10 deletions pkg/engine/anna_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import (
)

type TagRootTemplateData struct {
DeepDataMerge DeepDataMerge
PagePath template.URL
TagTemplateData parser.TemplateData
TagNames []string
DeepDataMerge DeepDataMerge
PageURL template.URL
TemplateData parser.TemplateData
TagNames []string
}

func (e *Engine) RenderTags(fileOutPath string, templ *template.Template) {
Expand All @@ -46,15 +46,14 @@ func (e *Engine) RenderTags(fileOutPath string, templ *template.Template) {
}

tagRootTemplataData := parser.TemplateData{
Layout: e.DeepDataMerge.LayoutConfig,
Frontmatter: parser.Frontmatter{Title: "Tags"},
}

tagTemplateData := TagRootTemplateData{
DeepDataMerge: e.DeepDataMerge,
PagePath: "tags.html",
TagTemplateData: tagRootTemplataData,
TagNames: tagNames,
DeepDataMerge: e.DeepDataMerge,
PageURL: "tags.html",
TemplateData: tagRootTemplataData,
TagNames: tagNames,
}

// Rendering the page displaying all tags
Expand All @@ -80,7 +79,6 @@ func (e *Engine) RenderTags(fileOutPath string, templ *template.Template) {
tagString, _ = strings.CutSuffix(tagString, ".html")

e.DeepDataMerge.Tags[tag] = parser.TemplateData{
Layout: e.DeepDataMerge.LayoutConfig,
Frontmatter: parser.Frontmatter{
Title: tagString,
},
Expand Down
12 changes: 6 additions & 6 deletions pkg/engine/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ func TestRenderPage(t *testing.T) {
Tags: []string{"blog", "thoughts"},
},
Body: template.HTML("<h1>Hello World</h1>"),
Layout: parser.LayoutConfig{
Navbar: []string{"index", "posts"},
BaseURL: "https://example.org",
SiteTitle: "Anna",
Author: "anna",
},
// Layout: parser.LayoutConfig{
// Navbar: []string{"index", "posts"},
// BaseURL: "https://example.org",
// SiteTitle: "Anna",
// Author: "anna",
// },
}

templ, err := template.ParseFiles(TestDirPath + "render_page/template_input.html")
Expand Down
9 changes: 5 additions & 4 deletions pkg/engine/user_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,21 @@ import (
)

type postsTemplateData struct {
Posts []parser.TemplateData
parser.TemplateData
DeepDataMerge DeepDataMerge
PageURL template.URL
TemplateData parser.TemplateData
}

func (e *Engine) RenderEngineGeneratedFiles(fileOutPath string, template *template.Template) {
// Rendering "posts.html"
var postsBuffer bytes.Buffer

postsData := postsTemplateData{
Posts: e.DeepDataMerge.Posts,
TemplateData: parser.TemplateData{
Frontmatter: parser.Frontmatter{Title: "Posts"},
Layout: e.DeepDataMerge.LayoutConfig,
},
DeepDataMerge: e.DeepDataMerge,
PageURL: "posts.html",
}
// e.DeepDataMerge.Templates["posts.html"] = parser.TemplateData{
// Frontmatter: parser.Frontmatter{Title: "Posts"},
Expand Down
2 changes: 0 additions & 2 deletions pkg/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ type TemplateData struct {
Date int64
Frontmatter Frontmatter
Body template.HTML
Layout LayoutConfig
LiveReload bool
}

Expand Down Expand Up @@ -132,7 +131,6 @@ func (p *Parser) AddFileAndRender(baseDirPath string, dirEntryPath string, front
Date: date,
Frontmatter: frontmatter,
Body: template.HTML(body),
Layout: p.LayoutConfig,
LiveReload: p.LiveReload,
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestAddFileandRender(t *testing.T) {
Date: want_parser.DateParse(sample_frontmatter.Date).Unix(),
Frontmatter: sample_frontmatter,
Body: template.HTML(sample_body),
Layout: want_layout,
// Layout: want_layout,
}
want_parser.LayoutConfig = want_layout

Expand Down
4 changes: 3 additions & 1 deletion site/content/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ To access the data for a particular page, use Go templating syntax:
{{$PageData.CompleteURL}}
```

To access the page data for `posts.html`, `tags.html` and partials, set {{$PageData := .TemplateData}}

All of the following page data fields can be accessed in the above manner:

- `{{$PageData.CompleteURL}}` : Returns the complete url of the given page
Expand Down Expand Up @@ -182,7 +184,7 @@ Detailed documentation for our SSG can be found: [here](https://anna-docs.netlif
If you have git installed, clone our repository and build against the latest commit

```sh
git clone github.com/acmpesuecc/anna; cd anna
git clone github.com/acmpesuecc/anna; cd anna
go build
```

Expand Down
8 changes: 4 additions & 4 deletions site/layout/page.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{{ define "page"}}
{{$PageData := index .DeepDataMerge.Templates .PageURL}}
{{ template "head" $PageData}}
{{ template "head" .}}

<body>

{{template "header" $PageData}}
{{template "header" .}}
<article>
<section class="body">
{{ if eq $PageData.Frontmatter.Type "post" }}
Expand Down Expand Up @@ -36,9 +36,9 @@ <h1>{{ $PageData.Frontmatter.Title }}</h1>
{{$PageData.Body}}
</section>
</article>
{{template "footer" $PageData}}
{{template "footer" .}}
</body>

</html>

{{ end}}
{{ end}}
32 changes: 18 additions & 14 deletions site/layout/partials/head.html
Original file line number Diff line number Diff line change
@@ -1,46 +1,50 @@
{{define "head"}}
{{ define "head" }}
<!-- If the template for a particular url does not exist(posts.html), setting it to the specific page template data -->
{{ $PageData := index .DeepDataMerge.Templates .PageURL }}
{{if eq $PageData nil }}
{{ $PageData = .TemplateData }}
{{end}}

<!doctype html>
<html lang="en">

<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>{{.Frontmatter.Title}}</title>
<title>{{$PageData.Frontmatter.Title}}</title>
<link rel="preload stylesheet" href="{{.DeepDataMerge.LayoutConfig.ThemeURL}}" />

<link rel="preload stylesheet" href="{{.Layout.ThemeURL}}" />
<!-- External Stylesheets and Plugins -->
<script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.9.0/build/highlight.min.js"></script>
<script defer src="https://unpkg.com/highlightjs-copy/dist/highlightjs-copy.min.js"></script>
<link rel="stylesheet" href="https://unpkg.com/highlightjs-copy/dist/highlightjs-copy.min.css" />
<link rel="alternate" type="application/atom+xml" title="feed" href="/feed.xml" />


{{ if .LiveReload }}
<!-- Scripts filled in from plugins -->
{{ if $PageData.LiveReload }}
<script>
const eventSource = new EventSource('http://localhost:8000/events');
eventSource.onmessage = function (event) {
location.reload()
};
window.onbeforeunload = function(event)
{
return confirm("Confirm refresh");
};
</script>
{{ end }}


{{range .Frontmatter.JSFiles}}
{{range $PageData.Frontmatter.JSFiles}}
<script src="/script/{{.}}" defer></script>
{{end}} {{range .Layout.SiteScripts}}
{{end}} {{range .DeepDataMerge.LayoutConfig.SiteScripts}}
<script src="/script/{{.}}" defer></script>
{{end}}


<meta property="og:type" content="website" />
<meta property="og:title" content="{{ .Frontmatter.Title }}" />
<meta property="og:description" content="{{ .Frontmatter.Description }}" />
<meta property="og:image" content="{{ .Frontmatter.PreviewImage }}" />
<meta property="og:title" content="{{ $PageData.Frontmatter.Title }}" />
<meta property="og:description" content="{{ $PageData.Frontmatter.Description }}" />
<meta property="og:image" content="{{ $PageData.Frontmatter.PreviewImage }}" />

<meta name="description" content="{{ .Frontmatter.Description }}">
<meta name="description" content="{{ $PageData.Frontmatter.Description }}">

<script defer>
hljs.highlightAll();
Expand Down
20 changes: 15 additions & 5 deletions site/layout/partials/header.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
{{define "header"}}
<!-- If the template for a particular url does not exist(posts.html), setting it to the specific page template data -->
{{ $PageData := index .DeepDataMerge.Templates .PageURL }}
{{if eq $PageData nil }}
{{ $PageData = .TemplateData }}
{{end}}

<header>
<nav>
{{range .Layout.Navbar}} {{ if eq . "index" }}
<a href="/">[{{ $.Layout.Author }}]</a>
{{else}}
<a href="/{{.}}.html">[{{.}}]</a>
{{end}} {{end}}
{{range .DeepDataMerge.LayoutConfig.Navbar}}
<!-- Replacing with author name for index page -->
{{ if eq . "index" }}
<a href="/">[{{ $.DeepDataMerge.LayoutConfig.Author }}]</a>
{{else}}
<!-- Other layout fields are filled here -->
<a href="/{{.}}.html">[{{.}}]</a>
{{end}}
{{end}}
</nav>
</header>
{{template "search" .}}
Expand Down
5 changes: 3 additions & 2 deletions site/layout/posts.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{{ define "posts"}}
{{$PageData := .TemplateData}}
{{ template "head" .}}

<body>
{{template "header" .}}
<div class="body">
<article>
<section class="posts">
{{range $PostDate, $Post := .Posts}}
{{range $PostDate, $Post := .DeepDataMerge.Posts}}
<a class="post-card" href="{{$Post.CompleteURL}}">
<div class="post-card-div">
<h3>{{$Post.Frontmatter.Title}}</h3>
Expand All @@ -24,4 +25,4 @@ <h3>{{$Post.Frontmatter.Title}}</h3>

</html>

{{ end}}
{{ end}}
6 changes: 3 additions & 3 deletions site/layout/tag-subpage.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{{ define "tag-subpage"}}
{{$PageData := index .DeepDataMerge.Tags .PageURL}}
{{ template "head" $PageData}}
{{ template "head" .}}

<body>
{{template "header" $PageData}}
{{template "header" .}}

<div class="body">
<article>
Expand All @@ -16,7 +16,7 @@
</article>
</div>

{{template "footer" $PageData}}
{{template "footer" .}}
</body>

</html>
Expand Down
9 changes: 5 additions & 4 deletions site/layout/tags.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{{ define "all-tags"}}
{{$PageData := .TagTemplateData}}
{{ template "head" $PageData}}
{{$PageData := .TemplateData}}
{{ template "head" .}}

<body>
{{template "header" $PageData}}
{{template "header" .}}
<div class="body">
<article>
<section class="posts">
Expand All @@ -20,7 +20,8 @@
<script src="https://d3js.org/d3.v7.min.js"></script>
<script src="/static/scripts/graph.js"></script>

{{template "footer" $PageData}}
{{template "footer" .}}

</body>

</html>
Expand Down
2 changes: 1 addition & 1 deletion test/engine/render_engine_generated/posts_template.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<div class="body">
<div class="posts">
{{range $PostDate, $Post := .Posts}}
{{range $PostDate, $Post := .DeepDataMerge.Posts}}
<a class="post-card" href="{{$Post.CompleteURL}}">
<div class="post-card-div">
<div class="post-card description">
Expand Down
2 changes: 1 addition & 1 deletion test/engine/render_tags/tags_template.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{ define "all-tags"}}
{{$PageData := .TagTemplateData}}
{{$PageData := .TemplateData}}

<body>
<div class="body">
Expand Down

0 comments on commit 8da851b

Please sign in to comment.