An open API service indexing awesome lists of open source software.

https://github.com/lordsynergy/editor_rails

Editor.js rendering and validation engine for Ruby and Rails
https://github.com/lordsynergy/editor_rails

content editorjs json rails renderer ruby wysiwyg

Last synced: about 1 year ago
JSON representation

Editor.js rendering and validation engine for Ruby and Rails

Awesome Lists containing this project

README

          

# EditorRails

[![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](LICENSE.txt)

EditorRails is a Ruby gem that brings [Editor.js](https://editorjs.io/) support to Rails applications.
It allows rendering JSON data generated by Editor.js into safe HTML and plain text.

---

## Features

- Support for structured content (Editor.js blocks)
- Built-in validation using JSON Schema
- Secure HTML output via escaping
- Configurable block rendering system
- No JavaScript dependencies required on the server side

---

## Installation

Add this line to your Gemfile:

```ruby
gem "editor_rails"
```

And then execute:

```bash
bundle install
```

---

## Usage

```
data = {
"time" => "123456789",
"blocks" => [
{ "type" => "paragraph", "data" => { "text" => "Hello world" } }
]
}
```

```bash
document = EditorRails::Document.new(data)
document.render(format: :html)
# => "

Hello <b>world</b>

"

document.render(format: :plain)
# => "Hello world"
```

---

## Configuration

The gem includes a default schema for the `paragraph` block, located at:

```
lib/editor_rails/schemas/
```

You can configure enabled block types and schema path:

```ruby
EditorRails.configure do |config|
config.enabled_blocks = %w[paragraph]
config.schemas_path = Rails.root.join("config/editor_schemas").to_s
end
```

Schemas should follow the JSON Schema format and be stored in YAML files.
Each block must have a schema named like paragraph.yml, header.yml, etc.

---

## Development

To run tests and linters:

```bash
bundle exec rake
```

#### Rendering Custom Blocks

To support a new block:

1. Create a class in EditorRails::Blocks namespace (e.g., Header)
2. Inherit from EditorRails::Blocks::Base
3. Implement render and plain
4. Add a corresponding schema

```ruby
module EditorRails
module Blocks
class Header < Base
def render
"

#{ERB::Util.html_escape(data["text"])}

"
end

def plain
data["text"].to_s
end
end
end
end
```

---

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/lordsynergy/editor_rails.

---

## License

This gem is licensed under the [MIT License](https://opensource.org/license/MIT).