package main import ( "flag" "fmt" "log" "os" "path" "path/filepath" "strings" ) const CONTENT_DIR = "./content" const OUTPUT_DIR = "./_dist" const UNTITLED_POST = "Untitled rambling." func main() { newAdminPassword := flag.String("set-password", "", "Sets the admin password and hashes it into the .envz.") flag.Parse() if *newAdminPassword != "" { passwordHash := HashPassword([]byte(*newAdminPassword)) SaveKeyToEnv(PASSWORD_HASH, string(passwordHash)) fmt.Printf("Hashed your password. '%s'. DO NOT SHARE IT.\n", passwordHash) os.Exit(0) } srcDir := EnsureDir(CONTENT_DIR, true) // distDir := EnsureDir(OUTPUT_DIR) ClearDir(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 _, post := range posts { post.Html = mdToHtml(post.Content) filenameNoExt := strings.TrimSuffix(post.Filename, filepath.Ext(post.Filename)) os.WriteFile(path.Join(OUTPUT_DIR, filenameNoExt)+".html", post.Html, os.ModePerm) } } } type Post struct { Filename string // only the name Frontmatter Frontmatter Content []byte // markdown Html []byte // html } func loadPosts(dir []os.DirEntry) ([]Post, error) { var files []Post for _, file := range dir { fileName := file.Name() data, err := os.ReadFile(path.Join(CONTENT_DIR, fileName)) 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{ Filename: file.Name(), 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 }