all repos — website @ c6917416ca0dcc36af98b7039fd19c57d8dcedb5

My website

prettify HTML

Alan Pearce
commit

c6917416ca0dcc36af98b7039fd19c57d8dcedb5

parent

100034677d2b4c4678c050ab08889b581c889840

1 file changed, 13 insertions(+), 10 deletions(-)

changed files
M cmd/build/build.gocmd/build/build.go
@@ -21,15 +21,16 @@ "website/internal/config"
"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 @@ outputReplacer := strings.NewReplacer(root, outputDir, ".md", "/index.html")
urlReplacer := strings.NewReplacer(root, "", ".md", "/") markdown := goldmark.New( goldmark.WithRendererOptions( - html.WithUnsafe(), + htmlrenderer.WithUnsafe(), ), goldmark.WithExtensions( extension.GFM,
@@ -154,18 +155,20 @@ return doc, nil
} 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 @@ doc.Find("body").AddClass("h-card")
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 @@ return os.MkdirAll(path.Join(dirs...), 0755)
} 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") }