Add Markdown HTML publication
This commit is contained in:
57
internal/transform/markdown/markdown.go
Normal file
57
internal/transform/markdown/markdown.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package markdown
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/yuin/goldmark"
|
||||
|
||||
"gitea.maximumdirect.net/eric/distributor/internal/bundle"
|
||||
"gitea.maximumdirect.net/eric/distributor/internal/storage"
|
||||
"gitea.maximumdirect.net/eric/distributor/internal/transform"
|
||||
)
|
||||
|
||||
type Transformer struct {
|
||||
renderer goldmark.Markdown
|
||||
}
|
||||
|
||||
func New() *Transformer {
|
||||
return &Transformer{renderer: goldmark.New()}
|
||||
}
|
||||
|
||||
func (t *Transformer) Generate(ctx context.Context, req transform.Request) ([]transform.Output, error) {
|
||||
if t.renderer == nil {
|
||||
t.renderer = goldmark.New()
|
||||
}
|
||||
var outputs []transform.Output
|
||||
for _, file := range req.SourceBundle.Manifest.Files {
|
||||
if !strings.HasSuffix(file.Path, ".md") {
|
||||
continue
|
||||
}
|
||||
sourcePath, err := storage.Join(req.SourceBundle.RootRelativePath, file.Path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
data, err := req.SourceBackend.ReadFile(ctx, sourcePath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read markdown source %q: %w", file.Path, err)
|
||||
}
|
||||
var rendered bytes.Buffer
|
||||
if err := t.renderer.Convert(data, &rendered); err != nil {
|
||||
return nil, fmt.Errorf("render markdown source %q: %w", file.Path, err)
|
||||
}
|
||||
html := wrapHTML(rendered.Bytes())
|
||||
outputPath := strings.TrimSuffix(file.Path, ".md") + ".html"
|
||||
outputs = append(outputs, transform.Output{
|
||||
Path: outputPath,
|
||||
SourcePath: file.Path,
|
||||
Transform: transform.MarkdownToHTML,
|
||||
Data: html,
|
||||
SHA256: bundle.FileDigest(html),
|
||||
Size: int64(len(html)),
|
||||
})
|
||||
}
|
||||
return outputs, nil
|
||||
}
|
||||
113
internal/transform/markdown/markdown_test.go
Normal file
113
internal/transform/markdown/markdown_test.go
Normal file
@@ -0,0 +1,113 @@
|
||||
package markdown
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gitea.maximumdirect.net/eric/distributor/internal/bundle"
|
||||
"gitea.maximumdirect.net/eric/distributor/internal/storage"
|
||||
"gitea.maximumdirect.net/eric/distributor/internal/storage/fake"
|
||||
"gitea.maximumdirect.net/eric/distributor/internal/transform"
|
||||
)
|
||||
|
||||
func TestGenerateMarkdownSidecar(t *testing.T) {
|
||||
backend, sourceBundle := markdownFixture(t, "# Title\n\nHello.\n")
|
||||
outputs, err := New().Generate(context.Background(), transform.Request{SourceBackend: backend, SourceBundle: sourceBundle})
|
||||
if err != nil {
|
||||
t.Fatalf("Generate() error = %v", err)
|
||||
}
|
||||
if got, want := len(outputs), 1; got != want {
|
||||
t.Fatalf("output count = %d, want %d", got, want)
|
||||
}
|
||||
output := outputs[0]
|
||||
if output.Path != "report.html" {
|
||||
t.Fatalf("path = %q, want report.html", output.Path)
|
||||
}
|
||||
if output.SourcePath != "report.md" || output.Transform != transform.MarkdownToHTML {
|
||||
t.Fatalf("metadata = %#v", output)
|
||||
}
|
||||
html := string(output.Data)
|
||||
for _, want := range []string{"<!doctype html>", "<h1>Title</h1>", "<p>Hello.</p>"} {
|
||||
if !strings.Contains(html, want) {
|
||||
t.Fatalf("html = %q, want substring %q", html, want)
|
||||
}
|
||||
}
|
||||
if output.SHA256 != bundle.FileDigest(output.Data) || output.Size != int64(len(output.Data)) {
|
||||
t.Fatalf("digest/size metadata = %s/%d", output.SHA256, output.Size)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateIgnoresNonMarkdown(t *testing.T) {
|
||||
backend := fake.New()
|
||||
if _, err := backend.WriteFile(context.Background(), "summary.txt", []byte("Summary\n"), storage.WriteOptions{}); err != nil {
|
||||
t.Fatalf("WriteFile() error = %v", err)
|
||||
}
|
||||
sourceBundle := bundle.Bundle{Manifest: bundle.Manifest{
|
||||
SchemaVersion: 1,
|
||||
ID: "bundle",
|
||||
Created: time.Date(2026, 5, 30, 11, 10, 0, 0, time.UTC),
|
||||
Files: []bundle.ManifestFile{{Path: "summary.txt", SHA256: bundle.FileDigest([]byte("Summary\n")), Size: 8}},
|
||||
}}
|
||||
outputs, err := New().Generate(context.Background(), transform.Request{SourceBackend: backend, SourceBundle: sourceBundle})
|
||||
if err != nil {
|
||||
t.Fatalf("Generate() error = %v", err)
|
||||
}
|
||||
if len(outputs) != 0 {
|
||||
t.Fatalf("outputs = %#v, want none", outputs)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateDoesNotPassRawHTML(t *testing.T) {
|
||||
backend, sourceBundle := markdownFixture(t, "# Title\n\n<script>alert('x')</script>\n")
|
||||
outputs, err := New().Generate(context.Background(), transform.Request{SourceBackend: backend, SourceBundle: sourceBundle})
|
||||
if err != nil {
|
||||
t.Fatalf("Generate() error = %v", err)
|
||||
}
|
||||
html := string(outputs[0].Data)
|
||||
if strings.Contains(html, "<script>") {
|
||||
t.Fatalf("html contains raw script: %q", html)
|
||||
}
|
||||
if !strings.Contains(html, "raw HTML omitted") && !strings.Contains(html, "<script>") {
|
||||
t.Fatalf("html = %q, want raw HTML disabled or escaped", html)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateDeterministicOutput(t *testing.T) {
|
||||
backend, sourceBundle := markdownFixture(t, "# Title\n\nHello.\n")
|
||||
first, err := New().Generate(context.Background(), transform.Request{SourceBackend: backend, SourceBundle: sourceBundle})
|
||||
if err != nil {
|
||||
t.Fatalf("first Generate() error = %v", err)
|
||||
}
|
||||
second, err := New().Generate(context.Background(), transform.Request{SourceBackend: backend, SourceBundle: sourceBundle})
|
||||
if err != nil {
|
||||
t.Fatalf("second Generate() error = %v", err)
|
||||
}
|
||||
if string(first[0].Data) != string(second[0].Data) {
|
||||
t.Fatalf("outputs differ:\n%s\n%s", first[0].Data, second[0].Data)
|
||||
}
|
||||
}
|
||||
|
||||
func markdownFixture(t *testing.T, markdown string) (*fake.Backend, bundle.Bundle) {
|
||||
t.Helper()
|
||||
backend := fake.New()
|
||||
if _, err := backend.WriteFile(context.Background(), "report.md", []byte(markdown), storage.WriteOptions{}); err != nil {
|
||||
t.Fatalf("WriteFile() error = %v", err)
|
||||
}
|
||||
if _, err := backend.WriteFile(context.Background(), "summary.txt", []byte("Summary\n"), storage.WriteOptions{}); err != nil {
|
||||
t.Fatalf("WriteFile() error = %v", err)
|
||||
}
|
||||
files := []bundle.ManifestFile{
|
||||
{Path: "report.md", SHA256: bundle.FileDigest([]byte(markdown)), Size: int64(len(markdown))},
|
||||
{Path: "summary.txt", SHA256: bundle.FileDigest([]byte("Summary\n")), Size: 8},
|
||||
}
|
||||
manifest := bundle.Manifest{
|
||||
SchemaVersion: 1,
|
||||
ID: "bundle",
|
||||
Created: time.Date(2026, 5, 30, 11, 10, 0, 0, time.UTC),
|
||||
Files: files,
|
||||
}
|
||||
manifest.Digest = bundle.BundleDigest(manifest.Files)
|
||||
return backend, bundle.Bundle{Manifest: manifest}
|
||||
}
|
||||
11
internal/transform/markdown/template.go
Normal file
11
internal/transform/markdown/template.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package markdown
|
||||
|
||||
import "bytes"
|
||||
|
||||
func wrapHTML(body []byte) []byte {
|
||||
var buf bytes.Buffer
|
||||
buf.WriteString("<!doctype html>\n<html lang=\"en\">\n<head>\n<meta charset=\"utf-8\">\n<title></title>\n</head>\n<body>\n")
|
||||
buf.Write(body)
|
||||
buf.WriteString("</body>\n</html>\n")
|
||||
return buf.Bytes()
|
||||
}
|
||||
3
internal/transform/plan.go
Normal file
3
internal/transform/plan.go
Normal file
@@ -0,0 +1,3 @@
|
||||
package transform
|
||||
|
||||
const MarkdownToHTML = "markdown_to_html"
|
||||
30
internal/transform/registry.go
Normal file
30
internal/transform/registry.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package transform
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Registry struct {
|
||||
transformers map[string]Transformer
|
||||
}
|
||||
|
||||
func NewRegistry() *Registry {
|
||||
return &Registry{transformers: make(map[string]Transformer)}
|
||||
}
|
||||
|
||||
func (r *Registry) Register(name string, transformer Transformer) error {
|
||||
if name == "" {
|
||||
return fmt.Errorf("transform name is required")
|
||||
}
|
||||
if transformer == nil {
|
||||
return fmt.Errorf("transformer %s is nil", name)
|
||||
}
|
||||
if _, exists := r.transformers[name]; exists {
|
||||
return fmt.Errorf("transform %s is already registered", name)
|
||||
}
|
||||
r.transformers[name] = transformer
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Registry) Get(name string) (Transformer, bool) {
|
||||
transformer, ok := r.transformers[name]
|
||||
return transformer, ok
|
||||
}
|
||||
26
internal/transform/transform.go
Normal file
26
internal/transform/transform.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package transform
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gitea.maximumdirect.net/eric/distributor/internal/bundle"
|
||||
"gitea.maximumdirect.net/eric/distributor/internal/storage"
|
||||
)
|
||||
|
||||
type Output struct {
|
||||
Path string
|
||||
SourcePath string
|
||||
Transform string
|
||||
Data []byte
|
||||
SHA256 string
|
||||
Size int64
|
||||
}
|
||||
|
||||
type Request struct {
|
||||
SourceBundle bundle.Bundle
|
||||
SourceBackend storage.Backend
|
||||
}
|
||||
|
||||
type Transformer interface {
|
||||
Generate(ctx context.Context, req Request) ([]Output, error)
|
||||
}
|
||||
Reference in New Issue
Block a user