Skip to content

Bug/Feature: Partials not rendered correctly #69

@schneems

Description

@schneems

Original issue: #53 (comment)

Copying because it's an old one and we have a new reproduction. If someone fixes this in October I'll hacktober-accepted label the PR.

Reproduction:


From @joshuapinter

I finally got around to creating a simple example app that re-creates the issue of markdown (.md.erb) partials not rendering correctly in plain text. You can view the repo here:

https://github.com/joshuapinter/maildown_partials_issue

I've included the repro instructions in the README but here they are as well:

  1. Clone the repo and install gems via bundle.

  2. Run the server via bundle exec rails s

  3. Navigate to http://localhost:3000/rails/mailers/notifier_mailer/markdown_partial_test in your browser:

    Screen Shot 2022-09-21 at 10 11 14

    By default the HTML format will appear, and it appears correctly.

  4. Click on the format drop down and select "View as plain-text email".

    Screen Shot 2022-09-21 at 10 11 46

    You will see that the partial gets rendered incorrectly as HTML instead of plain text.

  5. To see what it should look like, navigate to http://localhost:3000/rails/mailers/notifier_mailer/non_markdown_partial_test.txt in your browser.

    Screen Shot 2022-09-21 at 10 12 02

    You will see that using two files for formats (html.erb and text.erb) and not using markdown (md.erb) via maildown renders the partials correctly for both HTML and plain text.

I did this in Rails 6 and Ruby 3 because that's what I had handy.

I hope this is enough to re-open this Issue and take another look. I've spent a few hours trying to figure it out myself and digging into how Rails renders partials but it's a bit over my head.

Let me know if you need anything from my side or if you want me to do some testing.

Rendering emails in markdown is incredible and I feel like this is the missing piece to make it truly clean, DRY and beautiful.


My analysis

How maildown works: It first renders ERB and then runs it through a Markdown parser to generate HTML, or just plain text to deliver the text/plain version. What I think is happening here is that the ERB in the partial is being evaluated as if it's an HTML partial no matter what.

I think there's two paths forward:

  • Find out where the mime type for the partial is being set and either monkeypatch to hook into Rails and set it, or through some other more durable mechanism.
  • If that doesn't work, the fallback question could be "can we determine which view is being rendered from within the view?" If so, then maybe the partial rendering method in Rails has some way to pass in extra content
  • If all of that fails, maybe there's still some way to workaround the problem that investigation might show. If that happens documenting it as a known workaround could be helpful.

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