init render with markdown

main
sudo pacman -Syu 2021-11-30 22:17:09 +07:00
parent dbce3db5cf
commit 098beac960
5 changed files with 106 additions and 0 deletions

14
.gitignore vendored Normal file
View File

@ -0,0 +1,14 @@
# macOS
.DS_Store
# Window
*.exe
# IntelliJ
.idea/
# VSCode
.vscode/
# HTML
generated/

18
custom/head.html Normal file
View File

@ -0,0 +1,18 @@
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=Recursive:slnt,wght,CASL,CRSV,MONO@-15..0,300..800,0..1,0..1,0..1&display=swap"
rel="stylesheet"
/>
<style>
body {
font-family: "Recursive", sans-serif;
font-variation-settings: "MONO" 0, "CASL" 1;
}
code {
font-family: "Recursive", monospace;
font-variation-settings: "MONO" 1, "CASL" 1;
}
</style>

5
go.mod Normal file
View File

@ -0,0 +1,5 @@
module github.com/haunt98/posts-go
go 1.17
require github.com/gomarkdown/markdown v0.0.0-20211128002848-ab7d7bf476b3 // indirect

2
go.sum Normal file
View File

@ -0,0 +1,2 @@
github.com/gomarkdown/markdown v0.0.0-20211128002848-ab7d7bf476b3 h1:TyhCPRkxLfBBOuOy8oambova4pG4Iivc0lTdTrkhN98=
github.com/gomarkdown/markdown v0.0.0-20211128002848-ab7d7bf476b3/go.mod h1:JDGcbDT52eL4fju3sZ4TeHGsQwhG9nbDV21aMyhwPoA=

67
main.go Normal file
View File

@ -0,0 +1,67 @@
package main
import (
"log"
"os"
"path/filepath"
"strings"
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/html"
)
const (
postsPath = "posts"
headHTMLPath = "custom/head.html"
generatedPath = "generated"
htmlExt = ".html"
)
func main() {
headHTML, err := os.ReadFile(headHTMLPath)
if err != nil {
log.Fatalln("Failed to read file", headHTML)
}
files, err := os.ReadDir(postsPath)
if err != nil {
log.Fatalln("Failed to read dir", postsPath)
}
if err := os.RemoveAll(generatedPath); err != nil {
log.Fatalln("Failed to remove all", generatedPath, err)
}
if err := os.MkdirAll(generatedPath, 0777); err != nil {
log.Fatalln("Failed to mkdir all", generatedPath)
}
for _, file := range files {
if file.IsDir() {
continue
}
filePath := filepath.Join(postsPath, file.Name())
md, err := os.ReadFile(filePath)
if err != nil {
log.Fatalln("Failed to read file", filePath)
}
htmlFlags := html.CommonFlags | html.CompletePage | html.TOC
htmlRendererOtps := html.RendererOptions{
Title: file.Name(),
Head: headHTML,
Flags: htmlFlags,
}
htmlRenderer := html.NewRenderer(htmlRendererOtps)
generatedHTML := markdown.ToHTML(md, nil, htmlRenderer)
generatedFileName := strings.TrimSuffix(file.Name(), filepath.Ext(file.Name())) + htmlExt
generatedFilePath := filepath.Join(generatedPath, generatedFileName)
if err := os.WriteFile(generatedFilePath, generatedHTML, 0666); err != nil {
log.Fatalln("Failed to write file", generatedFilePath, err)
}
}
}