From c6917416ca0dcc36af98b7039fd19c57d8dcedb5 Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Thu, 18 Apr 2024 12:31:48 +0200 Subject: prettify HTML --- cmd/build/build.go | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) (limited to 'cmd/build/build.go') 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("\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("\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") } -- cgit 1.4.1