From 395471407a4f68d79dc545bb3bf09483fbc0eb75 Mon Sep 17 00:00:00 2001 From: poyo Date: Sun, 4 Oct 2026 00:06:30 +0200 Subject: [PATCH] render markdown into html and save to _dist. Ensure ./_dist and ./content exist. --- _dist/0.html | 3 +++ _dist/1.html | 3 +++ _dist/2.html | 3 +++ main.go | 34 +++++++++++----------------------- markdown.go | 1 + misc.go | 30 ++++++++++++++++++++++++++++++ 6 files changed, 51 insertions(+), 23 deletions(-) create mode 100755 _dist/0.html create mode 100755 _dist/1.html create mode 100755 _dist/2.html create mode 100644 misc.go diff --git a/_dist/0.html b/_dist/0.html new file mode 100755 index 0000000..a1c1bf6 --- /dev/null +++ b/_dist/0.html @@ -0,0 +1,3 @@ +

Cookies

+ +

blah blah blah

diff --git a/_dist/1.html b/_dist/1.html new file mode 100755 index 0000000..da1cf37 --- /dev/null +++ b/_dist/1.html @@ -0,0 +1,3 @@ +

No frontmatter at all

+ +

No frontmatter at all!!

diff --git a/_dist/2.html b/_dist/2.html new file mode 100755 index 0000000..a1c1bf6 --- /dev/null +++ b/_dist/2.html @@ -0,0 +1,3 @@ +

Cookies

+ +

blah blah blah

diff --git a/main.go b/main.go index 0a60009..c060630 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,6 @@ package main import ( - "errors" "fmt" "log" "os" @@ -13,26 +12,11 @@ const OUTPUT_DIR = "./_dist" const UNTITLED_POST = "Untitled rambling." func main() { - dir, err := os.ReadDir(CONTENT_DIR) - if err != nil { - if !errors.Is(err, os.ErrNotExist) { - log.Panic(err) - } - - fmt.Printf("Your %s folder does not exist yet. Creating one for you... ", CONTENT_DIR) - err := os.Mkdir("./content", os.ModePerm) - if err != nil { - log.Panic(err) - } - fmt.Print("Done!\n") + srcDir := EnsureDir(CONTENT_DIR) + // distDir := EnsureDir(OUTPUT_DIR) + EnsureDir(OUTPUT_DIR) - dir, err = os.ReadDir(CONTENT_DIR) - if err != nil { - log.Panic(err) // it's over - } - } - - posts, err := loadPosts(dir) + posts, err := loadPosts(srcDir) if err != nil { log.Panic(err) } @@ -40,13 +24,17 @@ func main() { if len(posts) == 0 { fmt.Printf("No posts could be found at %s\n", CONTENT_DIR) } else { - // convert to html + for i, post := range posts { + post.Html = mdToHtml(post.Content) + os.WriteFile(path.Join(OUTPUT_DIR, fmt.Sprint(i)+".html"), post.Html, os.ModePerm) + } } } type Post struct { Frontmatter Frontmatter - Content string + Content []byte // markdown + Html []byte // html } func loadPosts(dir []os.DirEntry) ([]Post, error) { @@ -62,7 +50,7 @@ func loadPosts(dir []os.DirEntry) ([]Post, error) { files = append(files, Post{ Frontmatter: parsedFrontmatter, - Content: content, + Content: []byte(content), }) } diff --git a/markdown.go b/markdown.go index 77d2326..f136a55 100644 --- a/markdown.go +++ b/markdown.go @@ -6,6 +6,7 @@ import ( "github.com/gomarkdown/markdown/parser" ) +// converts markdown into HTML. func mdToHtml(input []byte) []byte { extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock p := parser.NewWithExtensions(extensions) diff --git a/misc.go b/misc.go new file mode 100644 index 0000000..81ac34f --- /dev/null +++ b/misc.go @@ -0,0 +1,30 @@ +package main + +import ( + "errors" + "fmt" + "log" + "os" +) + +func EnsureDir(path string) []os.DirEntry { + dir, err := os.ReadDir(path) + if err != nil { + if !errors.Is(err, os.ErrNotExist) { + log.Panic(err) + } + + fmt.Printf("The '%s' directory does not exist yet. Creating one for you... ", path) + err := os.Mkdir(path, os.ModePerm) + if err != nil { + log.Panic(err) + } + fmt.Print("Done!\n") + + dir, err = os.ReadDir(path) + if err != nil { + log.Panic(err) // it's over + } + } + return dir +} -- 2.54.0