Skip to content

Commit

Permalink
feat: group posts by collections
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 Jun 23, 2024
1 parent d4c547f commit c0337d8
Show file tree
Hide file tree
Showing 12 changed files with 176 additions and 7 deletions.
16 changes: 10 additions & 6 deletions cmd/anna/anna.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,21 @@ type Cmd struct {
func (cmd *Cmd) VanillaRender() {
// Defining Engine and Parser Structures
p := parser.Parser{
Templates: make(map[template.URL]parser.TemplateData, 10),
TagsMap: make(map[template.URL][]parser.TemplateData, 10),
Notes: make(map[template.URL]parser.Note, 10),
ErrorLogger: log.New(os.Stderr, "ERROR\t", log.Ldate|log.Ltime|log.Lshortfile),
RenderDrafts: cmd.RenderDrafts,
LiveReload: cmd.LiveReload,
Templates: make(map[template.URL]parser.TemplateData, 10),
TagsMap: make(map[template.URL][]parser.TemplateData, 10),
CollectionsMap: make(map[template.URL][]parser.TemplateData, 10),
Notes: make(map[template.URL]parser.Note, 10),
ErrorLogger: log.New(os.Stderr, "ERROR\t", log.Ldate|log.Ltime|log.Lshortfile),
RenderDrafts: cmd.RenderDrafts,
LiveReload: cmd.LiveReload,
}

e := engine.Engine{
ErrorLogger: log.New(os.Stderr, "ERROR\t", log.Ldate|log.Ltime|log.Lshortfile),
}
e.DeepDataMerge.Templates = make(map[template.URL]parser.TemplateData, 10)
e.DeepDataMerge.TagsMap = make(map[template.URL][]parser.TemplateData, 10)
e.DeepDataMerge.CollectionsMap = make(map[template.URL][]parser.TemplateData, 10)
e.DeepDataMerge.Notes = make(map[template.URL]parser.Note, 10)
e.DeepDataMerge.LinkStore = make(map[template.URL][]*parser.Note, 10)

Expand All @@ -55,6 +57,7 @@ func (cmd *Cmd) VanillaRender() {

e.DeepDataMerge.Templates = p.Templates
e.DeepDataMerge.TagsMap = p.TagsMap
e.DeepDataMerge.CollectionsMap = p.CollectionsMap
e.DeepDataMerge.LayoutConfig = p.LayoutConfig
e.DeepDataMerge.Posts = p.Posts
e.DeepDataMerge.Notes = p.Notes
Expand Down Expand Up @@ -91,4 +94,5 @@ func (cmd *Cmd) VanillaRender() {
e.RenderEngineGeneratedFiles(helpers.SiteDataPath, templ)
e.RenderUserDefinedPages(helpers.SiteDataPath, templ)
e.RenderTags(helpers.SiteDataPath, templ)
e.RenderCollections(helpers.SiteDataPath, templ)
}
83 changes: 83 additions & 0 deletions pkg/engine/anna_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ type TagRootTemplateData struct {
TagNames []string
}

type CollectionRootTemplateData struct {
DeepDataMerge DeepDataMerge
PageURL template.URL
TemplateData parser.TemplateData
CollectionNames []string
}

func (e *Engine) RenderTags(fileOutPath string, templ *template.Template) {
var tagsBuffer bytes.Buffer

Expand Down Expand Up @@ -100,6 +107,82 @@ func (e *Engine) RenderTags(fileOutPath string, templ *template.Template) {
wg.Wait()
}

func (e *Engine) RenderCollections(fileOutPath string, templ *template.Template) {
var collectionsBuffer bytes.Buffer

// Extracting collection titles
collections := make([]template.URL, 0, len(e.DeepDataMerge.CollectionsMap))
for collection := range e.DeepDataMerge.CollectionsMap {
collections = append(collections, collection)
}

slices.SortFunc(collections, func(a, b template.URL) int {
return cmp.Compare(strings.ToLower(string(a)), strings.ToLower(string(b)))
})

collectionNames := make([]string, 0, len(collections))
for _, collection := range collections {
collectionString := string(collection)
collectionString, _ = strings.CutPrefix(collectionString, "collections/")
collectionString, _ = strings.CutSuffix(collectionString, ".html")

collectionNames = append(collectionNames, string(collectionString))
}

collectionRootTemplataData := parser.TemplateData{
Frontmatter: parser.Frontmatter{Title: "Collections"},
}

collectionTemplateData := CollectionRootTemplateData{
DeepDataMerge: e.DeepDataMerge,
PageURL: "collections.html",
TemplateData: collectionRootTemplataData,
CollectionNames: collectionNames,
}

// Rendering the page displaying all tags
err := templ.ExecuteTemplate(&collectionsBuffer, "all-collections", collectionTemplateData)
if err != nil {
e.ErrorLogger.Fatal(err)
}

// Flushing 'collections.html' to the disk
err = os.WriteFile(fileOutPath+"rendered/collections.html", collectionsBuffer.Bytes(), 0666)
if err != nil {
e.ErrorLogger.Fatal(err)
}

// Create a wait group to wait for all goroutines to finish
var wg sync.WaitGroup

e.DeepDataMerge.Collections = make(map[template.URL]parser.TemplateData)

for collection := range e.DeepDataMerge.CollectionsMap {
collectionString := string(collection)
collectionString, _ = strings.CutPrefix(collectionString, "collections/")
collectionString, _ = strings.CutSuffix(collectionString, ".html")

e.DeepDataMerge.Collections[collection] = parser.TemplateData{
Frontmatter: parser.Frontmatter{
Title: collectionString,
},
}
}

// Rendering the subpages with merged tagged posts
for collection, collectionTemplates := range e.DeepDataMerge.CollectionsMap {
wg.Add(1)
go func(collection template.URL, collectionTemplates []parser.TemplateData) {
defer wg.Done()

e.RenderPage(fileOutPath, collection, templ, "collection-subpage")
}(collection, collectionTemplates)
}

// Wait for all goroutines to finish
wg.Wait()
}

func (e *Engine) GenerateNoteJSONIdex(outFilePath string) {
jsonFile, err := os.Create(outFilePath + "rendered/static/noteindex.json")
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions pkg/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ type DeepDataMerge struct {
// Posts contains the template data of files in the posts directory
Posts []parser.TemplateData

// Templates stores the template data of all collection sub-pages of the site
Collections map[template.URL]parser.TemplateData

// K-V pair storing all templates corresponding to a particular collection in the site
CollectionsMap map[template.URL][]parser.TemplateData

//Stores all the notes
Notes map[template.URL]parser.Note

Expand Down
12 changes: 12 additions & 0 deletions pkg/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type Frontmatter struct {
Tags []string `yaml:"tags"`
TOC bool `yaml:"toc"`
Authors []string `yaml:"authors"`
Collections []string `yaml:"collections"`

// Head is specifically used for
// mentioning the head of the notes
Expand All @@ -69,6 +70,9 @@ type Parser struct {
// K-V pair storing all templates correspoding to a particular tag in the site
TagsMap map[template.URL][]TemplateData

// Collections stores template data of files in collections
CollectionsMap map[template.URL][]TemplateData

// Stores data parsed from layout/config.yml
LayoutConfig LayoutConfig

Expand Down Expand Up @@ -172,6 +176,14 @@ func (p *Parser) AddFile(baseDirPath string, dirEntryPath string, frontmatter Fr
p.TagsMap[template.URL(tagsMapKey)] = append(p.TagsMap[template.URL(tagsMapKey)], page)

}

// Adding the page to the collections map with the corresponding collections
for _, collection := range page.Frontmatter.Collections {
collectionsMapKey := "collections/" + collection + ".html"
p.CollectionsMap[template.URL(collectionsMapKey)] = append(p.CollectionsMap[template.URL(collectionsMapKey)], page)

}

}

if frontmatter.Type == "note" {
Expand Down
3 changes: 3 additions & 0 deletions site/content/developer-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ date: 2024-04-28
title: Developer Guide
type: page
toc: true
collections:
- anna
- guide
---

# Developer Guide
Expand Down
3 changes: 3 additions & 0 deletions site/content/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ date: 2024-04-28
title: Anna Documentation
type: page
toc: true
collections:
- anna
- guide
---

---
Expand Down
6 changes: 6 additions & 0 deletions site/content/docs_to_add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
title: home
type: page
---

Collections field and associated documentation
1 change: 1 addition & 0 deletions site/content/quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ date: 2024-04-28
title: Quick Start
type: page
toc: true
collections: ["anna"]
---

# Quick Start
Expand Down
24 changes: 24 additions & 0 deletions site/layout/collection-subpage.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{{ define "collection-subpage"}}
{{$PageData := index .DeepDataMerge.Collections .PageURL}}
{{ template "head" .}}

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

<div class="body">
<article>
<section class="tagged-posts">
{{$CollectionSet := index .DeepDataMerge.CollectionsMap .PageURL}}
{{range $CollectionSet }}
<a href="/{{.CompleteURL}}">{{.Frontmatter.Title}}</a>
{{end}}
</section>
</article>
</div>

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

</html>

{{ end }}
26 changes: 26 additions & 0 deletions site/layout/collections.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{{ define "all-collections"}}
{{$PageData := .TemplateData}}
{{ template "head" .}}

<body>
{{template "header" .}}
<div class="body">
<article>
<section class="posts">
<div class="all-tags">
{{range .CollectionNames}}
<a href="/collections/{{.}}.html">{{.}}</a>
{{end}}
</div>
</section>
</article>
</div>


{{template "footer" .}}

</body>

</html>

{{ end}}
1 change: 1 addition & 0 deletions site/layout/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ navbar:
- Docs: docs.html
- Dev Guide: developer-guide.html
- Tags: tags.html
- Collections: collections.html
- Notes: notes.html
- Posts: posts.html
baseURL: https://anna-docs.netlify.app
Expand Down
2 changes: 1 addition & 1 deletion test/engine/json_index_test/want_index.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"docs.md":{"CompleteURL":"docs.html","Frontmatter":{"Title":"Anna Documentation","Date":"","Draft":false,"JSFiles":null,"Type":"","Description":"","PreviewImage":"","Tags":null,"TOC":false,"Authors":null,"Head":false},"Tags":null}}
{"docs.md":{"CompleteURL":"docs.html","Frontmatter":{"Title":"Anna Documentation","Date":"","Draft":false,"JSFiles":null,"Type":"","Description":"","PreviewImage":"","Tags":null,"TOC":false,"Authors":null,"Collections":null,"Head":false},"Tags":null}}

0 comments on commit c0337d8

Please sign in to comment.