Began implementing YAML metadata
This commit is contained in:
parent
7915a4bb09
commit
68d2ddb692
6 changed files with 108 additions and 16 deletions
65
internal/convert/build_page.go
Normal file
65
internal/convert/build_page.go
Normal file
|
@ -0,0 +1,65 @@
|
|||
package convert
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"strings"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type PageData struct {
|
||||
Title string
|
||||
Icon string
|
||||
Stylesheet string
|
||||
Header template.HTML
|
||||
Content template.HTML
|
||||
NextPost template.HTML
|
||||
PrevPost template.HTML
|
||||
Footer template.HTML
|
||||
}
|
||||
|
||||
func processWithYaml(f []byte) (Metadata, []byte, error) {
|
||||
// Check if the file has valid metadata
|
||||
if !bytes.HasPrefix(f, []byte("---\n")) {
|
||||
// No valid yaml, so return the entire content
|
||||
return nil, f, nil
|
||||
}
|
||||
// Separate YAML from rest of document
|
||||
split := strings.SplitN(string(f), "---\n", 3)
|
||||
if len(split) < 3 {
|
||||
return nil, nil, fmt.Errorf("Invalid frontmatter format.")
|
||||
}
|
||||
var metadata Metadata
|
||||
// Parse YAML
|
||||
if err := yaml.Unmarshal([]byte(split[1]), &metadata); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return metadata, []byte(split[2]), nil
|
||||
}
|
||||
|
||||
func ConvertFile(in string, out string) error {
|
||||
mdPre, err := ReadFile(in)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
metadata, md, err := processWithYaml(mdPre)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
title, ok := metadata["title"].(string)
|
||||
if !ok {
|
||||
fmt.Println("No title in page.")
|
||||
} else {
|
||||
fmt.Println("Title found: ", title)
|
||||
}
|
||||
|
||||
html, err := MdToHTML(md)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = WriteFile(html, out)
|
||||
return err
|
||||
}
|
|
@ -21,7 +21,7 @@ func MdToHTML(md []byte) ([]byte, error) {
|
|||
doc := p.Parse(md)
|
||||
|
||||
// build HTML renderer
|
||||
htmlFlags := html.CommonFlags | html.HrefTargetBlank | html.CompletePage
|
||||
htmlFlags := html.CommonFlags | html.HrefTargetBlank
|
||||
opts := html.RendererOptions{Flags: htmlFlags}
|
||||
renderer := newZonaRenderer(opts)
|
||||
|
||||
|
@ -131,19 +131,6 @@ func CopyFile(inPath string, outPath string) error {
|
|||
}
|
||||
}
|
||||
|
||||
func ConvertFile(in string, out string) error {
|
||||
md, err := ReadFile(in)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
html, err := MdToHTML(md)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = WriteFile(html, out)
|
||||
return err
|
||||
}
|
||||
|
||||
func ChangeExtension(in string, outExt string) string {
|
||||
return strings.TrimSuffix(in, filepath.Ext(in)) + outExt
|
||||
}
|
||||
|
|
|
@ -60,7 +60,6 @@ func processFile(inPath string, entry fs.DirEntry, err error, outRoot string) er
|
|||
if !entry.IsDir() {
|
||||
ext := filepath.Ext(inPath)
|
||||
outPath := replaceRoot(inPath, outRoot)
|
||||
// fmt.Println("NewRoot: ", outPath)
|
||||
switch ext {
|
||||
case ".md":
|
||||
fmt.Println("Processing markdown...")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue