# app/models/marksmith/renderer.rb

module Marksmith
  class Renderer
    def initialize(body:)
      @body = body
    end

    def render
      if Marksmith.configuration.parser == "commonmarker"
        render_commonmarker
      elsif Marksmith.configuration.parser == "kramdown"
        render_kramdown
      else
        render_redcarpet
      end
    end

    def render_commonmarker
      # commonmarker expects an utf-8 encoded string
      body = @body.to_s.dup.force_encoding("utf-8")
      Commonmarker.to_html(body)
    end

    def render_redcarpet
      ::Redcarpet::Markdown.new(
        ::Redcarpet::Render::HTML,
        tables: true,
        lax_spacing: true,
        fenced_code_blocks: true,
        space_after_headers: true,
        hard_wrap: true,
        autolink: true,
        strikethrough: true,
        underline: true,
        highlight: true,
        quote: true,
        with_toc_data: true
      ).render(@body)
    end

    def render_kramdown
      body = @body.to_s.dup.force_encoding("utf-8")
      Kramdown::Document.new(body).to_html
    end
  end
end
```

<Option name="`media_library`">

Controls the visibility of the **"Attach from gallery"** option in the markdown editor.

##### Default value

`true`

#### Possible values

- `true`
- `false`

#### Code example

```ruby
field :body, as: :markdown, media_library: false
```
</Option>

<Option name="`file_uploads`">

Controls the visibility of the **"Upload files"** option in the markdown editor.

##### Default value

`true`

#### Possible values

- `true`
- `false`

#### Code example

```ruby
field :body, as: :markdown, file_uploads: false
```
</Option>

<Option name="`extra_preview_params`">

Sends additional parameters to the **preview renderer** of the markdown field.
Useful for injecting context-specific data during preview rendering.

##### Default value

`{}`

#### Possible values

- Any `Hash` of key-value pairs.

#### Code example

```ruby
field :body, as: :markdown, extra_preview_params: { foo: :bar }
```
</Option>

---