✨feature: Make "pretty" URLs
This commit is contained in:
90
main.go
90
main.go
@@ -29,7 +29,7 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
files, err := readMarkdownFiles(contentDir)
|
files, err := readContentFiles(contentDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Failed to read content directory: %v\n", err)
|
fmt.Printf("Failed to read content directory: %v\n", err)
|
||||||
return
|
return
|
||||||
@@ -37,7 +37,14 @@ func main() {
|
|||||||
|
|
||||||
nav := buildNavigation(files)
|
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.")
|
fmt.Println("✅ Site generation complete.")
|
||||||
}
|
}
|
||||||
@@ -46,7 +53,7 @@ func parseTemplate(templateFile string) (*template.Template, error) {
|
|||||||
return template.ParseFiles(templateFile)
|
return template.ParseFiles(templateFile)
|
||||||
}
|
}
|
||||||
|
|
||||||
func readMarkdownFiles(contentDir string) ([]os.FileInfo, error) {
|
func readContentFiles(contentDir string) ([]os.FileInfo, error) {
|
||||||
return ioutil.ReadDir(contentDir)
|
return ioutil.ReadDir(contentDir)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,47 +61,52 @@ func buildNavigation(files []os.FileInfo) []NavItem {
|
|||||||
var nav []NavItem
|
var nav []NavItem
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
if filepath.Ext(file.Name()) == ".md" {
|
if filepath.Ext(file.Name()) == ".md" {
|
||||||
title := strings.Title(strings.TrimSuffix(file.Name(), filepath.Ext(file.Name())))
|
name := strings.TrimSuffix(file.Name(), filepath.Ext(file.Name()))
|
||||||
filename := strings.TrimSuffix(file.Name(), filepath.Ext(file.Name())) + ".html"
|
title := strings.Title(name)
|
||||||
nav = append(nav, NavItem{Title: title, URL: "/" + filename})
|
url := "/"
|
||||||
|
if name != "index" {
|
||||||
|
url = "/" + name + "/"
|
||||||
|
}
|
||||||
|
nav = append(nav, NavItem{Title: title, URL: url})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nav
|
return nav
|
||||||
}
|
}
|
||||||
|
|
||||||
func generatePages(files []os.FileInfo, contentDir, outputDir string, tpl *template.Template, nav []NavItem) {
|
func generateHTML(file os.FileInfo, contentDir, outputDir string, tpl *template.Template, nav []NavItem) error {
|
||||||
for _, file := range files {
|
name := strings.TrimSuffix(file.Name(), filepath.Ext(file.Name()))
|
||||||
if filepath.Ext(file.Name()) == ".md" {
|
title := strings.Title(name)
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
htmlContent := blackfriday.Run(mdContent)
|
mdPath := filepath.Join(contentDir, file.Name())
|
||||||
outFile := strings.TrimSuffix(file.Name(), filepath.Ext(file.Name())) + ".html"
|
mdContent, err := ioutil.ReadFile(mdPath)
|
||||||
outPath := filepath.Join(outputDir, outFile)
|
if err != nil {
|
||||||
out, err := os.Create(outPath)
|
return fmt.Errorf("failed to read %s: %w", file.Name(), err)
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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>
|
<nav>
|
||||||
<ul>
|
<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>
|
</ul>
|
||||||
</nav>
|
</nav>
|
||||||
@@ -11,9 +11,9 @@
|
|||||||
<nav>
|
<nav>
|
||||||
<ul>
|
<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>
|
</ul>
|
||||||
</nav>
|
</nav>
|
||||||
|
|||||||
Reference in New Issue
Block a user