✨feature: Make "pretty" URLs
This commit is contained in:
90
main.go
90
main.go
@@ -29,7 +29,7 @@ func main() {
|
||||
return
|
||||
}
|
||||
|
||||
files, err := readMarkdownFiles(contentDir)
|
||||
files, err := readContentFiles(contentDir)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to read content directory: %v\n", err)
|
||||
return
|
||||
@@ -37,7 +37,14 @@ func main() {
|
||||
|
||||
nav := buildNavigation(files)
|
||||
|
||||
generatePages(files, contentDir, outputDir, tpl, nav)
|
||||
for _, file := range files {
|
||||
if filepath.Ext(file.Name()) == ".md" {
|
||||
err := generateHTML(file, contentDir, outputDir, tpl, nav)
|
||||
if err != nil {
|
||||
fmt.Printf("Error generating HTML for %s: %v\n", file.Name(), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("✅ Site generation complete.")
|
||||
}
|
||||
@@ -46,7 +53,7 @@ func parseTemplate(templateFile string) (*template.Template, error) {
|
||||
return template.ParseFiles(templateFile)
|
||||
}
|
||||
|
||||
func readMarkdownFiles(contentDir string) ([]os.FileInfo, error) {
|
||||
func readContentFiles(contentDir string) ([]os.FileInfo, error) {
|
||||
return ioutil.ReadDir(contentDir)
|
||||
}
|
||||
|
||||
@@ -54,47 +61,52 @@ func buildNavigation(files []os.FileInfo) []NavItem {
|
||||
var nav []NavItem
|
||||
for _, file := range files {
|
||||
if filepath.Ext(file.Name()) == ".md" {
|
||||
title := strings.Title(strings.TrimSuffix(file.Name(), filepath.Ext(file.Name())))
|
||||
filename := strings.TrimSuffix(file.Name(), filepath.Ext(file.Name())) + ".html"
|
||||
nav = append(nav, NavItem{Title: title, URL: "/" + filename})
|
||||
name := strings.TrimSuffix(file.Name(), filepath.Ext(file.Name()))
|
||||
title := strings.Title(name)
|
||||
url := "/"
|
||||
if name != "index" {
|
||||
url = "/" + name + "/"
|
||||
}
|
||||
nav = append(nav, NavItem{Title: title, URL: url})
|
||||
}
|
||||
}
|
||||
return nav
|
||||
}
|
||||
|
||||
func generatePages(files []os.FileInfo, contentDir, outputDir string, tpl *template.Template, nav []NavItem) {
|
||||
for _, file := range files {
|
||||
if filepath.Ext(file.Name()) == ".md" {
|
||||
mdPath := filepath.Join(contentDir, file.Name())
|
||||
mdContent, err := ioutil.ReadFile(mdPath)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to read %s: %v\n", file.Name(), err)
|
||||
continue
|
||||
}
|
||||
func generateHTML(file os.FileInfo, contentDir, outputDir string, tpl *template.Template, nav []NavItem) error {
|
||||
name := strings.TrimSuffix(file.Name(), filepath.Ext(file.Name()))
|
||||
title := strings.Title(name)
|
||||
|
||||
htmlContent := blackfriday.Run(mdContent)
|
||||
outFile := strings.TrimSuffix(file.Name(), filepath.Ext(file.Name())) + ".html"
|
||||
outPath := filepath.Join(outputDir, outFile)
|
||||
out, err := os.Create(outPath)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to create %s: %v\n", outPath, err)
|
||||
continue
|
||||
}
|
||||
|
||||
data := map[string]interface{}{
|
||||
"Content": template.HTML(htmlContent),
|
||||
"Title": strings.Title(strings.TrimSuffix(file.Name(), filepath.Ext(file.Name()))),
|
||||
"Nav": nav,
|
||||
"Year": time.Now().Year(),
|
||||
}
|
||||
|
||||
err = tpl.Execute(out, data)
|
||||
if err != nil {
|
||||
fmt.Printf("Template execution failed for %s: %v\n", file.Name(), err)
|
||||
continue
|
||||
}
|
||||
|
||||
fmt.Printf("Generated: %s\n", outPath)
|
||||
}
|
||||
mdPath := filepath.Join(contentDir, file.Name())
|
||||
mdContent, err := ioutil.ReadFile(mdPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to read %s: %w", file.Name(), err)
|
||||
}
|
||||
|
||||
htmlContent := blackfriday.Run(mdContent)
|
||||
|
||||
var outPath string
|
||||
if name == "index" {
|
||||
outPath = filepath.Join(outputDir, "index.html")
|
||||
} else {
|
||||
subDir := filepath.Join(outputDir, name)
|
||||
os.MkdirAll(subDir, os.ModePerm)
|
||||
outPath = filepath.Join(subDir, "index.html")
|
||||
}
|
||||
|
||||
outFile, err := os.Create(outPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create %s: %w", outPath, err)
|
||||
}
|
||||
defer outFile.Close()
|
||||
|
||||
data := map[string]interface{}{
|
||||
"Content": template.HTML(htmlContent),
|
||||
"Title": title,
|
||||
"Nav": nav,
|
||||
"Year": time.Now().Year(),
|
||||
"Date": time.Now().Format("January 2, 2006"),
|
||||
}
|
||||
|
||||
return tpl.Execute(outFile, data)
|
||||
}
|
||||
|
||||
@@ -11,9 +11,9 @@
|
||||
<nav>
|
||||
<ul>
|
||||
|
||||
<li><a href="/about.html">About</a></li>
|
||||
<li><a href="/about/">About</a></li>
|
||||
|
||||
<li><a href="/index.html">Index</a></li>
|
||||
<li><a href="/">Index</a></li>
|
||||
|
||||
</ul>
|
||||
</nav>
|
||||
@@ -11,9 +11,9 @@
|
||||
<nav>
|
||||
<ul>
|
||||
|
||||
<li><a href="/about.html">About</a></li>
|
||||
<li><a href="/about/">About</a></li>
|
||||
|
||||
<li><a href="/index.html">Index</a></li>
|
||||
<li><a href="/">Index</a></li>
|
||||
|
||||
</ul>
|
||||
</nav>
|
||||
|
||||
Reference in New Issue
Block a user