Skip to content

Commit

Permalink
fix: json parsing and search functionality
Browse files Browse the repository at this point in the history
Issue: search broken in post, migrate to wasm code
  • Loading branch information
bwaklog committed Mar 31, 2024
1 parent d533d39 commit ad7a86b
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 29 deletions.
1 change: 1 addition & 0 deletions cmd/anna/anna.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func (cmd *Cmd) VanillaRender() {

e.GenerateSitemap(helpers.SiteDataPath + "rendered/layout/sitemap.xml")
e.GenerateFeed()
p.ParseJsonMerged()

sort.Slice(e.Posts, func(i, j int) bool {
return e.Posts[i].Frontmatter.Date > e.Posts[j].Frontmatter.Date
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func main() {
annaCmd.VanillaRender()
},
}
go anna.CreateIndex()
// go anna.CreateIndex()

rootCmd.Flags().BoolVarP(&serve, "serve", "s", false, "serve the rendered content")
rootCmd.Flags().StringVarP(&addr, "addr", "a", "8000", "ip address to serve rendered content to")
Expand Down
44 changes: 44 additions & 0 deletions pkg/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package parser

import (
"bytes"
"encoding/json"
"regexp"

"html/template"
Expand Down Expand Up @@ -53,6 +54,13 @@ type TemplateData struct {
SpecificTagTemplates []TemplateData
}

type JsonTemplate struct {
CompleteURL template.URL
FilenameWithoutExtension string
Frontmatter Frontmatter
Tags []string
}

type Date int64

type Parser struct {
Expand Down Expand Up @@ -257,3 +265,39 @@ func (p *Parser) ParseLayoutFiles() *template.Template {

return templ
}

func (p *Parser) ParseJsonMerged() {
// create a json file
jsonFile, err := os.Create(helpers.SiteDataPath + "/static/scripts/merged.json")
if err != nil {
jsonFile.Close()
p.ErrorLogger.Fatal(err)
}

// copy data from p.Templates to new JsonTemplate
jsonMergedTemplate := make(map[template.URL]JsonTemplate)
for key, value := range p.Templates {
// copy the values
jsonMergedTemplate[key] = JsonTemplate{
CompleteURL: value.CompleteURL,
FilenameWithoutExtension: value.FilenameWithoutExtension,
Frontmatter: value.Frontmatter,
Tags: value.Tags,
}
}

// marshal the new JsonTemplate
jsonMerged, err := json.Marshal(jsonMergedTemplate)
if err != nil {
jsonFile.Close()
p.ErrorLogger.Fatal(err)
}

// write the new JsonTemplate to the json file
_, err = jsonFile.Write(jsonMerged)
if err != nil {
jsonFile.Close()
p.ErrorLogger.Fatal(err)
}
jsonFile.Close()
}
21 changes: 0 additions & 21 deletions site/layout/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,6 @@
<div class="tag">
<a href="/tags/{{.}}.html">{{.}}</a>
</div>
<label for="urlInput">Website URL:</label>
<input type="text" id="urlInput" placeholder="Enter website URL">

<label for="keywordInput">Keyword:</label>
<input type="text" id="keywordInput" placeholder="Enter keyword">

<button onclick="performSearch()">Search</button>

<script>
function performSearch() {
// Retrieve values from input fields
var url = document.getElementById("urlInput").value;
var keyword = document.getElementById("keywordInput").value;

// Call the search function passing URL and keyword
var searchResult = search(url, keyword);

// Display search result (example: print to console)
console.log("Search Result:", searchResult);
}
</script>
{{end}}
</div>
{{.Body}}
Expand Down
62 changes: 62 additions & 0 deletions site/layout/partials/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,66 @@
{{end}} {{end}}
</nav>
</header>

<form class="siteSearch" id="searchSite">
<input
type="text"
placeholder="Serach Site"
id="searchSiteInput"
name="searchSite"
required
/>
</form>
<div id="result"></div>
<script>
var resultDiv = document.getElementById("result");
document
.getElementById("searchSite")
.addEventListener("keyup", function (e) {
e.preventDefault();
const searchValue =
document.getElementById("searchSiteInput").value;

const url = "static/scripts/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())) {
console.log(data[key]["Frontmatter"]["Title"]);

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("result").innerHTML = "";
}
})
.catch((error) => {
// Handle errors
console.error(
"There was a problem with the fetch operation:",
error,
);
});
});
</script>
{{end}}
1 change: 1 addition & 0 deletions site/static/scripts/merged.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"docs.md":{"CompleteURL":"docs.html","FilenameWithoutExtension":"docs","Frontmatter":{"Title":"Anna Documentation","Date":"","Draft":false,"JSFiles":null,"Type":"","Description":"","PreviewImage":"","Tags":null},"Tags":null},"index copy.md":{"CompleteURL":"index copy.html","FilenameWithoutExtension":"index copy","Frontmatter":{"Title":"Home","Date":"2024-02-24","Draft":false,"JSFiles":null,"Type":"","Description":"homepage for our ssg","PreviewImage":"/static/plane.jpg","Tags":null},"Tags":null},"index.md":{"CompleteURL":"index.html","FilenameWithoutExtension":"index","Frontmatter":{"Title":"Home","Date":"2024-02-24","Draft":false,"JSFiles":null,"Type":"","Description":"homepage for our ssg","PreviewImage":"/static/plane.jpg","Tags":null},"Tags":null},"lang_test copy.md":{"CompleteURL":"posts/lang_test copy.html","FilenameWithoutExtension":"lang_test copy","Frontmatter":{"Title":"Language Codeblock Test","Date":"2024-02-23","Draft":false,"JSFiles":null,"Type":"post","Description":"","PreviewImage":"","Tags":["rust","C"]},"Tags":null},"lang_test.md":{"CompleteURL":"posts/lang_test.html","FilenameWithoutExtension":"lang_test","Frontmatter":{"Title":"Language Codeblock Test","Date":"2024-02-23","Draft":false,"JSFiles":null,"Type":"post","Description":"","PreviewImage":"","Tags":["rust","C"]},"Tags":null},"test_0.md":{"CompleteURL":"posts/test_0.html","FilenameWithoutExtension":"test_0","Frontmatter":{"Title":"Chronological Test 2","Date":"2023-01-02","Draft":false,"JSFiles":null,"Type":"post","Description":"This is a test description","PreviewImage":"","Tags":["test-post"]},"Tags":null},"week-1.md":{"CompleteURL":"posts/week-1.html","FilenameWithoutExtension":"week-1","Frontmatter":{"Title":"Week-1 Progress","Date":"2024-03-25","Draft":false,"JSFiles":null,"Type":"post","Description":"","PreviewImage":"","Tags":["progress"]},"Tags":null},"week-2.md":{"CompleteURL":"posts/week-2.html","FilenameWithoutExtension":"week-2","Frontmatter":{"Title":"Week-2 Progress","Date":"2024-04-01","Draft":false,"JSFiles":null,"Type":"post","Description":"","PreviewImage":"","Tags":["progress"]},"Tags":null}}
File renamed without changes.
34 changes: 27 additions & 7 deletions site/static/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,28 @@ nav {
}
}

#searchSiteInput {
background: #11111a;
color: white;
border: 1px solid #5990ac;
border-radius: 5px;
margin-left: 1rem;
padding: 0.5rem;
::placeholder {
color: white;
}
&:focus {
outline: none;
}
$:active {
outline: none;
}
}

#result {
margin-left: 1rem;
}

a {
color:#5990ac;
text-decoration: none;
Expand Down Expand Up @@ -177,7 +199,7 @@ iframe {
flex-direction: row;
align-items: center;
flex-wrap: wrap;

& > a {
font-size: 1.2rem;
background-color: #1e1e1e;
Expand All @@ -186,7 +208,7 @@ iframe {
margin: .3rem;

}

}

.tag-placeholder {
Expand Down Expand Up @@ -363,7 +385,7 @@ blockquote {
nav {
font-size: 1.05rem;
}

iframe {
width: auto;
height: auto;
Expand All @@ -388,7 +410,7 @@ blockquote {
margin-top: 2rem;
margin-bottom: 2rem;
}


.body {
& > a,
Expand All @@ -407,7 +429,7 @@ blockquote {
& > p,
& > .post-card-div > p {
font-size: .9rem;
}
}

& > h3 {
font-size: 1.3rem;
Expand All @@ -423,5 +445,3 @@ blockquote {
height: auto;
}
}


0 comments on commit ad7a86b

Please sign in to comment.