prosemirror 0.7.1 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +15 -0
- data/COOKBOOK.md +35 -0
- data/README.md +66 -2
- data/TYPES.md +2 -2
- data/lib/prosemirror/checker.rb +1 -0
- data/lib/prosemirror/coerce.rb +4 -0
- data/lib/prosemirror/content_expression.rb +1 -0
- data/lib/prosemirror/content_match.rb +7 -0
- data/lib/prosemirror/extensions/text_align.rb +34 -0
- data/lib/prosemirror/html/builder.rb +30 -11
- data/lib/prosemirror/html/entities.rb +1 -0
- data/lib/prosemirror/html/selector.rb +1 -0
- data/lib/prosemirror/html/splitter.rb +17 -4
- data/lib/prosemirror/html_parser.rb +39 -14
- data/lib/prosemirror/json_parser.rb +2 -1
- data/lib/prosemirror/mark_type.rb +2 -0
- data/lib/prosemirror/markdown/builder.rb +2 -1
- data/lib/prosemirror/markdown_serializer.rb +10 -0
- data/lib/prosemirror/naming.rb +1 -0
- data/lib/prosemirror/node.rb +19 -4
- data/lib/prosemirror/node_type.rb +6 -2
- data/lib/prosemirror/nodes/bullet_list.rb +1 -0
- data/lib/prosemirror/nodes/code_block.rb +1 -0
- data/lib/prosemirror/nodes/image.rb +1 -0
- data/lib/prosemirror/nodes/mention.rb +1 -0
- data/lib/prosemirror/nodes/ordered_list.rb +3 -0
- data/lib/prosemirror/nodes/twitch.rb +3 -0
- data/lib/prosemirror/nodes/youtube.rb +2 -0
- data/lib/prosemirror/parse_rule.rb +1 -0
- data/lib/prosemirror/schema.rb +17 -5
- data/lib/prosemirror/text_serializer.rb +1 -0
- data/lib/prosemirror/transformer.rb +1 -0
- data/lib/prosemirror/type_definition.rb +14 -1
- data/lib/prosemirror/utf8.rb +1 -0
- data/lib/prosemirror/version.rb +1 -1
- data/lib/prosemirror.rb +7 -3
- metadata +9 -6
- data/ROADMAP.md +0 -224
- data/lib/prosemirror/text_align.rb +0 -31
data/ROADMAP.md
DELETED
|
@@ -1,224 +0,0 @@
|
|
|
1
|
-
# Roadmap
|
|
2
|
-
|
|
3
|
-
`prosemirror` is a Ruby gem for [ProseMirror](https://prosemirror.net) and [Tiptap](https://tiptap.dev) documents. It renders them to HTML or text, and imports HTML, Markdown or text into them.
|
|
4
|
-
|
|
5
|
-
Feedback is welcome in the issues.
|
|
6
|
-
|
|
7
|
-
## Why
|
|
8
|
-
|
|
9
|
-
ProseMirror editors store documents as JSON. Ruby apps need to render this JSON to HTML, extract its text, and import old HTML or Markdown content. Today, this is split across several small gems:
|
|
10
|
-
|
|
11
|
-
| Gem | Does | Downloads | Limits |
|
|
12
|
-
|---|---|---|---|
|
|
13
|
-
| [`prosemirror_to_html`](https://rubygems.org/gems/prosemirror_to_html) | JSON → HTML | 122k | Builds HTML strings, then cleans them with Loofah |
|
|
14
|
-
| [`html_to_prosemirror`](https://rubygems.org/gems/html_to_prosemirror) | HTML → JSON | 19k | Few node types, regex-based cleanup |
|
|
15
|
-
| [`prose_mirror_ruby`](https://rubygems.org/gems/prose_mirror_ruby) | JSON → Markdown | 78k | Needs ActiveSupport, no HTML |
|
|
16
|
-
| [`prose_render`](https://github.com/mattbarrry/prose_render) (unreleased) | JSON → HTML | — | Needs Rails and ViewComponent |
|
|
17
|
-
|
|
18
|
-
They use different type names (`bullet_list` or `bulletList`, `strong` or `bold`), and none of them uses the same schema for rendering and parsing. `prosemirror` replaces the first two, which have the same maintainers.
|
|
19
|
-
|
|
20
|
-
## Principles
|
|
21
|
-
|
|
22
|
-
- [Phlex](https://www.phlex.fun) builds the HTML and escapes text and attribute values. [Literal](https://literal.fun) types the attributes, so a heading level is an integer from 1 to 6. URLs must use an allowed scheme. Attribute names never come from the document. Nothing is cleaned up after rendering.
|
|
23
|
-
- Output is HTML5 only: no `<strike>`, `<font>` or `align=`. The importer still reads them.
|
|
24
|
-
- Bad data doesn't break the page. For an invalid attribute:
|
|
25
|
-
1. convert it when the meaning is clear (`"2"` becomes `2`);
|
|
26
|
-
2. otherwise, use its default (a heading with `level: "abc"` becomes an `h1`, and keeps its text);
|
|
27
|
-
3. if it has no default, remove what it belongs to: an image without a valid `src`, or a link with an invalid `href` (the text stays).
|
|
28
|
-
|
|
29
|
-
An unknown node renders its content. An `on_invalid` hook reports each problem, for example to an error tracker. A strict mode raises errors instead. `to_h` returns the corrected document.
|
|
30
|
-
- Only `phlex` and `literal` are required: no Rails, no Nokogiri. [`herb`](https://herb-tools.dev) (HTML) and [`commonmarker`](https://github.com/gjtorikian/commonmarker) (Markdown) are loaded only by the importers.
|
|
31
|
-
- Schemas, content expressions, marks and parse rules work as in [ProseMirror's guide](https://prosemirror.net/docs/guide/#schema), with the same JSON format. Methods keep ProseMirror's names in Ruby style (`text_content`, `descendants`, `check`).
|
|
32
|
-
- Nodes are immutable values that support pattern matching. Documents are `Enumerable`. Node types are classes.
|
|
33
|
-
- Each node type defines its attributes, its allowed content, its HTML output, and how to recognize it in HTML. Rendering and import use the same schema.
|
|
34
|
-
|
|
35
|
-
## API
|
|
36
|
-
|
|
37
|
-
```ruby
|
|
38
|
-
doc = Prosemirror.parse(record.content) # Hash or JSON string
|
|
39
|
-
doc = Prosemirror.from_html(html)
|
|
40
|
-
doc = Prosemirror.from_markdown(markdown)
|
|
41
|
-
doc = Prosemirror.from_text(text)
|
|
42
|
-
|
|
43
|
-
doc.to_html # => "<h2>Hello</h2><p>World</p>"
|
|
44
|
-
doc.to_text # => "Hello\n\nWorld"
|
|
45
|
-
doc.to_h # for a json/jsonb column
|
|
46
|
-
|
|
47
|
-
doc.select { |node| node in { type: :heading, attrs: { level: 1 } } }
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
### Custom nodes
|
|
51
|
-
|
|
52
|
-
A node type is a Phlex component. Its attributes are Literal properties. Its name comes from the class name (`ImageBlock` → `image_block`).
|
|
53
|
-
|
|
54
|
-
```ruby
|
|
55
|
-
class ImageBlock < Prosemirror::NodeType
|
|
56
|
-
group :block
|
|
57
|
-
|
|
58
|
-
prop :src, _String?
|
|
59
|
-
prop :alt, _String?
|
|
60
|
-
|
|
61
|
-
parse_html tag: "img[data-block]" do |element|
|
|
62
|
-
{ src: element["src"], alt: element["alt"] }
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
def view_template
|
|
66
|
-
figure { img(src: safe_url(@src), alt: @alt) }
|
|
67
|
-
end
|
|
68
|
-
end
|
|
69
|
-
|
|
70
|
-
schema = Prosemirror::Schema.tiptap.with(ImageBlock)
|
|
71
|
-
Prosemirror.parse(json, schema:).to_html
|
|
72
|
-
```
|
|
73
|
-
|
|
74
|
-
To change a built-in type, subclass it:
|
|
75
|
-
|
|
76
|
-
```ruby
|
|
77
|
-
class Heading < Prosemirror::Nodes::Heading
|
|
78
|
-
prop :id, _String?
|
|
79
|
-
|
|
80
|
-
def view_template(&)
|
|
81
|
-
tag(:"h#{@level}", id: @id || slugify(node.text_content), class: "heading", &)
|
|
82
|
-
end
|
|
83
|
-
end
|
|
84
|
-
```
|
|
85
|
-
|
|
86
|
-
A node can render parts of its content in different places, with `node` and `render_content`:
|
|
87
|
-
|
|
88
|
-
```ruby
|
|
89
|
-
# A quote whose last line, after a line break, is the author
|
|
90
|
-
class Blockquote < Prosemirror::Nodes::Blockquote
|
|
91
|
-
def view_template
|
|
92
|
-
inline = node.content.flat_map(&:content)
|
|
93
|
-
split = inline.rindex { |child| child.type == :hard_break }
|
|
94
|
-
|
|
95
|
-
figure do
|
|
96
|
-
blockquote { render_content(split ? inline[...split] : inline) }
|
|
97
|
-
figcaption { render_content(inline[(split + 1)..]) } if split
|
|
98
|
-
end
|
|
99
|
-
end
|
|
100
|
-
end
|
|
101
|
-
```
|
|
102
|
-
|
|
103
|
-
A `resolve_url` hook can rewrite URLs before they are checked, for example to turn `gid://` references into links:
|
|
104
|
-
|
|
105
|
-
```ruby
|
|
106
|
-
Prosemirror::Schema.tiptap(resolve_url: ->(url) { url.start_with?("gid://") ? url_for(GlobalID::Locator.locate(url)) : url })
|
|
107
|
-
```
|
|
108
|
-
|
|
109
|
-
In Rails, a custom node can render view components with [`phlex-rails`](https://github.com/yippee-fun/phlex-rails).
|
|
110
|
-
|
|
111
|
-
### Schemas
|
|
112
|
-
|
|
113
|
-
The gem has two schemas:
|
|
114
|
-
|
|
115
|
-
```ruby
|
|
116
|
-
Prosemirror::Schema.tiptap # Tiptap's StarterKit and common extensions (default)
|
|
117
|
-
Prosemirror::Schema.prosemirror # ProseMirror's prosemirror-schema-basic and prosemirror-schema-list
|
|
118
|
-
```
|
|
119
|
-
|
|
120
|
-
They differ in more than names:
|
|
121
|
-
|
|
122
|
-
| | `Schema.tiptap` | `Schema.prosemirror` |
|
|
123
|
-
|---|---|---|
|
|
124
|
-
| Marks | `bold`, `italic`, `code`, `link`, `strike`, `underline`… | `strong`, `em`, `code`, `link` |
|
|
125
|
-
| Ordered list | `start` | `order` |
|
|
126
|
-
| Code block | `language` | no attributes |
|
|
127
|
-
| Image | block | inline |
|
|
128
|
-
| Link | `href`, `target`, `rel`, `class` | `href`, `title` |
|
|
129
|
-
|
|
130
|
-
- Both use snake_case type names (`bullet_list`, `hard_break`). `case: :camel` outputs camelCase type names (`bulletList`), as Tiptap does.
|
|
131
|
-
- `case:` doesn't change attribute names. Editors only read the exact names they declare, and Tiptap mixes styles (`textAlign`, `colwidth`). In Ruby, attributes are snake_case properties. A different JSON name is declared on the property (`prop :text_align, _String?, json: "textAlign"`), and the built-in types use Tiptap's exact names. Input accepts both forms.
|
|
132
|
-
- Both accept input in both cases, and with the other schema's names (`strong` for `bold`, `order` for `start`).
|
|
133
|
-
- Nodes that are the same in both (paragraph, heading, blockquote…) use the same classes.
|
|
134
|
-
|
|
135
|
-
## Milestones
|
|
136
|
-
|
|
137
|
-
### 0.1: Rendering (released)
|
|
138
|
-
|
|
139
|
-
- `Node`, `Mark` and `Document` values, built from a Hash or a JSON string, with a nesting limit.
|
|
140
|
-
- Normalization as in ProseMirror: marks sorted by schema order, adjacent text with the same marks joined, empty text removed.
|
|
141
|
-
- Attributes a type doesn't declare kept in `unknown_attrs`: never rendered, and given back by `to_h`.
|
|
142
|
-
- `Schema`, `NodeType` and `MarkType`, with the options of ProseMirror's `NodeSpec` and `MarkSpec`: `content`, `group`, `inline`, `marks`, `whitespace`, `leaf_text`, `excludes`, `spanning`. `marks`, `whitespace` and `excludes` are declared, but only take effect with HTML import (0.2) and validation (0.4), as ProseMirror's `fromJSON` doesn't enforce them either.
|
|
143
|
-
- `Schema.tiptap`: `doc`, `paragraph`, `text`, `heading`, `blockquote`, `bullet_list`, `ordered_list`, `list_item`, `code_block`, `hard_break`, `horizontal_rule`, `image`, `table`, `table_row`, `table_header`, `table_cell`. Marks: `link`, `bold`, `italic`, `underline`, `strike`, `code`, `subscript`, `superscript`, `highlight`.
|
|
144
|
-
- `Schema.prosemirror`: exactly the nodes and marks of `prosemirror-schema-basic` and `prosemirror-schema-list`.
|
|
145
|
-
- `to_html`, with one tag for a mark shared by adjacent text (one `<a>` for a link with mixed formatting).
|
|
146
|
-
- `to_phlex` to render in Phlex components, and `render doc` in ERB templates and ViewComponents.
|
|
147
|
-
- `node` and `render_content` in node types, to render parts of a node's content.
|
|
148
|
-
- `to_text`, like Tiptap's `getText`.
|
|
149
|
-
- `case: :camel`, name aliases, allowed URL schemes, `resolve_url` (a URL it can't resolve is removed and reported).
|
|
150
|
-
- One JSON test fixture per node and mark, for both schemas.
|
|
151
|
-
- A [cookbook](COOKBOOK.md).
|
|
152
|
-
- Security tests, starting with every issue fixed in `prosemirror_to_html`:
|
|
153
|
-
- unescaped attribute values ([CVE-2025-64501](https://github.com/etaminstudio/prosemirror_to_html/security/advisories/GHSA-52c5-vh7f-26fx));
|
|
154
|
-
- attribute names from the document, like `onmouseover`, and `javascript:` URLs ([GHSA-6gj3-5qf2-wwvv](https://github.com/etaminstudio/prosemirror_to_html/security/advisories/GHSA-6gj3-5qf2-wwvv));
|
|
155
|
-
- a heading `level` like `1><script>`;
|
|
156
|
-
- other URL schemes, hidden schemes (`java\tscript:`), `style` injection, deep nesting.
|
|
157
|
-
|
|
158
|
-
### 0.2: HTML import (released)
|
|
159
|
-
|
|
160
|
-
- `Prosemirror.from_html`, built on [Herb](https://herb-tools.dev), which doesn't raise on bad HTML.
|
|
161
|
-
- The importer also needs to:
|
|
162
|
-
- decode entities (`&`, `é`), with the [WHATWG entity list](https://html.spec.whatwg.org/entities.json);
|
|
163
|
-
- keep `<% %>` in content as text;
|
|
164
|
-
- apply unclosed tags (`<strong>` without `</strong>`) to the rest of their parent;
|
|
165
|
-
- walk the tree without recursion, with a depth limit.
|
|
166
|
-
- Parse rules like ProseMirror's `ParseRule`: simple CSS selectors, `style` rules, `attrs`, `priority`, `context`, `ignore`, `skip`, `preserve_whitespace`, and more.
|
|
167
|
-
- ProseMirror's `DOMParser` behavior: collapse whitespace, wrap loose text in paragraphs, add missing wrappers (an `<li>` outside a list), unwrap unknown elements, ignore `script`, `style` and similar.
|
|
168
|
-
- Marks from inline styles (`font-weight: bold`), for content pasted from Google Docs or Word.
|
|
169
|
-
- Old HTML mapped to the schema: `<b>` and `<i>` to `bold` and `italic`, `<strike>` to `strike`, `<tt>` to `code`. `<font>` and `<center>` are unwrapped.
|
|
170
|
-
- Tests: HTML from Tiptap gives the same JSON as Tiptap.
|
|
171
|
-
|
|
172
|
-
### 0.3: Markdown and text import (released)
|
|
173
|
-
|
|
174
|
-
- `Prosemirror.from_markdown`, built from [`commonmarker`](https://github.com/gjtorikian/commonmarker)'s syntax tree. commonmarker's parser is written in Rust. Each Markdown node becomes the element commonmarker renders for it, without going through an HTML string, and HTML import's parse rules build the document.
|
|
175
|
-
- GitHub Flavored Markdown: tables, strikethrough, autolinks. Task list items stay as text until task list nodes (0.6).
|
|
176
|
-
- Custom node types match Markdown with their parse rules.
|
|
177
|
-
- Raw HTML in Markdown goes through the HTML importer if Herb is installed.
|
|
178
|
-
- `Prosemirror.from_text`: blank lines make paragraphs, line breaks make hard breaks.
|
|
179
|
-
|
|
180
|
-
### 0.4: Working with documents (released)
|
|
181
|
-
|
|
182
|
-
- Transformations that return a new document: rewrite links, unwrap lists, split paragraphs, remove empty ones.
|
|
183
|
-
|
|
184
|
-
```ruby
|
|
185
|
-
doc.transform do |node|
|
|
186
|
-
case node
|
|
187
|
-
in { type: :bullet_list } then node.content.map { |item| item.content.first }
|
|
188
|
-
in { type: :paragraph } if node.blank? then nil
|
|
189
|
-
else node
|
|
190
|
-
end
|
|
191
|
-
end
|
|
192
|
-
```
|
|
193
|
-
- Validation against the schema (`check`).
|
|
194
|
-
|
|
195
|
-
### 0.5: Markdown export (released)
|
|
196
|
-
|
|
197
|
-
- `doc.to_markdown`, ported from [`prosemirror-markdown`](https://github.com/ProseMirror/prosemirror-markdown)'s serializer. Useful to send content to LLMs.
|
|
198
|
-
- GitHub Flavored Markdown tables and strikethrough. Formatting without Markdown syntax, like underline, keeps its text, or is written as HTML with `html: true`.
|
|
199
|
-
- Custom node types write themselves in `to_markdown`, and mark types declare their syntax with `markdown`.
|
|
200
|
-
- Importing the Markdown gives the document back, as far as Markdown can hold it, tested on real-world Markdown and HTML.
|
|
201
|
-
|
|
202
|
-
### 0.6: More Tiptap node types (released)
|
|
203
|
-
|
|
204
|
-
- Mentions, task lists, details, and YouTube and Twitch embeds, in `Schema.tiptap`, with HTML that gives the same JSON as Tiptap's `generateJSON`.
|
|
205
|
-
- Text alignment, as an option: `Schema.tiptap(text_align: true)`.
|
|
206
|
-
- GitHub Flavored Markdown task lists in Markdown import and export. Details are written as GitHub's `<details>` HTML.
|
|
207
|
-
|
|
208
|
-
### 0.7: Resolving URLs at once (released)
|
|
209
|
-
|
|
210
|
-
- `resolve_urls:` resolves all the URLs of a document before rendering it, to look up records in one query. Types declare their URL attributes with `prop … url: true`, and `Document#urls` lists them.
|
|
211
|
-
- A [migration guide](MIGRATING.md) from `prosemirror_to_html` and `html_to_prosemirror`.
|
|
212
|
-
|
|
213
|
-
### 1.0
|
|
214
|
-
|
|
215
|
-
- Stable API, documentation site, benchmarks.
|
|
216
|
-
- Migration guides from `prosemirror_to_html` and `html_to_prosemirror`, and final releases of both pointing to `prosemirror`.
|
|
217
|
-
- Used in production by [Trek](https://trek.etamin.studio), a Rails CMS that renders Tiptap documents with custom nodes.
|
|
218
|
-
|
|
219
|
-
## Out of scope
|
|
220
|
-
|
|
221
|
-
- CSS framework themes: subclass node types instead.
|
|
222
|
-
- A Rails or ViewComponent dependency.
|
|
223
|
-
- Editing features: positions, steps, commands, collaboration.
|
|
224
|
-
- The editor itself.
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Prosemirror
|
|
4
|
-
# Tiptap's TextAlign extension: a `textAlign` attribute, rendered as a `text-align` style. Included in the
|
|
5
|
-
# paragraphs and headings of `Schema.tiptap(text_align: true)`. A custom type includes it, and renders
|
|
6
|
-
# `style: text_align_style`.
|
|
7
|
-
module TextAlign
|
|
8
|
-
ALIGNMENTS = %w[left center right justify].freeze
|
|
9
|
-
|
|
10
|
-
def self.included(type)
|
|
11
|
-
# From the `text-align` style, or the old `align` attribute, which Tiptap doesn't read.
|
|
12
|
-
type.prop :text_align, type._Union?(*ALIGNMENTS), json: "textAlign", parse_html: lambda { |element|
|
|
13
|
-
(element.style["text-align"] || element["align"]).to_s.strip.downcase.then { |value| value if ALIGNMENTS.include?(value) }
|
|
14
|
-
}
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
def text_align_style = @text_align && "text-align: #{@text_align}"
|
|
18
|
-
|
|
19
|
-
class Paragraph < Nodes::Paragraph
|
|
20
|
-
include TextAlign
|
|
21
|
-
|
|
22
|
-
def view_template(&) = p(style: text_align_style, &)
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
class Heading < Nodes::Heading
|
|
26
|
-
include TextAlign
|
|
27
|
-
|
|
28
|
-
def view_template(&) = tag(:"h#{@level}", style: text_align_style, &)
|
|
29
|
-
end
|
|
30
|
-
end
|
|
31
|
-
end
|