inputrules.ts 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { NodeType, Schema } from 'prosemirror-model'
  2. import {
  3. inputRules,
  4. wrappingInputRule,
  5. textblockTypeInputRule,
  6. smartQuotes,
  7. emDash,
  8. ellipsis,
  9. } from 'prosemirror-inputrules'
  10. const blockQuoteRule = (nodeType: NodeType) => wrappingInputRule(/^\s*>\s$/, nodeType)
  11. const orderedListRule = (nodeType: NodeType) => (
  12. wrappingInputRule(
  13. /^(\d+)\.\s$/,
  14. nodeType,
  15. match => ({order: +match[1]}),
  16. (match, node) => node.childCount + node.attrs.order == +match[1],
  17. )
  18. )
  19. const bulletListRule = (nodeType: NodeType) => wrappingInputRule(/^\s*([-+*])\s$/, nodeType)
  20. const codeBlockRule = (nodeType: NodeType) => textblockTypeInputRule(/^```$/, nodeType)
  21. export const buildInputRules = (schema: Schema) => {
  22. const rules = [
  23. ...smartQuotes,
  24. ellipsis,
  25. emDash,
  26. ]
  27. rules.push(blockQuoteRule(schema.nodes.blockquote))
  28. rules.push(orderedListRule(schema.nodes.ordered_list))
  29. rules.push(bulletListRule(schema.nodes.bullet_list))
  30. rules.push(codeBlockRule(schema.nodes.code_block))
  31. return inputRules({ rules })
  32. }