Skip to content

feat: Return structured result with content/dimensions (no SVG wrapper) #10

@davefowler

Description

@davefowler

Summary

Add an option to return rendered content without the SVG wrapper, along with dimensions. This allows callers to compose multiple mdsvg outputs into a larger SVG without regex-stripping wrappers.

Use Case

When embedding mdsvg output in larger SVG compositions, callers need to:

  1. Extract the inner content (currently requires regex)
  2. Know the actual rendered dimensions

Current workaround:

# Hacky regex extraction in dataface
content_svg = mdsvg.render(markdown, width=400)
inner_match = re.search(r"<svg[^>]*>(.*)</svg>", content_svg, re.DOTALL)
markdown_content = inner_match.group(1)

# Also need to extract height via regex
height_match = re.search(r'height="([0-9.]+)"', content_svg)
height = float(height_match.group(1))

Proposed API

Option A: Structured result type

from mdsvg import render_content, RenderResult

result: RenderResult = render_content("# Hello", width=400)
result.content  # SVG elements without <svg> wrapper
result.width    # 400.0
result.height   # Actual rendered height
result.to_svg() # Full SVG with wrapper (convenience method)

Option B: Parameter to skip wrapper

# Returns just the inner content
content = render("# Hello", width=400, wrap=False)

# Separate function to get dimensions
dims = get_dimensions("# Hello", width=400)
dims.width, dims.height

Recommendation

Option A (structured result) is cleaner - it's the pattern we just implemented in dataface's LayoutResult and works well.

@cursor Instructions

  1. Create a RenderResult dataclass with content, width, height fields
  2. Add to_svg() method that wraps content in SVG element
  3. Add render_content() function that returns RenderResult
  4. Keep existing render() function unchanged for backward compatibility
  5. Add tests for the new API
  6. Update README with examples

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions