Skip to content

Commit

Permalink
(merge) fix: prevent panic due to frontmatter absence, remove baseURL…
Browse files Browse the repository at this point in the history
…, static links updation
  • Loading branch information
anirudhsudhir authored Mar 2, 2024
2 parents ef3fd75 + d0baf41 commit 1f46cfd
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 149 deletions.
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
# Static Site Generator

Checkout this doc-page [rendered](https://ssg-test-org.github.io/docs.html) using our ssg

## Directory structure

The ssg currently requires the following directory structure
Expand Down Expand Up @@ -66,7 +62,7 @@ The layout files can access the following rendered data from the markdown files:

## Notes

1. Images: To add images, add it to the 'static/' folder or a subdirectory under it. Use "static/[imagename.format]" as the image link format in the markdown files.
1. Images: To add images, add it to the 'static/' folder or a subdirectory under it. Use "/static/[imagename.format]" as the image link format in the markdown files.

2. CSS: CSS can be added in the following ways:

Expand Down Expand Up @@ -95,6 +91,7 @@ navbar:
- posts

baseURL: http://localhost:8000/
# Replace this with the actual canonical-url of your site.

# baseURL tells search-engines (SEO), web-crawlers (robots.txt) so people can discover your site on the internet.
# It's also embeded in your sitemap / atom feed and can be used to change metadata about your site.
Expand Down
1 change: 0 additions & 1 deletion TODO.md

This file was deleted.

46 changes: 8 additions & 38 deletions cmd/ssg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"log"
"net/http"
"os"
"regexp"
"slices"
"strings"

Expand Down Expand Up @@ -59,11 +58,10 @@ func (g *Generator) draftChecker() {
func (g *Generator) RenderSite(addr string) {
// Creating the "rendered" directory if not present
err := os.MkdirAll("rendered/", 0750)
if err != nil {
g.ErrorLogger.Fatal(err)
}
if err != nil {
g.ErrorLogger.Fatal(err)
}

g.replaceBaseURL(addr)
g.parseConfig()
g.readMdDir("content/")
g.parseRobots()
Expand Down Expand Up @@ -128,30 +126,7 @@ func (g *Generator) RenderSite(addr string) {
g.ErrorLogger.Fatal(err)
}
}
func (g *Generator) replaceBaseURL(addr string) {
configFile, err := os.ReadFile("layout/config.yml")
if err != nil {
g.ErrorLogger.Fatal(err)
}

var config LayoutConfig
if err := yaml.Unmarshal(configFile, &config); err != nil {
g.ErrorLogger.Fatal(err)
}

config.BaseURL = "http://localhost:" + addr + "/"

updatedConfig, err := yaml.Marshal(config)
if err != nil {
g.ErrorLogger.Fatal(err)
}

if err = os.WriteFile("layout/config.yml", updatedConfig, 0666); err != nil {
g.ErrorLogger.Fatal(err)
}
}

// link sitemap at the end of robots.txt
func (g *Generator) parseRobots() {
tmpl, err := template.ParseFiles("layout/robots.txt")
if err != nil {
Expand Down Expand Up @@ -219,7 +194,11 @@ func (g *Generator) parseMarkdownContent(filecontent string) (Frontmatter, strin
markdown content
--- => markdown divider and not to be touched while yaml parsing
*/
frontmatterSplit := strings.Split(filecontent, "---")[1]
splitContents := strings.Split(filecontent, "---")
frontmatterSplit := ""
if len(splitContents) > 1 {
frontmatterSplit = splitContents[1]
}

if frontmatterSplit != "" {
// Parsing YAML frontmatter
Expand All @@ -235,8 +214,6 @@ func (g *Generator) parseMarkdownContent(filecontent string) (Frontmatter, strin
markdown = filecontent
}

g.generateAbsoluteStaticLinks(&markdown)

// Parsing markdown to HTML
var parsedMarkdown bytes.Buffer
if err := goldmark.Convert([]byte(markdown), &parsedMarkdown); err != nil {
Expand Down Expand Up @@ -264,13 +241,6 @@ func (g *Generator) parseConfig() {
}
}

// Make links to static assets load from root dir /
func (g *Generator) generateAbsoluteStaticLinks(mdBody *string) {
re := regexp.MustCompile(`static\/`)
absLink := "/" + "static/"
*mdBody = re.ReplaceAllString(*mdBody, absLink)
}

// Parse all the ".html" layout files in the layout/ directory
func (g *Generator) parseLayoutFiles() *template.Template {
// Parsing all files in the layout/ dir which match the "*.html" pattern
Expand Down
14 changes: 0 additions & 14 deletions content/about.md

This file was deleted.

2 changes: 1 addition & 1 deletion content/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ The layout files can access the following rendered data from the markdown files:

## Notes

1. Images: To add images, add it to the 'static/' folder or a subdirectory under it. Use "static/[imagename.format]" as the image link format in the markdown files.
1. Images: To add images, add it to the 'static/' folder or a subdirectory under it. Use "/static/[imagename.format]" as the image link format in the markdown files.

2. CSS: CSS can be added in the following ways:

Expand Down
4 changes: 2 additions & 2 deletions content/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ title: Home
---

### Henlo!
A computer science student and tech enthusiast. Welcome to my home on the internet.
This is the documentation for the SSG

![demo-image](static/plane.jpg)
![demo-image](/static/plane.jpg)
86 changes: 0 additions & 86 deletions content/posts/qapture.md

This file was deleted.

1 change: 1 addition & 0 deletions content/posts/test_nofrontmatter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a file without frontmatter
3 changes: 1 addition & 2 deletions layout/config.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
navbar:
- index
- about
- docs
- posts
baseURL: http://localhost:8000/
baseURL: https://ssg-test-org.github.io/

0 comments on commit 1f46cfd

Please sign in to comment.