bumpover icon indicating copy to clipboard operation
bumpover copied to clipboard

Extensible rule getter

Open doodlewind opened this issue 6 years ago • 1 comments

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

Feature

We have a well defined rule runner for now, whereas for a large doc shape, writing rules in flat array may not scale up as expected.

One solution is to expose the getRule to user, leaving them to get and even compose rules, like what chainCommand did in ProseMirror. But then rules may not be enforced as flat array.

This is a very premature idea. New API may change whenever better thoughts are proposed.

doodlewind avatar Jan 08 '18 23:01 doodlewind

A simple pattern can be used to group similar rules:

export const rules = [
  // Table
  {
    match: ({ name }) => {
      const tableTags = [
        'table', 'colgroup', 'col', 'thead', 'tbody', 'tr', 'th', 'td'
      ]
      return tableTags.includes(name)
    },
    update: node => Promise.resolve({ node })
  },
  // Text
  {
    match: ({ type }) => type === 'text',
    update: node => Promise.resolve({ node })
  },
  {
    match: ({ name }) => {
      const textTags = [
        'p', 'pre', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'strong', 'em', 'br'
      ]
      return textTags.includes(name)
    },
    update: node => Promise.resolve({ node })
  },
  {
    match: ({ name }) => name === 'a',
    update: node => Promise.resolve({ node })
  },
  // List
  {
    match: ({ name }) => {
      const listTags = ['ul', 'ol', 'li']
      return listTags.includes(name)
    },
    update: node => Promise.resolve({ node })
  }
]

Let's see if this is sufficient.

P.S. We're solving a question similar to iptables, some ideas may be borrowed from there.

doodlewind avatar Jan 09 '18 06:01 doodlewind