Skip to content

Commit

Permalink
fix: updated pr
Browse files Browse the repository at this point in the history
  • Loading branch information
anirudhsudhir committed Apr 1, 2024
1 parent 96c01e8 commit d82e8ea
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 99 deletions.
2 changes: 1 addition & 1 deletion cmd/anna/anna.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (cmd *Cmd) VanillaRender() {
e.GenerateSitemap(helpers.SiteDataPath + "rendered/layout/sitemap.xml")
e.GenerateFeed()
helper.CopyDirectoryContents(helpers.SiteDataPath+"static/", helpers.SiteDataPath+"rendered/static/")
e.GenerateMergedJson(helpers.SiteDataPath)
e.GenerateJSONIndex(helpers.SiteDataPath)

sort.Slice(e.Posts, func(i, j int) bool {
return e.Posts[i].Frontmatter.Date > e.Posts[j].Frontmatter.Date
Expand Down
21 changes: 8 additions & 13 deletions pkg/engine/anna_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"cmp"
"encoding/json"
"fmt"
"html/template"
"os"
"slices"
Expand Down Expand Up @@ -64,16 +63,16 @@ func (e *Engine) RenderTags(fileOutPath string, templ *template.Template) {
}
}

func (e *Engine) GenerateMergedJson(outFilePath string) {
// This function is extracting data off of the e.Templates array that has all
// required data for creating the merged json file with the data from all
// posts and pages contained in the side. The merged.json file will be created
// even when the site isnt being served (part of cmd/anna/anna.go)
jsonFile, err := os.Create(outFilePath + "/static/merged.json")
func (e *Engine) GenerateJSONIndex(outFilePath string) {
// This function creates an index of the site for search
// It extracts data from the e.Templates slice
// The index.json file is created during every VanillaRender()

jsonFile, err := os.Create(outFilePath + "/static/index.json")
if err != nil {
jsonFile.Close()
e.ErrorLogger.Fatal(err)
}
defer jsonFile.Close()

// Copying contents from e.Templates to new JsonMerged struct
jsonMergedData := make(map[template.URL]JsonTemplateData)
Expand All @@ -86,19 +85,16 @@ func (e *Engine) GenerateMergedJson(outFilePath string) {
}
}

// Marshasl the contents of the jsonMergedData
// Marshal the contents of jsonMergedData
jsonMergedMarshaledData, err := json.Marshal(jsonMergedData)
if err != nil {
jsonFile.Close()
e.ErrorLogger.Fatal(err)
}

_, err = jsonFile.Write(jsonMergedMarshaledData)
if err != nil {
jsonFile.Close()
e.ErrorLogger.Fatal(err)
}
jsonFile.Close()
}

func (e *Engine) GenerateSitemap(outFilePath string) {
Expand All @@ -120,7 +116,6 @@ func (e *Engine) GenerateSitemap(outFilePath string) {

e.Templates = tempTemplates

fmt.Println(e.LayoutConfig.BaseURL)
// Iterate over parsed markdown files
for _, templateData := range e.Templates {
url := e.LayoutConfig.BaseURL + "/" + templateData.FilenameWithoutExtension + ".html"
Expand Down
26 changes: 10 additions & 16 deletions pkg/engine/anna_engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ func TestRenderTags(t *testing.T) {
e.RenderTags(fileOutPath, templ)

t.Run("render tag.html", func(t *testing.T) {

got_tags_file, err := os.ReadFile(TestDirPath + "render_tags/rendered/tags.html")
if err != nil {
t.Errorf("%v", err)
Expand All @@ -80,7 +79,6 @@ func TestRenderTags(t *testing.T) {
})

t.Run("render tag-subpage.html", func(t *testing.T) {

got_blogs_file, err := os.ReadFile(TestDirPath + "render_tags/rendered/tags/blogs.html")
if err != nil {
t.Errorf("%v", err)
Expand Down Expand Up @@ -119,26 +117,22 @@ func TestGenerateMergedJson(t *testing.T) {
ErrorLogger: log.New(os.Stderr, "TEST ERROR\t", log.Ldate|log.Ltime|log.Lshortfile),
}

e.Templates["docs.md"] =
parser.TemplateData{
FilenameWithoutExtension: "docs",
CompleteURL: "docs.html",
Frontmatter: parser.Frontmatter{
Title: "Anna Documentation",
},
}

// use theParseJsonMerged method
e.GenerateMergedJson(TestDirPath + "merged_data_test")
e.Templates["docs.md"] = parser.TemplateData{
FilenameWithoutExtension: "docs",
CompleteURL: "docs.html",
Frontmatter: parser.Frontmatter{
Title: "Anna Documentation",
},
}

// read off from the generated json file and want_merged.json
e.GenerateJSONIndex(TestDirPath + "merged_data_test")

got_json, err := os.ReadFile(TestDirPath + "/merged_data_test/static/merged.json")
got_json, err := os.ReadFile(TestDirPath + "/merged_data_test/static/index.json")
if err != nil {
t.Errorf("%v", err)
}

want_json, err := os.ReadFile(TestDirPath + "/merged_data_test/want_merged.json")
want_json, err := os.ReadFile(TestDirPath + "/merged_data_test/want_index.json")
if err != nil {
t.Errorf("%v", err)
}
Expand Down
54 changes: 1 addition & 53 deletions site/layout/partials/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,57 +8,5 @@
{{end}} {{end}}
</nav>
</header>
<form class="siteSearch" id="searchSite">
<input type="text" placeholder="Serach Site" id="searchSiteInput" name="searchSite" required autocomplete="off" />
</form>
<div id="site_search_result"></div>
<script>
var resultDiv = document.getElementById("site_search_result");
document
.getElementById("searchSite")
.addEventListener("keyup", function (e) {
e.preventDefault();
const searchValue =
document.getElementById("searchSiteInput").value;

const url = "/static/merged.json";

fetch(url)
.then((response) => {
if (!response.ok) {
throw new Error("Network response was not ok");
}
return response.json();
})
.then((data) => {
resultDiv.innerHTML = "";
found = 0;
if (searchValue != "") {
for (key in data) {
found += 1;
title =
data[key]["Frontmatter"]["Title"].toLowerCase();
if (title.includes(searchValue.toLowerCase())) {
var a = document.createElement("a");
a.innerHTML = data[key]["Frontmatter"]["Title"];
a.href = `/${data[key]["CompleteURL"]}`;
resultDiv.appendChild(a);
resultDiv.appendChild(
document.createElement("br"),
);
}
}
} else {
document.getElementById("site_search_result").innerHTML = "";
}
})
.catch((error) => {
// Handle errors
console.error(
"There was a problem with the fetch operation:",
error,
);
});
});
</script>
{{template "search" .}}
{{end}}
23 changes: 7 additions & 16 deletions 2 → site/layout/partials/search.html
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
{{define "header"}}
<header>
<nav>
{{range .Layout.Navbar}} {{ if eq . "index" }}
<a href="/">[{{ $.Layout.Author }}]</a>
{{else}}
<a href="/{{.}}.html">[{{.}}]</a>
{{end}} {{end}}
</nav>
</header>
{{define "search"}}
<form class="siteSearch" id="searchSite">
<input type="text" placeholder="Serach Site" id="searchSiteInput" name="searchSite" required autocomplete="off" />
<input type="text" placeholder="Search Site" id="searchSiteInput" name="searchSite" required autocomplete="off" />
</form>
<div id="result"></div>
<div id="site_search_result"></div>
<script>
var resultDiv = document.getElementById("result");
var resultDiv = document.getElementById("site_search_result");
document
.getElementById("searchSite")
.addEventListener("keyup", function (e) {
e.preventDefault();
const searchValue =
document.getElementById("searchSiteInput").value;

const url = "/static/merged.json";
const url = "/static/index.json";

fetch(url)
.then((response) => {
Expand All @@ -41,15 +32,15 @@
if (title.includes(searchValue.toLowerCase())) {
var a = document.createElement("a");
a.innerHTML = data[key]["Frontmatter"]["Title"];
a.href = data[key]["CompleteURL"];
a.href = `/${data[key]["CompleteURL"]}`;
resultDiv.appendChild(a);
resultDiv.appendChild(
document.createElement("br"),
);
}
}
} else {
document.getElementById("result").innerHTML = "";
document.getElementById("site_search_result").innerHTML = "";
}
})
.catch((error) => {
Expand Down
File renamed without changes.

0 comments on commit d82e8ea

Please sign in to comment.