about summary refs log tree commit diff stats
path: root/cmd/build
diff options
context:
space:
mode:
authorAlan Pearce2024-04-18 12:31:48 +0200
committerAlan Pearce2024-04-18 12:37:41 +0200
commitc6917416ca0dcc36af98b7039fd19c57d8dcedb5 (patch)
tree5caf8d8ab4fccbaf1be3ca1d878678660e5b6992 /cmd/build
parent100034677d2b4c4678c050ab08889b581c889840 (diff)
downloadwebsite-c6917416ca0dcc36af98b7039fd19c57d8dcedb5.tar.lz
website-c6917416ca0dcc36af98b7039fd19c57d8dcedb5.tar.zst
website-c6917416ca0dcc36af98b7039fd19c57d8dcedb5.zip
prettify HTML
Diffstat (limited to 'cmd/build')
-rw-r--r--cmd/build/build.go23
1 files changed, 13 insertions, 10 deletions
diff --git a/cmd/build/build.go b/cmd/build/build.go
index 0436c20..4254bf8 100644
--- a/cmd/build/build.go
+++ b/cmd/build/build.go
@@ -21,15 +21,16 @@ import (
 
 	"github.com/BurntSushi/toml"
 	"github.com/PuerkitoBio/goquery"
+	"github.com/a-h/htmlformat"
 	"github.com/adrg/frontmatter"
 	"github.com/antchfx/xmlquery"
 	"github.com/antchfx/xpath"
 	mapset "github.com/deckarep/golang-set/v2"
-	iohelper "github.com/jf-tech/go-corelib/ios"
 	"github.com/pkg/errors"
 	"github.com/yuin/goldmark"
 	"github.com/yuin/goldmark/extension"
-	"github.com/yuin/goldmark/renderer/html"
+	htmlrenderer "github.com/yuin/goldmark/renderer/html"
+	"golang.org/x/net/html"
 )
 
 type PostMatter struct {
@@ -79,7 +80,7 @@ func readPosts(root string, inputDir string, outputDir string) ([]Post, Tags, er
 	urlReplacer := strings.NewReplacer(root, "", ".md", "/")
 	markdown := goldmark.New(
 		goldmark.WithRendererOptions(
-			html.WithUnsafe(),
+			htmlrenderer.WithUnsafe(),
 		),
 		goldmark.WithExtensions(
 			extension.GFM,
@@ -154,18 +155,20 @@ func layout(filename string, config config.Config, pageTitle string) (*goquery.D
 }
 
 func renderHTML(doc *goquery.Document) io.Reader {
-	r := bytes.NewBufferString("<!doctype html>\n")
-	// hello go? HTML 5 does not have self-closing tags
-	replacer := iohelper.NewBytesReplacingReader(r, []byte("/>"), []byte(" >"))
+	r, w := io.Pipe()
 
 	// TODO: return errors to main thread
 	go func() {
-		err := goquery.Render(r, doc.Children())
+		w.Write([]byte("<!doctype html>\n"))
+		err := htmlformat.Nodes(w, []*html.Node{doc.Children().Get(0)})
 		if err != nil {
 			slog.Error("error rendering html", "error", err)
+			w.CloseWithError(err)
+			return
 		}
+		defer w.Close()
 	}()
-	return replacer
+	return r
 }
 
 func renderPost(post Post, config config.Config) (r io.Reader, err error) {
@@ -255,7 +258,7 @@ func renderHomepage(config config.Config, posts []Post) (io.Reader, error) {
 	doc.Find(".title").AddClass("p-name u-url")
 	var buf bytes.Buffer
 
-	md := goldmark.New(goldmark.WithRendererOptions(html.WithUnsafe()))
+	md := goldmark.New(goldmark.WithRendererOptions(htmlrenderer.WithUnsafe()))
 	if err := md.Convert(*index, &buf); err != nil {
 		return nil, err
 	}
@@ -386,7 +389,7 @@ func mkdirp(dirs ...string) error {
 }
 
 func outputToFile(output io.Reader, filename ...string) error {
-	file, err := os.OpenFile(path.Join(filename...), os.O_WRONLY|os.O_CREATE, 00644)
+	file, err := os.OpenFile(path.Join(filename...), os.O_WRONLY|os.O_CREATE, 0644)
 	if err != nil {
 		return errors.WithMessage(err, "could not open output file")
 	}