Can InputRules match marks?


#1

possible solution #537

There is my solution

const ruleMarkTypes = [
  {
    name: 'subscript',
    rule: (markType) => {
      let getAttrs = {}
      return new InputRule(
        /\s_((?!_|\s).)+\s/,
        (state, match, start, end) => {
          let attrs = getAttrs instanceof Function ? getAttrs(match) : getAttrs
          let tr = state.tr
          end = start + match[0].length - 1

          tr.addMark(start, end, markType.create(attrs))
            .delete(start, start + 2)
            .removeStoredMark(markType)
          return tr
        })
    }
  },
  {
    name: 'superscript',
    rule: (markType) => {
      let getAttrs = {}
      return new InputRule(
        /\s\^((?!\^|\s).)+\s/,
        (state, match, start, end) => {
          let attrs = getAttrs instanceof Function ? getAttrs(match) : getAttrs
          let tr = state.tr
          end = start + match[0].length - 1

          tr.addMark(start, end, markType.create(attrs))
            .delete(start, start + 2)
            .removeStoredMark(markType)
          return tr
        })
    }
  },
  {
    name: 'strikethrough',
    rule: (markType) => {
      let getAttrs = {}
      return new InputRule(
        /\s~[\S\s]*~\s/,
        (state, match, start, end) => {
          let attrs = getAttrs instanceof Function ? getAttrs(match) : getAttrs
          let tr = state.tr
          end = start + match[0].length - 1

          tr.addMark(start, end, markType.create(attrs))
            .delete(end - 1, end)
            .delete(start, start + 2)
            .removeStoredMark(markType)
          return tr
        })
    }
  },
  {
    name: 'highlight',
    rule: (markType) => {
      let getAttrs = {}
      return new InputRule(
        /\s`[\S\s]*`\s/,
        (state, match, start, end) => {
          let attrs = getAttrs instanceof Function ? getAttrs(match) : getAttrs
          let tr = state.tr
          end = start + match[0].length - 1

          tr.addMark(start, end, markType.create(attrs))
            .delete(end - 1, end)
            .delete(start, start + 2)
            .removeStoredMark(markType)
          return tr
        })
    }
  },
  {
    name: 'em',
    rule: (markType) => {
      let getAttrs = {}
      return new InputRule(
        /\s\^[\S\s]*\^\s/,
        (state, match, start, end) => {
          let attrs = getAttrs instanceof Function ? getAttrs(match) : getAttrs
          let tr = state.tr
          end = start + match[0].length - 1

          tr.addMark(start, end, markType.create(attrs))
            .delete(end - 1, end)
            .delete(start, start + 2)
            .removeStoredMark(markType)
          return tr
        })
    }
  },
  {
    name: 'strong',
    rule: (markType) => {
      let getAttrs = {}
      return new InputRule(
        /\s\*[\S\s]*\*\s/,
        (state, match, start, end) => {
          let attrs = getAttrs instanceof Function ? getAttrs(match) : getAttrs
          let tr = state.tr
          end = start + match[0].length - 1

          tr.addMark(start, end, markType.create(attrs))
            .delete(end - 1, end)
            .delete(start, start + 2)
            .removeStoredMark(markType)
          return tr
        })
    }
  },
  {
    name: 'underline',
    rule: (markType) => {
      let getAttrs = {}
      return new InputRule(
        /\s_[\S\s]*_\s/,
        (state, match, start, end) => {
          let attrs = getAttrs instanceof Function ? getAttrs(match) : getAttrs
          let tr = state.tr
          end = start + match[0].length - 1

          tr.addMark(start, end, markType.create(attrs))
            .delete(end - 1, end)
            .delete(start, start + 2)
            .removeStoredMark(markType)
          return tr
        })
    }
  }
]


Screenshot:

Jan-08-2019%2012-59-30


#4

@ marijn