fixed title casing
This commit is contained in:
parent
ff1357c8da
commit
12ebba687b
7 changed files with 44 additions and 14 deletions
|
@ -4,7 +4,6 @@ import (
|
|||
"bytes"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
|
@ -42,17 +41,10 @@ func processWithYaml(f []byte) (Metadata, []byte, error) {
|
|||
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 {
|
||||
p := &PageData{}
|
||||
if title, ok := m["title"].(string); ok {
|
||||
p.Title = title
|
||||
p.Title = wordsToTitle(title)
|
||||
} else {
|
||||
p.Title = pathToTitle(path)
|
||||
}
|
||||
|
@ -89,6 +81,7 @@ func ConvertFile(in string, out string) error {
|
|||
return err
|
||||
}
|
||||
pd := buildPageData(metadata, in)
|
||||
fmt.Println("Title: ", pd.Title)
|
||||
|
||||
// build according to template here
|
||||
html, err := MdToHTML(md)
|
||||
|
|
|
@ -42,11 +42,11 @@ func PathIsValid(path string, requireFile bool) bool {
|
|||
}
|
||||
|
||||
func processLink(p string) string {
|
||||
fmt.Println("Processing link...")
|
||||
// fmt.Println("Processing link...")
|
||||
ext := filepath.Ext(p)
|
||||
// Only process if it points to an existing, local markdown file
|
||||
if ext == ".md" && filepath.IsLocal(p) {
|
||||
fmt.Println("Markdown link detected...")
|
||||
// fmt.Println("Markdown link detected...")
|
||||
return ChangeExtension(p, ".html")
|
||||
} else {
|
||||
return p
|
||||
|
|
30
internal/build/title.go
Normal file
30
internal/build/title.go
Normal 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)
|
||||
}
|
|
@ -2,7 +2,6 @@ package build
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"path/filepath"
|
||||
|
||||
|
@ -18,7 +17,7 @@ func processFile(inPath string, entry fs.DirEntry, err error, outRoot string) er
|
|||
outPath := util.ReplaceRoot(inPath, outRoot)
|
||||
switch ext {
|
||||
case ".md":
|
||||
fmt.Println("Processing markdown...")
|
||||
// fmt.Println("Processing markdown...")
|
||||
outPath = ChangeExtension(outPath, ".html")
|
||||
if err := util.CreateParents(outPath); err != nil {
|
||||
return err
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue