fixed title casing

This commit is contained in:
Daniel Fichtinger 2024-11-25 14:15:33 -05:00
parent ff1357c8da
commit 12ebba687b
7 changed files with 44 additions and 14 deletions

2
go.mod
View file

@ -6,3 +6,5 @@ require (
github.com/gomarkdown/markdown v0.0.0-20241105142532-d03b89096d81 github.com/gomarkdown/markdown v0.0.0-20241105142532-d03b89096d81
gopkg.in/yaml.v3 v3.0.1 gopkg.in/yaml.v3 v3.0.1
) )
require golang.org/x/text v0.20.0 // indirect

View file

@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"html/template" "html/template"
"path/filepath"
"strings" "strings"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
@ -42,17 +41,10 @@ func processWithYaml(f []byte) (Metadata, []byte, error) {
return meta, []byte(split[2]), nil return meta, []byte(split[2]), nil
} }
// this function converts a file path to its title form
func pathToTitle(path string) string {
stripped := ChangeExtension(filepath.Base(path), "")
replaced := strings.NewReplacer("-", " ", "_", " ", `\ `, " ").Replace(stripped)
return strings.ToTitle(replaced)
}
func buildPageData(m Metadata, path string) *PageData { func buildPageData(m Metadata, path string) *PageData {
p := &PageData{} p := &PageData{}
if title, ok := m["title"].(string); ok { if title, ok := m["title"].(string); ok {
p.Title = title p.Title = wordsToTitle(title)
} else { } else {
p.Title = pathToTitle(path) p.Title = pathToTitle(path)
} }
@ -89,6 +81,7 @@ func ConvertFile(in string, out string) error {
return err return err
} }
pd := buildPageData(metadata, in) pd := buildPageData(metadata, in)
fmt.Println("Title: ", pd.Title)
// build according to template here // build according to template here
html, err := MdToHTML(md) html, err := MdToHTML(md)

View file

@ -42,11 +42,11 @@ func PathIsValid(path string, requireFile bool) bool {
} }
func processLink(p string) string { func processLink(p string) string {
fmt.Println("Processing link...") // fmt.Println("Processing link...")
ext := filepath.Ext(p) ext := filepath.Ext(p)
// Only process if it points to an existing, local markdown file // Only process if it points to an existing, local markdown file
if ext == ".md" && filepath.IsLocal(p) { if ext == ".md" && filepath.IsLocal(p) {
fmt.Println("Markdown link detected...") // fmt.Println("Markdown link detected...")
return ChangeExtension(p, ".html") return ChangeExtension(p, ".html")
} else { } else {
return p return p

30
internal/build/title.go Normal file
View file

@ -0,0 +1,30 @@
package build
import (
"path/filepath"
"strings"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
// pathToWords takes a full path
// and strips separators and extension
// from the file name
func pathToWords(path string) string {
stripped := ChangeExtension(filepath.Base(path), "")
replaced := strings.NewReplacer("-", " ", "_", " ", `\ `, " ").Replace(stripped)
return strings.ToTitle(replaced)
}
func wordsToTitle(words string) string {
caser := cases.Title(language.English)
return caser.String(words)
}
// pathToTitle converts a full path to a string
// in title case
func pathToTitle(path string) string {
words := pathToWords(path)
return wordsToTitle(words)
}

View file

@ -2,7 +2,6 @@ package build
import ( import (
"errors" "errors"
"fmt"
"io/fs" "io/fs"
"path/filepath" "path/filepath"
@ -18,7 +17,7 @@ func processFile(inPath string, entry fs.DirEntry, err error, outRoot string) er
outPath := util.ReplaceRoot(inPath, outRoot) outPath := util.ReplaceRoot(inPath, outRoot)
switch ext { switch ext {
case ".md": case ".md":
fmt.Println("Processing markdown...") // fmt.Println("Processing markdown...")
outPath = ChangeExtension(outPath, ".html") outPath = ChangeExtension(outPath, ".html")
if err := util.CreateParents(outPath); err != nil { if err := util.CreateParents(outPath); err != nil {
return err return err

View file

@ -5,4 +5,4 @@ fi
go run cmd/zona/main.go test go run cmd/zona/main.go test
bat foobar/in.html # bat foobar/in.html

View file

@ -0,0 +1,6 @@
# My amazing markdown file!
I can _even_ do **this**!
- Or, I could...
- [Link](page.md) to this file