Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add comparator functions on schema #2139

Closed
Calyhre opened this issue Aug 30, 2018 · 1 comment · Fixed by #2151
Closed

Add comparator functions on schema #2139

Calyhre opened this issue Aug 30, 2018 · 1 comment · Fixed by #2151

Comments

@Calyhre
Copy link
Collaborator

Calyhre commented Aug 30, 2018

Do you want to request a feature or report a bug?

A feature

What's the current behaviour?

Right now, we can specify rules in the schema that will match against properties of the nodes. The comparison is often as simple as a === against two attributes.

Example for a rule like this:

  // Merge adjacent text nodes.
  {
    match: { object: 'text' },
    next: [{ object: 'block' }, { object: 'inline' }],
    normalize: (change, error) => {
      // Normalize here...
    },
  },

It takes each node where object === 'text' and check if the next sibling node verifies object === 'block' or object === 'inline'. If not, it goes through the normalize method.

It's enough to cover lots of use case, but I think we can go a little further.

What's the expected behaviour?

Let's say we want to enforce no succeeding blocks of certain types. I'll take an unordered-list as example. Right now we would have to list each and every other type of block on the rule to enforce this.

  {
    match: { type: 'unordered-list' },
    next: [{ type: 'ordered-list' }, { type: 'header-one' }, ...everyOtherTypes],
    normalize: () => { /**/ },
  },

Would be really inefficient and error prone, especially for published plugins who have no idea of every other block types.

One easy way to solve this would be to inverse the condition. But I actually have no idea how to properly implement this, so here is 3 other options instead 😅:

  1. Allow function to be passed as rule, the function would return a falsey value, or the error code
     {
       match: { type: 'unordered-list' },
       next: node => node.type === 'unordered-list' && 'next_sibling_invalid_type'
     }
  2. Allow functions to be passed as matching attributes, same return as above. It would allow as to combine it with other simple conditions
     {
       match: { type: 'unordered-list' },
       next: {
         type: type => type === 'unordered-list' && 'next_sibling_invalid_type'
       },
     }
  3. Both :D

Would be as simple as adding a typeof === 'function' at a few places like here src/models/schema.js#L533

I could handle the implementation if that interests you :)

@isubasti
Copy link
Contributor

isubasti commented Sep 4, 2018

this could be useful to do some kind of negation check like not having void node as the last node rather than checking that the last node should be of type like the example schema for images. any thought @ianstormtaylor ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants