Add Markdown index HTML output mode
This commit is contained in:
@@ -25,24 +25,26 @@ func (t *Transformer) Generate(ctx context.Context, req transform.Request) ([]tr
|
||||
if t.renderer == nil {
|
||||
t.renderer = goldmark.New()
|
||||
}
|
||||
switch markdownMode(req.Markdown.Mode) {
|
||||
case transform.MarkdownModeSidecar:
|
||||
return t.generateSidecars(ctx, req)
|
||||
case transform.MarkdownModeIndex:
|
||||
return t.generateIndex(ctx, req)
|
||||
default:
|
||||
return nil, fmt.Errorf("markdown mode %q is not supported", req.Markdown.Mode)
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Transformer) generateSidecars(ctx context.Context, req transform.Request) ([]transform.Output, error) {
|
||||
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)
|
||||
html, err := t.render(ctx, req, 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,
|
||||
@@ -55,3 +57,78 @@ func (t *Transformer) Generate(ctx context.Context, req transform.Request) ([]tr
|
||||
}
|
||||
return outputs, nil
|
||||
}
|
||||
|
||||
func (t *Transformer) generateIndex(ctx context.Context, req transform.Request) ([]transform.Output, error) {
|
||||
input, err := selectIndexInput(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
html, err := t.render(ctx, req, input)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return []transform.Output{{
|
||||
Path: "index.html",
|
||||
SourcePath: input,
|
||||
Transform: transform.MarkdownToHTML,
|
||||
Data: html,
|
||||
SHA256: bundle.FileDigest(html),
|
||||
Size: int64(len(html)),
|
||||
}}, nil
|
||||
}
|
||||
|
||||
func selectIndexInput(req transform.Request) (string, error) {
|
||||
if req.Markdown.Input != "" {
|
||||
if err := storage.ValidatePath(req.Markdown.Input); err != nil {
|
||||
return "", fmt.Errorf("markdown input %q: %w", req.Markdown.Input, err)
|
||||
}
|
||||
for _, file := range req.SourceBundle.Manifest.Files {
|
||||
if file.Path != req.Markdown.Input {
|
||||
continue
|
||||
}
|
||||
if !strings.HasSuffix(file.Path, ".md") {
|
||||
return "", fmt.Errorf("markdown input %q must end in .md", req.Markdown.Input)
|
||||
}
|
||||
return file.Path, nil
|
||||
}
|
||||
return "", fmt.Errorf("markdown input %q is not listed in the source manifest", req.Markdown.Input)
|
||||
}
|
||||
|
||||
var markdownFiles []string
|
||||
for _, file := range req.SourceBundle.Manifest.Files {
|
||||
if strings.HasSuffix(file.Path, ".md") {
|
||||
markdownFiles = append(markdownFiles, file.Path)
|
||||
}
|
||||
}
|
||||
switch len(markdownFiles) {
|
||||
case 0:
|
||||
return "", fmt.Errorf("markdown index mode requires one markdown source file or transform.markdown_to_html.input")
|
||||
case 1:
|
||||
return markdownFiles[0], nil
|
||||
default:
|
||||
return "", fmt.Errorf("markdown index mode found multiple markdown source files; set transform.markdown_to_html.input")
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Transformer) render(ctx context.Context, req transform.Request, sourceFile string) ([]byte, error) {
|
||||
sourcePath, err := storage.Join(req.SourceBundle.RootRelativePath, sourceFile)
|
||||
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", sourceFile, err)
|
||||
}
|
||||
var rendered bytes.Buffer
|
||||
if err := t.renderer.Convert(data, &rendered); err != nil {
|
||||
return nil, fmt.Errorf("render markdown source %q: %w", sourceFile, err)
|
||||
}
|
||||
return wrapHTML(rendered.Bytes()), nil
|
||||
}
|
||||
|
||||
func markdownMode(mode string) string {
|
||||
if mode == "" {
|
||||
return transform.MarkdownModeSidecar
|
||||
}
|
||||
return mode
|
||||
}
|
||||
|
||||
@@ -40,6 +40,124 @@ func TestGenerateMarkdownSidecar(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateMarkdownIndexExplicitInput(t *testing.T) {
|
||||
backend := fake.New()
|
||||
sourceBundle := testutil.WriteFakeSourceBundle(t, backend, "", testutil.BundleOptions{
|
||||
ID: "bundle",
|
||||
Files: []testutil.SourceFile{
|
||||
{Path: "report.md", Data: "# Report\n"},
|
||||
{Path: "notes.md", Data: "# Notes\n"},
|
||||
},
|
||||
})
|
||||
|
||||
outputs, err := New().Generate(context.Background(), transform.Request{
|
||||
SourceBackend: backend,
|
||||
SourceBundle: sourceBundle,
|
||||
Markdown: transform.MarkdownOptions{Mode: transform.MarkdownModeIndex, Input: "notes.md"},
|
||||
})
|
||||
|
||||
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 != "index.html" || output.SourcePath != "notes.md" || output.Transform != transform.MarkdownToHTML {
|
||||
t.Fatalf("output metadata = %#v, want index from notes.md", output)
|
||||
}
|
||||
if !strings.Contains(string(output.Data), "<h1>Notes</h1>") {
|
||||
t.Fatalf("html = %q, want notes content", output.Data)
|
||||
}
|
||||
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 TestGenerateMarkdownIndexSelectsOnlyMarkdownFile(t *testing.T) {
|
||||
backend, sourceBundle := markdownFixture(t, "# Title\n\nHello.\n")
|
||||
|
||||
outputs, err := New().Generate(context.Background(), transform.Request{
|
||||
SourceBackend: backend,
|
||||
SourceBundle: sourceBundle,
|
||||
Markdown: transform.MarkdownOptions{Mode: transform.MarkdownModeIndex},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("Generate() error = %v", err)
|
||||
}
|
||||
if got, want := outputs[0].Path, "index.html"; got != want {
|
||||
t.Fatalf("path = %q, want %q", got, want)
|
||||
}
|
||||
if got, want := outputs[0].SourcePath, "report.md"; got != want {
|
||||
t.Fatalf("source path = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateMarkdownIndexRejectsAmbiguousInput(t *testing.T) {
|
||||
backend := fake.New()
|
||||
sourceBundle := testutil.WriteFakeSourceBundle(t, backend, "", testutil.BundleOptions{
|
||||
ID: "bundle",
|
||||
Files: []testutil.SourceFile{
|
||||
{Path: "report.md", Data: "# Report\n"},
|
||||
{Path: "notes.md", Data: "# Notes\n"},
|
||||
},
|
||||
})
|
||||
|
||||
_, err := New().Generate(context.Background(), transform.Request{
|
||||
SourceBackend: backend,
|
||||
SourceBundle: sourceBundle,
|
||||
Markdown: transform.MarkdownOptions{Mode: transform.MarkdownModeIndex},
|
||||
})
|
||||
|
||||
if err == nil || !strings.Contains(err.Error(), "multiple markdown source files") {
|
||||
t.Fatalf("Generate() error = %v, want ambiguous input error", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateMarkdownIndexRejectsMissingMarkdown(t *testing.T) {
|
||||
backend := fake.New()
|
||||
sourceBundle := testutil.WriteFakeSourceBundle(t, backend, "", testutil.BundleOptions{
|
||||
ID: "bundle",
|
||||
Files: []testutil.SourceFile{{Path: "summary.txt", Data: "Summary\n"}},
|
||||
})
|
||||
|
||||
_, err := New().Generate(context.Background(), transform.Request{
|
||||
SourceBackend: backend,
|
||||
SourceBundle: sourceBundle,
|
||||
Markdown: transform.MarkdownOptions{Mode: transform.MarkdownModeIndex},
|
||||
})
|
||||
|
||||
if err == nil || !strings.Contains(err.Error(), "requires one markdown source file") {
|
||||
t.Fatalf("Generate() error = %v, want missing markdown error", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateMarkdownIndexRejectsInvalidExplicitInput(t *testing.T) {
|
||||
backend, sourceBundle := markdownFixture(t, "# Title\n")
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
wantError string
|
||||
}{
|
||||
{name: "unsafe", input: "../report.md", wantError: "markdown input"},
|
||||
{name: "not listed", input: "missing.md", wantError: "not listed"},
|
||||
{name: "not markdown", input: "summary.txt", wantError: "must end in .md"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
_, err := New().Generate(context.Background(), transform.Request{
|
||||
SourceBackend: backend,
|
||||
SourceBundle: sourceBundle,
|
||||
Markdown: transform.MarkdownOptions{Mode: transform.MarkdownModeIndex, Input: tt.input},
|
||||
})
|
||||
if err == nil || !strings.Contains(err.Error(), tt.wantError) {
|
||||
t.Fatalf("Generate() error = %v, want substring %q", err, tt.wantError)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateIgnoresNonMarkdown(t *testing.T) {
|
||||
backend := fake.New()
|
||||
if _, err := backend.WriteFile(context.Background(), "summary.txt", []byte("Summary\n"), storage.WriteOptions{}); err != nil {
|
||||
|
||||
@@ -1,3 +1,8 @@
|
||||
package transform
|
||||
|
||||
const MarkdownToHTML = "markdown_to_html"
|
||||
|
||||
const (
|
||||
MarkdownModeSidecar = "sidecar"
|
||||
MarkdownModeIndex = "index"
|
||||
)
|
||||
|
||||
@@ -19,6 +19,12 @@ type Output struct {
|
||||
type Request struct {
|
||||
SourceBundle bundle.Bundle
|
||||
SourceBackend storage.Backend
|
||||
Markdown MarkdownOptions
|
||||
}
|
||||
|
||||
type MarkdownOptions struct {
|
||||
Mode string
|
||||
Input string
|
||||
}
|
||||
|
||||
type Transformer interface {
|
||||
|
||||
Reference in New Issue
Block a user