prosemirror: a Ruby gem to render and import ProseMirror and Tiptap documents

Hi all,

I’ve just released 1.0 of prosemirror, a Ruby gem for apps that store ProseMirror or Tiptap documents and need to work with them on the server:

doc = Prosemirror.parse(record.content)   # the JSON your editor saves
doc.to_html                               # or to_text, to_markdown
Prosemirror.from_html(html).to_h          # import HTML (or Markdown, or text) into a document

It follows ProseMirror’s model closely: schemas with content expressions, marks with excludes, parse rules ported from DOMParser (context, getAttrs, contentElement, preserveWhitespace…), and a Markdown serializer ported from prosemirror-markdown. Importing HTML gives the same JSON as Tiptap’s generateJSON on its test fixtures, so documents imported on the server open in the editor unchanged.

A few things that might be useful if you have a Ruby backend:

  • One schema for rendering, import and export. A custom node type is a Ruby class that declares its attributes, its content, its HTML and its parse rules once.
  • Safe HTML by construction. HTML is built with Phlex and escaped as it’s written, attributes are typed, and link and image URLs must use an allowed scheme. Nothing is cleaned up afterwards.
  • Invalid documents don’t break the page. Bad attributes are fixed or dropped, missing required content is filled in, and each problem is reported, or raised in strict mode.
  • Two built-in schemas: Tiptap’s StarterKit with tables, task lists, details, mentions, embeds and text alignment, and ProseMirror’s own prosemirror-schema-basic and prosemirror-schema-list.

It’s MIT licensed and has no Rails dependency: bundle add prosemirror (RubyGems). It replaces two older gems, prosemirror_to_html and html_to_prosemirror, which I maintained and have now deprecated.

Feedback is welcome, especially from people who know ProseMirror’s internals better than I do. Issues on GitHub, or here.