nseaSeb
Coelho - Structured rich text for Phoenix
I’ve just published Coelho, a structured rich text library for Phoenix.
The idea is somewhat inspired by Rails’ Action Text: provide a proper rich text layer between the editor in the browser and the application’s data model.
But instead of storing HTML, Coelho stores the document as a structured JSON tree — the same kind of document model used by ProseMirror.
That gives the application a single source of truth for both the editor and the server:
-
define the document schema once in Elixir
-
export the schema to ProseMirror
-
validate documents before they reach the database
-
store the document directly in a
jsonbcolumn -
render it to HTML or extract plain text
-
customise rendering without changing the stored document
-
use different, restricted schemas for different rich text fields
-
validate and cast rich text directly through Ecto
-
use it from Phoenix LiveView with an included editor
-
support attachments with URLs resolved at render time
One of the things I particularly wanted to avoid is treating HTML as the data model.
When rich text is stored as HTML, the markup becomes part of the persisted data. With a structured document instead, the data remains queryable and migratable, while rendering stays an application-level decision.
Coelho also deliberately keeps the core small: no file storage, image processing or collaborative editing. Those are integration points rather than responsibilities of the document layer.
It’s early, but the core is already in place and tested.
I’d particularly love feedback from people building Phoenix applications that need rich text, especially if you’ve used Action Text or another structured rich text solution before.
HexDocs: Coelho — coelho v0.5.0
Hex.pm: coelho | Hex
Trending in Announcing
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
widianto
Thank you! Bookmarked it.
olivermt
Great stuff, but why not just use the existing standard for storage?
Prosemirror has been out for years and a lot of popular editors like Tiptap uses it.
nseaSeb
Hey, thank you.
That’s what it stores. The column holds the tree ProseMirror’s toJSON() produces, in a jsonb column:
{“type”:“doc”,“content”:[{“type”:“paragraph”,“content”:[
{“type”:“text”,“text”:“hello”,“marks”:[{“type”:“bold”}]}]}]}
No struct wraps it, so what is validated is what is stored and a jsonb round trip is the identity. And the browser half of Coelho is ProseMirror, used directly rather than reimplemented — prosemirror-model, -state, -view, -keymap, -commands, -history and -schema-list are its peer dependencies. A Tiptap document goes in and comes back out unchanged.
What’s worth pulling apart is what “the ProseMirror format” is and isn’t. It isn’t self-describing the way Markdown or HTML is: a ProseMirror document means nothing without the schema it was created under. {“type”: “callout”} is a perfectly good node or a fatal one depending entirely on which schema is in scope. So “just store the ProseMirror format” leaves open the only question a server actually has to answer, which is whether this particular document is one the application permits.
Coelho’s answer is to write that schema once, in Elixir, and export it with Coelho.Schema.to_json/1 for the browser to build its ProseMirror schema from — nodes and marks as ordered pairs rather than an object, because node order decides default types in ProseMirror and JSON object key order doesn’t survive a round trip. There’s a check in the demo that feeds the JSON Elixir actually emits to the browser’s real buildSchema and asserts a valid ProseMirror schema comes out the other end.
What that buys:
So it’s less a competing format than the half of ProseMirror that doesn’t ship. The document model exists in JS; what’s missing in a Phoenix app is the same model on the server, which is what turns the column into something you can validate, query, migrate and render instead of something you have to trust. If you already have HTML from another editor, Coelho.HTML.from_html/2 is the way in.
olivermt
Excellent! Will give it a closer look
nseaSeb
Perhaps the documentation or the readme file isn’t clear?