Began implementing YAML metadata
This commit is contained in:
parent
7915a4bb09
commit
68d2ddb692
6 changed files with 108 additions and 16 deletions
5
go.mod
5
go.mod
|
@ -2,4 +2,7 @@ module github.com/ficcdaf/zona
|
||||||
|
|
||||||
go 1.23.2
|
go 1.23.2
|
||||||
|
|
||||||
require github.com/gomarkdown/markdown v0.0.0-20241105142532-d03b89096d81 // indirect
|
require (
|
||||||
|
github.com/gomarkdown/markdown v0.0.0-20241105142532-d03b89096d81
|
||||||
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
|
)
|
||||||
|
|
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)
|
doc := p.Parse(md)
|
||||||
|
|
||||||
// build HTML renderer
|
// build HTML renderer
|
||||||
htmlFlags := html.CommonFlags | html.HrefTargetBlank | html.CompletePage
|
htmlFlags := html.CommonFlags | html.HrefTargetBlank
|
||||||
opts := html.RendererOptions{Flags: htmlFlags}
|
opts := html.RendererOptions{Flags: htmlFlags}
|
||||||
renderer := newZonaRenderer(opts)
|
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 {
|
func ChangeExtension(in string, outExt string) string {
|
||||||
return strings.TrimSuffix(in, filepath.Ext(in)) + outExt
|
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() {
|
if !entry.IsDir() {
|
||||||
ext := filepath.Ext(inPath)
|
ext := filepath.Ext(inPath)
|
||||||
outPath := replaceRoot(inPath, outRoot)
|
outPath := replaceRoot(inPath, outRoot)
|
||||||
// fmt.Println("NewRoot: ", outPath)
|
|
||||||
switch ext {
|
switch ext {
|
||||||
case ".md":
|
case ".md":
|
||||||
fmt.Println("Processing markdown...")
|
fmt.Println("Processing markdown...")
|
||||||
|
|
28
templates/article.html
Normal file
28
templates/article.html
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
<!doctype html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>{{ .Title }}</title>
|
||||||
|
<link rel="icon" href="{{ .Icon }}" type="image/x-icon" />
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
|
<meta charset="UTF-8" />
|
||||||
|
<link
|
||||||
|
href="{{ .Stylesheet }}"
|
||||||
|
rel="stylesheet"
|
||||||
|
type="text/css"
|
||||||
|
media="all"
|
||||||
|
/>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="container">
|
||||||
|
<header id="header">{{ .Header }}</header>
|
||||||
|
<article id="content">
|
||||||
|
{{ .Content }}
|
||||||
|
<nav id="nextprev">
|
||||||
|
{{ .NextPost }}<br />
|
||||||
|
{{ .PrevPost }}
|
||||||
|
</nav>
|
||||||
|
</article>
|
||||||
|
<footer id="footer">{{ .Footer }}</footer>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
10
test/yamltest.md
Normal file
10
test/yamltest.md
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
---
|
||||||
|
title: Yaml testing file
|
||||||
|
---
|
||||||
|
|
||||||
|
# My amazing markdown file!
|
||||||
|
|
||||||
|
I can _even_ do **this**!
|
||||||
|
|
||||||
|
- Or, I could...
|
||||||
|
- [Link](page.md) to this file
|
Loading…
Add table
Add a link
Reference in a new issue