prosemirror 0.7.0 → 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 +19 -0
- data/COOKBOOK.md +35 -0
- data/README.md +68 -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 +22 -8
- 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 +18 -3
- metadata +9 -6
- data/ROADMAP.md +0 -224
- data/lib/prosemirror/text_align.rb +0 -31
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 1c411a333e40240328c4e19d8b5f8870c0f90bad21573b6fc68b503901d2f0b7
|
|
4
|
+
data.tar.gz: d13dcd8a9fb6001183010cc73c82a6541d6466214fcf59947d01bb5a4e2c0c21
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: '0311783f814484b6f3e682c07228590084a97f9a1e555bdc9dbbb61acfd5d88c3ae4688c2b6afada4f1683395df878e0c88f97e4f673dfe106250e017f21d532'
|
|
7
|
+
data.tar.gz: 35b83befa7e82312b910152784800192d958151497436abba2d279b8d80929430a7062fe3c003f5ec84c3b713a0b8c12693412767fd863f51588d5ba625a3af1
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,24 @@
|
|
|
1
1
|
## [Unreleased]
|
|
2
2
|
|
|
3
|
+
## [1.0.0] - 2026-10-25
|
|
4
|
+
|
|
5
|
+
### Public API
|
|
6
|
+
|
|
7
|
+
The public API is defined for 1.0: what the README, TYPES.md and the cookbook document. The rest is marked `@api private`.
|
|
8
|
+
|
|
9
|
+
- **Breaking:** `Schema#parse_html`, `#parse_markdown` and `#parse_text` are renamed `#from_html`, `#from_markdown` and `#from_text`, as `Prosemirror.from_html`, `.from_markdown` and `.from_text`, which are unchanged.
|
|
10
|
+
- **Breaking:** `Prosemirror::TextAlign` moves to `Prosemirror::Extensions::TextAlign`, with its `Paragraph` and `Heading`.
|
|
11
|
+
- **Breaking:** a JSON string or Markdown nested too deeply raises `Prosemirror::TooDeep`, a subclass of `Prosemirror::Error`. JSON raised `InvalidDocument`, and Markdown `Error`.
|
|
12
|
+
- `Schema#with_options` is documented: a copy of the schema with other options.
|
|
13
|
+
|
|
14
|
+
### Performance
|
|
15
|
+
|
|
16
|
+
- Faster HTML and Markdown import: about a quarter less time, with the same results. Imports allocate a third fewer objects, so they also spend less time in garbage collection.
|
|
17
|
+
|
|
18
|
+
## [0.7.1] - 2026-10-24
|
|
19
|
+
|
|
20
|
+
- A URL that `resolve_url` or `resolve_urls` resolves to nil, like a link to a deleted record, is reported as `Prosemirror::UnresolvedURL`, a subclass of `InvalidDocument` with the `url`, so that error trackers group dead links apart from other problems.
|
|
21
|
+
|
|
3
22
|
## [0.7.0] - 2026-10-24
|
|
4
23
|
|
|
5
24
|
- `resolve_urls:` resolves all the URLs of a document at once, before rendering it, to look up records in one query, where `resolve_url:` is called once per URL. Attributes holding URLs are declared with `prop … url: true`, like links' `href` and images' `src`, and `Document#urls` lists them.
|
data/COOKBOOK.md
CHANGED
|
@@ -48,6 +48,23 @@ record.update!(content: document.to_h)
|
|
|
48
48
|
|
|
49
49
|
Without `case: :camel`, `to_h` writes snake_case type names like `bullet_list`, which Tiptap doesn't read. `blank?` gets the schema so that it knows your custom types: an image block isn't blank. Children are transformed first, so the list sees its remaining items. Attributes the schema doesn't declare are kept. When something the schema requires is gone, like the only paragraph of a list item kept by another rule, `transform` adds it back and reports it with `on_invalid`.
|
|
50
50
|
|
|
51
|
+
## Tight lists
|
|
52
|
+
|
|
53
|
+
Editors wrap each list item's text in a paragraph, so lists render as `<li><p>text</p></li>`, with the paragraphs' margins. To render an item of a single paragraph as `<li>text</li>`, as Markdown does for tight lists:
|
|
54
|
+
|
|
55
|
+
```ruby
|
|
56
|
+
class ListItem < Prosemirror::Nodes::ListItem
|
|
57
|
+
def view_template(&)
|
|
58
|
+
paragraph = node.content.first if node.content.size == 1
|
|
59
|
+
return super unless paragraph&.type == :paragraph && paragraph.content.any?
|
|
60
|
+
|
|
61
|
+
li { render_content(paragraph.content) }
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Items with several paragraphs or a nested list, and items left empty, render as before.
|
|
67
|
+
|
|
51
68
|
## Heading anchors
|
|
52
69
|
|
|
53
70
|
To link to a section, give each heading an `id` made from its text, unless the document already has one:
|
|
@@ -73,6 +90,24 @@ end
|
|
|
73
90
|
|
|
74
91
|
A node doesn't see the rest of the document, so two headings with the same text get the same `id`.
|
|
75
92
|
|
|
93
|
+
## Quotes with their author
|
|
94
|
+
|
|
95
|
+
A node can render parts of its content in different places, with `node` and `render_content`. Here, a quote's last line, after a line break, is its author:
|
|
96
|
+
|
|
97
|
+
```ruby
|
|
98
|
+
class Blockquote < Prosemirror::Nodes::Blockquote
|
|
99
|
+
def view_template
|
|
100
|
+
inline = node.content.flat_map(&:content)
|
|
101
|
+
split = inline.rindex { |child| child.type == :hard_break }
|
|
102
|
+
|
|
103
|
+
figure do
|
|
104
|
+
blockquote { render_content(split ? inline[...split] : inline) }
|
|
105
|
+
figcaption { render_content(inline[(split + 1)..]) } if split
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
```
|
|
110
|
+
|
|
76
111
|
## Render your own components
|
|
77
112
|
|
|
78
113
|
A node type is a Phlex component, so it can render the app's Phlex components:
|
data/README.md
CHANGED
|
@@ -7,7 +7,11 @@ Render [ProseMirror](https://prosemirror.net) and [Tiptap](https://tiptap.dev) d
|
|
|
7
7
|
- Link and image URLs must use an allowed scheme.
|
|
8
8
|
- No Rails dependency.
|
|
9
9
|
|
|
10
|
-
This gem replaces [prosemirror_to_html](https://github.com/etaminstudio/prosemirror_to_html) and [html_to_prosemirror](https://github.com/etaminstudio/html_to_prosemirror): see [Migrating](MIGRATING.md).
|
|
10
|
+
This gem replaces [prosemirror_to_html](https://github.com/etaminstudio/prosemirror_to_html) and [html_to_prosemirror](https://github.com/etaminstudio/html_to_prosemirror), now deprecated: see [Migrating](MIGRATING.md).
|
|
11
|
+
|
|
12
|
+
## Why
|
|
13
|
+
|
|
14
|
+
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. This gem does all of it with one schema: a custom type is declared once, and renders, imports and exports the same way.
|
|
11
15
|
|
|
12
16
|
## Installation
|
|
13
17
|
|
|
@@ -116,7 +120,7 @@ It reads GitHub Flavored Markdown: tables, strikethrough, autolinks and task lis
|
|
|
116
120
|
|
|
117
121
|
Raw HTML in Markdown goes through HTML import when the herb gem is installed, and stays as text otherwise.
|
|
118
122
|
|
|
119
|
-
YAML front matter, between `---` lines at the start, is dropped. Footnotes aren't supported yet: `[^1]: Note` reads as a link definition, as in CommonMark. Markdown nested more than 1,000 levels deep raises `Prosemirror::
|
|
123
|
+
YAML front matter, between `---` lines at the start, is dropped. Footnotes aren't supported yet: `[^1]: Note` reads as a link definition, as in CommonMark. Markdown nested more than 1,000 levels deep raises `Prosemirror::TooDeep`.
|
|
120
124
|
|
|
121
125
|
## Markdown export
|
|
122
126
|
|
|
@@ -165,6 +169,26 @@ Type names are written in snake_case (`bullet_list`). Use `case: :camel` to writ
|
|
|
165
169
|
Prosemirror.schema = Prosemirror::Schema.tiptap(case: :camel)
|
|
166
170
|
```
|
|
167
171
|
|
|
172
|
+
`case:` doesn't change attribute names: editors only read the exact names they declare, and Tiptap mixes styles (`textAlign`, `colwidth`). The built-in types use Tiptap's exact names, and input accepts both cases.
|
|
173
|
+
|
|
174
|
+
The two schemas differ in more than names:
|
|
175
|
+
|
|
176
|
+
| | `Schema.tiptap` | `Schema.prosemirror` |
|
|
177
|
+
|---|---|---|
|
|
178
|
+
| Marks | `bold`, `italic`, `code`, `link`, `strike`, `underline`… | `strong`, `em`, `code`, `link` |
|
|
179
|
+
| Ordered list | `start` | `order` |
|
|
180
|
+
| Code block | `language` | no attributes |
|
|
181
|
+
| Image | block | inline |
|
|
182
|
+
| Link | `href`, `target`, `rel`, `class`, `title` | `href`, `title` |
|
|
183
|
+
|
|
184
|
+
Each accepts the other's names on input (`strong` for `bold`, `order` for `start`), and the types that are the same in both, like paragraphs, headings and lists, are the same classes.
|
|
185
|
+
|
|
186
|
+
Schemas are immutable. `with` returns a copy with more types (see [Custom nodes and marks](#custom-nodes-and-marks)), and `with_options` a copy with other options, like a stricter one for an import:
|
|
187
|
+
|
|
188
|
+
```ruby
|
|
189
|
+
strict = Prosemirror.schema.with_options(strict: true)
|
|
190
|
+
```
|
|
191
|
+
|
|
168
192
|
`text_align: true` adds Tiptap's TextAlign extension, configured for headings and paragraphs: a `textAlign` attribute, `left`, `center`, `right` or `justify`, rendered as a `text-align` style. HTML import also reads the old `align` attribute. Markdown has no syntax for it: Markdown export leaves it out. Without the option, `textAlign` is kept in `to_h`, but not rendered.
|
|
169
193
|
|
|
170
194
|
```ruby
|
|
@@ -248,6 +272,10 @@ Prosemirror::Schema.tiptap(on_invalid: ->(error) { Rails.error.report(error) })
|
|
|
248
272
|
Prosemirror::Schema.tiptap(strict: true) # raises Prosemirror::InvalidDocument
|
|
249
273
|
```
|
|
250
274
|
|
|
275
|
+
A URL that `resolve_url` or `resolve_urls` resolves to nil, like a link to a deleted record, is reported as `Prosemirror::UnresolvedURL`, a subclass of `InvalidDocument` with the `url`, so that error trackers group dead links apart from other problems.
|
|
276
|
+
|
|
277
|
+
A JSON string or Markdown nested too deeply to import raises `Prosemirror::TooDeep`, in any mode: JSON deeper than 100 levels, Markdown deeper than 1,000. A Hash nested too deeply is cut and reported instead.
|
|
278
|
+
|
|
251
279
|
## URLs
|
|
252
280
|
|
|
253
281
|
Links and images allow `http`, `https`, `mailto`, `tel` and relative URLs. Change the list with `url_schemes:`, or rewrite URLs before they are checked with `resolve_url:`:
|
|
@@ -261,10 +289,48 @@ Prosemirror::Schema.tiptap(
|
|
|
261
289
|
|
|
262
290
|
`resolve_urls:` gets all the URLs of a document at once, before rendering it, and returns a Hash from each URL to its rewritten URL, to look up records in one query (see the [cookbook](COOKBOOK.md#links-to-records-with-globalid)). A URL missing from the Hash, or mapped to nil, is removed and reported, as with `resolve_url`. The URLs are those of the attributes declared with `url: true`, like links' `href` and images' `src`, which `doc.urls` lists. URLs that types build while rendering, like YouTube's embed URLs, are resolved one at a time.
|
|
263
291
|
|
|
292
|
+
## Design
|
|
293
|
+
|
|
294
|
+
- Nothing is cleaned up after rendering: Phlex escapes text and attribute values as it writes them, attributes are typed, attribute names never come from the document, and URLs must use an allowed scheme.
|
|
295
|
+
- Output is HTML5 only: no `<strike>`, `<font>` or `align=`. Import still reads them.
|
|
296
|
+
- Only `phlex` and `literal` are required. [`herb`](https://herb-tools.dev) and [`commonmarker`](https://github.com/gjtorikian/commonmarker) are loaded only by HTML and Markdown import.
|
|
297
|
+
- Schemas, content expressions, marks and parse rules work as in [ProseMirror's guide](https://prosemirror.net/docs/guide/#schema), with the same JSON. Methods keep ProseMirror's names in Ruby style (`text_content`, `descendants`, `check`).
|
|
298
|
+
- Nodes are immutable values with pattern matching, documents are `Enumerable`, and node types are classes. Each type declares its attributes, its content, its HTML and how to recognize it in HTML, so rendering and import use the same schema.
|
|
299
|
+
|
|
300
|
+
Out of scope: CSS framework themes (subclass node types instead), a Rails or ViewComponent dependency, editing features (positions, steps, commands, collaboration), and the editor itself.
|
|
301
|
+
|
|
302
|
+
## Performance
|
|
303
|
+
|
|
304
|
+
On a page of 151 nodes (8 KB of JSON: headings, paragraphs with marks and links, lists, an image, a table…) and a long document of 50 such pages, with Ruby 4.0 and YJIT on an Apple M2:
|
|
305
|
+
|
|
306
|
+
| | Page | Long document |
|
|
307
|
+
|---|--:|--:|
|
|
308
|
+
| `parse` (JSON) | 0.3 ms | 15 ms |
|
|
309
|
+
| `to_html` | 0.2 ms | 8 ms |
|
|
310
|
+
| `to_text` | 0.06 ms | 3 ms |
|
|
311
|
+
| `to_markdown` | 0.6 ms | 29 ms |
|
|
312
|
+
| `to_h` | 0.2 ms | 10 ms |
|
|
313
|
+
| `from_html` | 1.2 ms | 68 ms |
|
|
314
|
+
| `from_markdown` | 1 ms | 46 ms |
|
|
315
|
+
| `transform`, `check` | 0.3 ms | 15 ms |
|
|
316
|
+
|
|
317
|
+
From a JSON string to HTML, a page takes 0.6 ms, about 5 times less than with prosemirror_to_html. HTML import takes about 1.5 times as long as html_to_prosemirror's, which does less: it drops text in nested marks, ignores styles, and doesn't follow the schema. See [`benchmark/run.rb`](benchmark/run.rb) to measure on your machine.
|
|
318
|
+
|
|
319
|
+
## Versioning
|
|
320
|
+
|
|
321
|
+
The gem follows [semantic versioning](https://semver.org). Its public API is what this README, [Node and mark types](TYPES.md) and the [cookbook](COOKBOOK.md) document: it only changes in a major version. The rest, marked `@api private` in the code, like the parsers and the schema's internal methods, can change in any version.
|
|
322
|
+
|
|
264
323
|
## Development
|
|
265
324
|
|
|
266
325
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests, and `rake` to also run [Standard](https://github.com/standardrb/standard). You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
267
326
|
|
|
327
|
+
`benchmark/run.rb` measures each operation on a page and a long document, and compares with the gems this one replaces. It has its own Gemfile:
|
|
328
|
+
|
|
329
|
+
```sh
|
|
330
|
+
BUNDLE_GEMFILE=benchmark/Gemfile bundle install
|
|
331
|
+
BUNDLE_GEMFILE=benchmark/Gemfile bundle exec ruby benchmark/run.rb
|
|
332
|
+
```
|
|
333
|
+
|
|
268
334
|
To install this gem onto your local machine, run `bundle exec rake install`.
|
|
269
335
|
|
|
270
336
|
To release a new version, update the version number in `version.rb` and the changelog, and merge them into main. Then tag the version and push the tag:
|
data/TYPES.md
CHANGED
|
@@ -261,11 +261,11 @@ Prosemirror.schema = Prosemirror::Schema.tiptap.with(Paragraph)
|
|
|
261
261
|
|
|
262
262
|
A subclass that declares parse rules replaces the rules of its parent. See the [cookbook](COOKBOOK.md) for more examples.
|
|
263
263
|
|
|
264
|
-
With `Schema.tiptap(text_align: true)`, paragraphs and headings are `Prosemirror::TextAlign::Paragraph` and `Prosemirror::TextAlign::Heading`: subclass these to keep the alignment. Other types get it by including `Prosemirror::TextAlign`, which declares the `text_align` attribute, and rendering `text_align_style`:
|
|
264
|
+
With `Schema.tiptap(text_align: true)`, paragraphs and headings are `Prosemirror::Extensions::TextAlign::Paragraph` and `Prosemirror::Extensions::TextAlign::Heading`: subclass these to keep the alignment. Other types get it by including `Prosemirror::Extensions::TextAlign`, which declares the `text_align` attribute, and rendering `text_align_style`:
|
|
265
265
|
|
|
266
266
|
```ruby
|
|
267
267
|
class Callout < Prosemirror::NodeType
|
|
268
|
-
include Prosemirror::TextAlign
|
|
268
|
+
include Prosemirror::Extensions::TextAlign
|
|
269
269
|
group :block
|
|
270
270
|
content "inline*"
|
|
271
271
|
|
data/lib/prosemirror/checker.rb
CHANGED
|
@@ -6,6 +6,7 @@ module Prosemirror
|
|
|
6
6
|
#
|
|
7
7
|
# Rendering and JSON parsing don't need valid content: they render what they get. This checks what
|
|
8
8
|
# they don't: node and mark types, attributes, content expressions, and the marks each node allows.
|
|
9
|
+
# @api private
|
|
9
10
|
class Checker
|
|
10
11
|
def initialize(schema)
|
|
11
12
|
@schema = schema
|
data/lib/prosemirror/coerce.rb
CHANGED
|
@@ -9,14 +9,18 @@ module Prosemirror
|
|
|
9
9
|
INTEGER = proc { |value| (value.is_a?(String) && value.match?(/\A\s*-?\d+\s*\z/)) ? value.to_i : value }
|
|
10
10
|
|
|
11
11
|
# "bullet_list" becomes :bullet_list.
|
|
12
|
+
# @api private
|
|
12
13
|
SYMBOL = proc { |value| value.is_a?(String) ? value.to_sym : value }
|
|
13
14
|
|
|
14
15
|
# A frozen copy, unless the value is already frozen.
|
|
16
|
+
# @api private
|
|
15
17
|
FROZEN = proc { |value| value.frozen? ? value : value.dup.freeze }
|
|
16
18
|
|
|
17
19
|
# The coercions above, which don't use `self`.
|
|
20
|
+
# @api private
|
|
18
21
|
PLAIN = [INTEGER, SYMBOL, FROZEN].freeze
|
|
19
22
|
|
|
23
|
+
# @api private
|
|
20
24
|
def self.frozen(value)
|
|
21
25
|
value.frozen? ? value : value.dup.freeze
|
|
22
26
|
end
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
module Prosemirror
|
|
4
4
|
# A parsed content expression, such as `"paragraph block*"` or `"(table_cell | table_header){1,5}"`.
|
|
5
5
|
# See https://prosemirror.net/docs/guide/#schema.content_expressions
|
|
6
|
+
# @api private
|
|
6
7
|
class ContentExpression
|
|
7
8
|
TOKEN = /\s*(\w+|[()|+*?]|\{\s*\d+\s*(?:,\s*\d*\s*)?\})/
|
|
8
9
|
|
|
@@ -6,6 +6,7 @@ module Prosemirror
|
|
|
6
6
|
#
|
|
7
7
|
# Edges go from one state to the next for a node type. Their order matters: the first generatable
|
|
8
8
|
# type is used to fill required content.
|
|
9
|
+
# @api private
|
|
9
10
|
class ContentMatch
|
|
10
11
|
attr_reader :edges
|
|
11
12
|
|
|
@@ -34,6 +35,8 @@ module Prosemirror
|
|
|
34
35
|
|
|
35
36
|
# The state after these node types, or nil when they don't match.
|
|
36
37
|
def match_types(types)
|
|
38
|
+
return self if types.empty?
|
|
39
|
+
|
|
37
40
|
types.reduce(self) { |match, type| match&.match_type(type) }
|
|
38
41
|
end
|
|
39
42
|
|
|
@@ -49,6 +52,10 @@ module Prosemirror
|
|
|
49
52
|
# The node types to insert before `types` so that they match, possibly none, or nil when no
|
|
50
53
|
# insertion works. With `to_end`, the content must also be able to end after them.
|
|
51
54
|
def fill_before(types, schema, to_end: false)
|
|
55
|
+
# Most content needs nothing: no search.
|
|
56
|
+
finished = match_types(types)
|
|
57
|
+
return Node::EMPTY_ARRAY if finished && (!to_end || finished.valid_end?)
|
|
58
|
+
|
|
52
59
|
seen = [self]
|
|
53
60
|
search = lambda do |match, filled|
|
|
54
61
|
finished = match.match_types(types)
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Prosemirror
|
|
4
|
+
# Tiptap extensions that add attributes to existing types, rather than types of their own.
|
|
5
|
+
module Extensions
|
|
6
|
+
# Tiptap's TextAlign extension: a `textAlign` attribute, rendered as a `text-align` style. Included in the
|
|
7
|
+
# paragraphs and headings of `Schema.tiptap(text_align: true)`. A custom type includes it, and renders
|
|
8
|
+
# `style: text_align_style`.
|
|
9
|
+
module TextAlign
|
|
10
|
+
ALIGNMENTS = %w[left center right justify].freeze
|
|
11
|
+
|
|
12
|
+
def self.included(type)
|
|
13
|
+
# From the `text-align` style, or the old `align` attribute, which Tiptap doesn't read.
|
|
14
|
+
type.prop :text_align, type._Union?(*ALIGNMENTS), json: "textAlign", parse_html: lambda { |element|
|
|
15
|
+
(element.style["text-align"] || element["align"]).to_s.strip.downcase.then { |value| value if ALIGNMENTS.include?(value) }
|
|
16
|
+
}
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def text_align_style = @text_align && "text-align: #{@text_align}"
|
|
20
|
+
|
|
21
|
+
class Paragraph < Nodes::Paragraph
|
|
22
|
+
include TextAlign
|
|
23
|
+
|
|
24
|
+
def view_template(&) = p(style: text_align_style, &)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
class Heading < Nodes::Heading
|
|
28
|
+
include TextAlign
|
|
29
|
+
|
|
30
|
+
def view_template(&) = tag(:"h#{@level}", style: text_align_style, &)
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -9,6 +9,7 @@ module Prosemirror
|
|
|
9
9
|
# - applies an opening tag without a closing tag to the rest of its parent, as browsers do;
|
|
10
10
|
# - drops comments (including Word's `<![if …]>`), doctypes and CDATA;
|
|
11
11
|
# - walks the tree without recursion, and flattens elements nested deeper than MAX_DEPTH.
|
|
12
|
+
# @api private
|
|
12
13
|
class Builder
|
|
13
14
|
MAX_DEPTH = 256
|
|
14
15
|
# The size of the pieces Herb parses, in bytes. See Splitter.
|
|
@@ -76,7 +77,7 @@ module Prosemirror
|
|
|
76
77
|
case node
|
|
77
78
|
when Herb::AST::HTMLElementNode then add_element(node, target, depth)
|
|
78
79
|
when Herb::AST::HTMLOpenTagNode then add_open_tag(node, target, depth)
|
|
79
|
-
when Herb::AST::HTMLTextNode then add_text(Entities.decode(node.content.to_s
|
|
80
|
+
when Herb::AST::HTMLTextNode then add_text(Entities.decode(without_bogus_comments(node.content.to_s)), target)
|
|
80
81
|
when Herb::AST::LiteralNode then add_text(node.content.to_s, target)
|
|
81
82
|
when Herb::AST::WhitespaceNode then add_text(node.value&.value.to_s, target)
|
|
82
83
|
when Herb::AST::HTMLCommentNode, Herb::AST::HTMLDoctypeNode, Herb::AST::XMLDeclarationNode,
|
|
@@ -126,28 +127,46 @@ module Prosemirror
|
|
|
126
127
|
nil
|
|
127
128
|
end
|
|
128
129
|
|
|
130
|
+
def without_bogus_comments(text)
|
|
131
|
+
text.include?("<!") ? text.gsub(BOGUS_COMMENT, "") : text
|
|
132
|
+
end
|
|
133
|
+
|
|
129
134
|
def new_element(open_tag)
|
|
130
|
-
Element.new(open_tag.tag_name.value
|
|
135
|
+
Element.new(lowercase(open_tag.tag_name.value), attributes(open_tag.children))
|
|
131
136
|
end
|
|
132
137
|
|
|
133
138
|
# The first value wins when an attribute is repeated, as in browsers.
|
|
134
139
|
def attributes(nodes)
|
|
135
|
-
|
|
136
|
-
|
|
140
|
+
result = {}
|
|
141
|
+
nodes.each do |attribute|
|
|
142
|
+
next unless attribute.is_a?(Herb::AST::HTMLAttributeNode)
|
|
143
|
+
|
|
144
|
+
name = lowercase(text_of(attribute.name&.children))
|
|
137
145
|
next if name.empty? || result.key?(name)
|
|
138
146
|
|
|
139
147
|
result[name] = Entities.decode(text_of(attribute.value&.children), attribute: true)
|
|
140
148
|
end
|
|
149
|
+
result
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
# Names are lowercase in most HTML: only the others are copied.
|
|
153
|
+
def lowercase(name)
|
|
154
|
+
name.match?(/[A-Z]/) ? name.downcase : name
|
|
141
155
|
end
|
|
142
156
|
|
|
143
157
|
def text_of(nodes)
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
158
|
+
return "" if nodes.nil? || nodes.empty?
|
|
159
|
+
return text_of_node(nodes.first) if nodes.size == 1 # most names and values: no arrays to join
|
|
160
|
+
|
|
161
|
+
nodes.map { |node| text_of_node(node) }.join
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def text_of_node(node)
|
|
165
|
+
if node.respond_to?(:tag_opening) && node.respond_to?(:tag_closing)
|
|
166
|
+
[node.tag_opening, node.content, node.tag_closing].map { |token| token&.value.to_s }.join
|
|
167
|
+
else
|
|
168
|
+
node.content.to_s
|
|
169
|
+
end
|
|
151
170
|
end
|
|
152
171
|
end
|
|
153
172
|
end
|
|
@@ -4,6 +4,7 @@ module Prosemirror
|
|
|
4
4
|
module HTML
|
|
5
5
|
# Decodes character references (`&`, `é`, `é`, `é`) as HTML5 does, with the
|
|
6
6
|
# WHATWG list of named references: https://html.spec.whatwg.org/multipage/named-characters.html
|
|
7
|
+
# @api private
|
|
7
8
|
module Entities
|
|
8
9
|
# Code points 0x80 to 0x9F, read as Windows-1252, as browsers do.
|
|
9
10
|
WINDOWS_1252 = {
|
|
@@ -5,6 +5,7 @@ module Prosemirror
|
|
|
5
5
|
# The CSS selectors parse rules need, matched against one element: lists of compound selectors,
|
|
6
6
|
# with a type (`p`, `*`), `#id`, `.class`, attribute selectors (`[href]`, `[a=b]`, `[a^=b]`,
|
|
7
7
|
# `[a$=b]`, `[a*=b]`, `[a~=b]`, `[a|=b]`) and `:not(…)`. Combinators aren't supported.
|
|
8
|
+
# @api private
|
|
8
9
|
class Selector
|
|
9
10
|
IDENTIFIER = /-?[_a-zA-Z][-\w]*/
|
|
10
11
|
ATTRIBUTE = /\[\s*(#{IDENTIFIER})\s*(?:([~|^$*]?=)\s*(?:"([^"]*)"|'([^']*)'|(#{IDENTIFIER}))\s*)?\]/
|
|
@@ -11,6 +11,7 @@ module Prosemirror
|
|
|
11
11
|
# It only cuts where it can follow the structure: when a closing tag doesn't match the last open
|
|
12
12
|
# element, as with unclosed or implicitly closed elements, the rest stays in one piece. HTML with
|
|
13
13
|
# ERB tags isn't split, since Herb groups ERB blocks with the HTML between them.
|
|
14
|
+
# @api private
|
|
14
15
|
class Splitter
|
|
15
16
|
# As Herb's lists.
|
|
16
17
|
VOID_TAGS = %w[
|
|
@@ -41,14 +42,14 @@ module Prosemirror
|
|
|
41
42
|
elsif scanner.skip(/[!?]/)
|
|
42
43
|
break unless scanner.skip_until(/>/)
|
|
43
44
|
elsif scanner.skip(%r{/(#{NAME})\s*>}o)
|
|
44
|
-
break unless open.pop == scanner[1]
|
|
45
|
+
break unless open.pop == lowercase(scanner[1])
|
|
45
46
|
elsif scanner.skip(/(#{NAME})/o)
|
|
46
|
-
name = scanner[1]
|
|
47
|
-
break unless
|
|
47
|
+
name = lowercase(scanner[1])
|
|
48
|
+
break unless scanner.skip(TAG_REST)
|
|
48
49
|
|
|
49
50
|
if RAW_TEXT_TAGS.include?(name)
|
|
50
51
|
break unless scanner.skip_until(%r{</#{Regexp.escape(name)}\s*>}i)
|
|
51
|
-
elsif !VOID_TAGS.include?(name) && !
|
|
52
|
+
elsif !VOID_TAGS.include?(name) && !self_closing?(html, scanner.pos)
|
|
52
53
|
open << name
|
|
53
54
|
end
|
|
54
55
|
else
|
|
@@ -63,6 +64,18 @@ module Prosemirror
|
|
|
63
64
|
pieces << html.byteslice(start..) if start < html.bytesize
|
|
64
65
|
pieces
|
|
65
66
|
end
|
|
67
|
+
|
|
68
|
+
private
|
|
69
|
+
|
|
70
|
+
# Tag names are lowercase in most HTML: only the others are copied.
|
|
71
|
+
def lowercase(name)
|
|
72
|
+
name.match?(/[A-Z]/) ? name.downcase : name
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# Whether the tag that ends before this byte position ends with `/>`, without copying it.
|
|
76
|
+
def self_closing?(html, position)
|
|
77
|
+
html.getbyte(position - 2) == 47 # "/"
|
|
78
|
+
end
|
|
66
79
|
end
|
|
67
80
|
end
|
|
68
81
|
end
|
|
@@ -10,6 +10,7 @@ module Prosemirror
|
|
|
10
10
|
# - required content is filled in (an empty list item gets a paragraph);
|
|
11
11
|
# - elements without a rule are unwrapped, and `head`, `noscript`, `object`, `script`, `style` and `title`
|
|
12
12
|
# are ignored.
|
|
13
|
+
# @api private
|
|
13
14
|
class HTMLParser
|
|
14
15
|
BLOCK_TAGS = %w[
|
|
15
16
|
address article aside blockquote canvas dd div dl fieldset figcaption figure footer form h1 h2 h3 h4 h5 h6
|
|
@@ -23,6 +24,8 @@ module Prosemirror
|
|
|
23
24
|
|
|
24
25
|
WHITESPACE = /[ \t\r\n\f]/
|
|
25
26
|
WHITESPACE_RUN = /[ \t\r\n\f]+/
|
|
27
|
+
# Whitespace that collapsing changes: other than a single space.
|
|
28
|
+
COLLAPSIBLE = /[\t\r\n\f]| /
|
|
26
29
|
|
|
27
30
|
# A rule that matched an element: the rule, its index, the attributes it gives, and the schema's type.
|
|
28
31
|
Match = Data.define(:rule, :index, :attrs, :type)
|
|
@@ -42,6 +45,7 @@ module Prosemirror
|
|
|
42
45
|
@styles = rules.select(&:style)
|
|
43
46
|
@matched_styles = @styles.map(&:style_property).uniq
|
|
44
47
|
@normalize_lists = @tags.none? { |rule| lists_contain_themselves?(rule) }
|
|
48
|
+
@report = ->(message) { @schema.report(message, path: "html") }
|
|
45
49
|
end
|
|
46
50
|
|
|
47
51
|
# The document parsed from the HTML, as its top node.
|
|
@@ -110,9 +114,12 @@ module Prosemirror
|
|
|
110
114
|
|
|
111
115
|
# The rule's attributes, with the ones read from the element, built with the type's properties.
|
|
112
116
|
def build_attrs(type, given, element)
|
|
113
|
-
given =
|
|
114
|
-
|
|
115
|
-
|
|
117
|
+
given = given ? given.transform_keys(&:to_s) : TypeDefinition::EMPTY_HASH
|
|
118
|
+
if element
|
|
119
|
+
from_html = type.attrs_from_html(element)
|
|
120
|
+
given = given.empty? ? from_html : given.merge(from_html)
|
|
121
|
+
end
|
|
122
|
+
type.build_attrs(given, report: @report)
|
|
116
123
|
end
|
|
117
124
|
|
|
118
125
|
def call_get_attrs(get_attrs, element)
|
|
@@ -148,9 +155,10 @@ module Prosemirror
|
|
|
148
155
|
|
|
149
156
|
def finish
|
|
150
157
|
strip_trailing_whitespace unless @options.anybits?(PRESERVE_WS)
|
|
151
|
-
fill = @match&.fill_before(
|
|
152
|
-
content =
|
|
153
|
-
|
|
158
|
+
fill = @match&.fill_before(Node::EMPTY_ARRAY, @schema, to_end: true) || Node::EMPTY_ARRAY
|
|
159
|
+
content = fill.empty? ? @content : @content + fill.filter_map { |type| @schema.create_and_fill(type) }
|
|
160
|
+
# Frozen here, so that the node doesn't copy it: nothing else holds it.
|
|
161
|
+
Node.new(type: @type.type_name, attrs: @attrs, content: Node.join_text(content).freeze, marks: @marks)
|
|
154
162
|
end
|
|
155
163
|
|
|
156
164
|
def inline_context?
|
|
@@ -243,7 +251,7 @@ module Prosemirror
|
|
|
243
251
|
return unless preserve == :full || top.inline_context? || value.match?(/[^ \t\r\n\f]/)
|
|
244
252
|
|
|
245
253
|
if !preserve
|
|
246
|
-
value = value.gsub(WHITESPACE_RUN, " ")
|
|
254
|
+
value = value.gsub(WHITESPACE_RUN, " ") if value.match?(COLLAPSIBLE)
|
|
247
255
|
value = value[1..] if value.start_with?(" ") && @open == @nodes.size - 1 && strip_leading_space?(dom, top)
|
|
248
256
|
elsif preserve == :full
|
|
249
257
|
value = value.gsub(/\r\n?/, "\n")
|
|
@@ -388,8 +396,11 @@ module Prosemirror
|
|
|
388
396
|
route = nil
|
|
389
397
|
sync_to = nil
|
|
390
398
|
penalty = 0
|
|
391
|
-
@open
|
|
399
|
+
depth = @open
|
|
400
|
+
# A loop rather than `downto`: breaking out of its block allocates.
|
|
401
|
+
while depth >= 0
|
|
392
402
|
context = @nodes[depth]
|
|
403
|
+
depth -= 1
|
|
393
404
|
found = context.find_wrapping(type)
|
|
394
405
|
if found && (route.nil? || route.size > found.size + penalty)
|
|
395
406
|
route = found
|
|
@@ -416,12 +427,19 @@ module Prosemirror
|
|
|
416
427
|
close_extra
|
|
417
428
|
top = self.top
|
|
418
429
|
top.match &&= top.match.match_type(type)
|
|
419
|
-
|
|
430
|
+
top.content << with_marks(node, inner_marks, top)
|
|
431
|
+
true
|
|
432
|
+
end
|
|
433
|
+
|
|
434
|
+
# The node with the marks its parent allows, from the open marks and its own.
|
|
435
|
+
def with_marks(node, marks, parent)
|
|
436
|
+
return node if marks.empty? && node.marks.empty?
|
|
437
|
+
|
|
438
|
+
node_marks = (marks + node.marks).each_with_object([]) do |mark, set|
|
|
420
439
|
mark_type = @schema.mark_type(mark.type)
|
|
421
|
-
set.replace(@schema.add_mark(mark, set)) if mark_type &&
|
|
440
|
+
set.replace(@schema.add_mark(mark, set)) if mark_type && parent.type.allows_mark?(mark_type, @schema)
|
|
422
441
|
end
|
|
423
|
-
|
|
424
|
-
true
|
|
442
|
+
(node_marks == node.marks) ? node : node.with(marks: node_marks.freeze)
|
|
425
443
|
end
|
|
426
444
|
|
|
427
445
|
def enter(type, attrs, marks, preserve_ws)
|
|
@@ -458,18 +476,23 @@ module Prosemirror
|
|
|
458
476
|
|
|
459
477
|
# Finishes the nodes above the open one, and adds them to their parents.
|
|
460
478
|
def close_extra
|
|
479
|
+
return if @nodes.size <= @open + 1
|
|
480
|
+
|
|
461
481
|
(@nodes.size - 1).downto(@open + 1) { |index| @nodes[index - 1].content << @nodes[index].finish }
|
|
462
482
|
@nodes.slice!((@open + 1)..)
|
|
463
483
|
end
|
|
464
484
|
|
|
485
|
+
# A loop rather than `downto`: returning from its block allocates, and this runs for each element.
|
|
465
486
|
def sync(to)
|
|
466
|
-
@open
|
|
487
|
+
index = @open
|
|
488
|
+
while index >= 0
|
|
467
489
|
if @nodes[index].equal?(to)
|
|
468
490
|
@open = index
|
|
469
491
|
return true
|
|
470
492
|
elsif @local_preserve_ws
|
|
471
493
|
@nodes[index].options |= PRESERVE_WS
|
|
472
494
|
end
|
|
495
|
+
index -= 1
|
|
473
496
|
end
|
|
474
497
|
false
|
|
475
498
|
end
|
|
@@ -494,8 +517,10 @@ module Prosemirror
|
|
|
494
517
|
dom.children.replace(kept)
|
|
495
518
|
end
|
|
496
519
|
|
|
520
|
+
# The value is frozen in place, so that the node doesn't copy it: the parser made it, or the element
|
|
521
|
+
# tree it parses holds it, unchanged.
|
|
497
522
|
def text_node(value)
|
|
498
|
-
Node.new(type: :text, text: value)
|
|
523
|
+
Node.new(type: :text, text: value.freeze)
|
|
499
524
|
end
|
|
500
525
|
|
|
501
526
|
def node_type(node)
|
|
@@ -4,6 +4,7 @@ module Prosemirror
|
|
|
4
4
|
# Builds nodes from ProseMirror JSON, and normalizes them as ProseMirror does:
|
|
5
5
|
# marks sorted in schema order, adjacent text with the same marks joined, empty text removed.
|
|
6
6
|
# Invalid parts are fixed and reported to the schema (which raises in strict mode).
|
|
7
|
+
# @api private
|
|
7
8
|
class JSONParser
|
|
8
9
|
MAX_DEPTH = 100
|
|
9
10
|
EMPTY = [].freeze
|
|
@@ -28,7 +29,7 @@ module Prosemirror
|
|
|
28
29
|
invalid("the top node is #{root.type}, not #{@schema.top_node}")
|
|
29
30
|
Node.new(type: @schema.top_node, content: [root])
|
|
30
31
|
rescue JSON::NestingError
|
|
31
|
-
raise
|
|
32
|
+
raise TooDeep, "the document is nested more than #{MAX_DEPTH} levels deep"
|
|
32
33
|
end
|
|
33
34
|
|
|
34
35
|
private
|
|
@@ -15,6 +15,7 @@ module Prosemirror
|
|
|
15
15
|
include Rendering
|
|
16
16
|
|
|
17
17
|
class << self
|
|
18
|
+
# @api private
|
|
18
19
|
def base_type?
|
|
19
20
|
self == MarkType
|
|
20
21
|
end
|
|
@@ -55,6 +56,7 @@ module Prosemirror
|
|
|
55
56
|
@markdown_spec = {open:, close:, mixable:, expel_enclosing_whitespace:, escape:}.freeze
|
|
56
57
|
end
|
|
57
58
|
|
|
59
|
+
# @api private
|
|
58
60
|
def markdown_spec
|
|
59
61
|
inherited_setting(:@markdown_spec, nil)
|
|
60
62
|
end
|
|
@@ -13,6 +13,7 @@ module Prosemirror
|
|
|
13
13
|
# stays as text.
|
|
14
14
|
# - Code blocks lose their final newline, as in prosemirror-markdown.
|
|
15
15
|
# - YAML front matter, between `---` lines at the start, is dropped.
|
|
16
|
+
# @api private
|
|
16
17
|
class Builder
|
|
17
18
|
MAX_DEPTH = HTML::Builder::MAX_DEPTH
|
|
18
19
|
EXTENSIONS = {
|
|
@@ -69,7 +70,7 @@ module Prosemirror
|
|
|
69
70
|
def parse(markdown)
|
|
70
71
|
Commonmarker.parse(markdown, options: {extension: EXTENSIONS})
|
|
71
72
|
rescue Commonmarker::NestingDepthExceededError => error
|
|
72
|
-
raise
|
|
73
|
+
raise TooDeep, "Markdown nested too deeply: #{error.message}"
|
|
73
74
|
end
|
|
74
75
|
|
|
75
76
|
# Walks the tree without recursion. Each item of the stack is a node, the element to add it to, and
|