Personal website hybrid SSG.

Code git clone ssh://git.poy.ooo:6969/~poyo/ssg ZIP
main.go / 1KiB #c0606308
Raw
package mainimport (	"fmt"	"log"	"os"	"path")const CONTENT_DIR = "./content"const OUTPUT_DIR = "./_dist"const UNTITLED_POST = "Untitled rambling."func main() {	srcDir := EnsureDir(CONTENT_DIR)	// distDir := EnsureDir(OUTPUT_DIR)	EnsureDir(OUTPUT_DIR)	posts, err := loadPosts(srcDir)	if err != nil {		log.Panic(err)	}	if len(posts) == 0 {		fmt.Printf("No posts could be found at %s\n", CONTENT_DIR)	} else {		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     []byte // markdown	Html        []byte // html}func loadPosts(dir []os.DirEntry) ([]Post, error) {	var files []Post	for _, file := range dir {		data, err := os.ReadFile(path.Join(CONTENT_DIR, file.Name()))		if err != nil {			log.Printf("whoops. something went wrong while attempting to get info from %s: %s\n", file.Name(), err)		}		content, frontmatter := splitFrontmatter(string(data))		parsedFrontmatter := parseFrontmatter(frontmatter)		files = append(files, Post{			Frontmatter: parsedFrontmatter,			Content:     []byte(content),		})	}	for _, file := range files {		title := file.Frontmatter.Title		if title == "" {			fmt.Printf("Parsed an untitled post\n")		} else {			fmt.Printf("Parsed '%s'\n", file.Frontmatter.Title)		}	}	return files, nil}