{"version":3,"sources":["../src/index.ts","../src/highlight.ts"],"sourcesContent":["import { Highlight } from './highlight.js'\n\nexport * from './highlight.js'\n\nexport default Highlight\n","import { Mark, markInputRule, markPasteRule, mergeAttributes } from '@tiptap/core'\n\nexport interface HighlightOptions {\n  /**\n   * Allow multiple highlight colors\n   * @default false\n   * @example true\n   */\n  multicolor: boolean\n\n  /**\n   * HTML attributes to add to the highlight element.\n   * @default {}\n   * @example { class: 'foo' }\n   */\n  HTMLAttributes: Record<string, any>\n}\n\ndeclare module '@tiptap/core' {\n  interface Commands<ReturnType> {\n    highlight: {\n      /**\n       * Set a highlight mark\n       * @param attributes The highlight attributes\n       * @example editor.commands.setHighlight({ color: 'red' })\n       */\n      setHighlight: (attributes?: { color: string }) => ReturnType\n      /**\n       * Toggle a highlight mark\n       * @param attributes The highlight attributes\n       * @example editor.commands.toggleHighlight({ color: 'red' })\n       */\n      toggleHighlight: (attributes?: { color: string }) => ReturnType\n      /**\n       * Unset a highlight mark\n       * @example editor.commands.unsetHighlight()\n       */\n      unsetHighlight: () => ReturnType\n    }\n  }\n}\n\n/**\n * Matches a highlight to a ==highlight== on input.\n */\nexport const inputRegex = /(?:^|\\s)(==(?!\\s+==)((?:[^=]+))==(?!\\s+==))$/\n\n/**\n * Matches a highlight to a ==highlight== on paste.\n */\nexport const pasteRegex = /(?:^|\\s)(==(?!\\s+==)((?:[^=]+))==(?!\\s+==))/g\n\n/**\n * This extension allows you to highlight text.\n * @see https://www.tiptap.dev/api/marks/highlight\n */\nexport const Highlight = Mark.create<HighlightOptions>({\n  name: 'highlight',\n\n  addOptions() {\n    return {\n      multicolor: false,\n      HTMLAttributes: {},\n    }\n  },\n\n  addAttributes() {\n    if (!this.options.multicolor) {\n      return {}\n    }\n\n    return {\n      color: {\n        default: null,\n        parseHTML: element => element.getAttribute('data-color') || element.style.backgroundColor,\n        renderHTML: attributes => {\n          if (!attributes.color) {\n            return {}\n          }\n\n          return {\n            'data-color': attributes.color,\n            style: `background-color: ${attributes.color}; color: inherit`,\n          }\n        },\n      },\n    }\n  },\n\n  parseHTML() {\n    return [\n      {\n        tag: 'mark',\n      },\n    ]\n  },\n\n  renderHTML({ HTMLAttributes }) {\n    return ['mark', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0]\n  },\n\n  renderMarkdown: (node, h) => {\n    return `==${h.renderChildren(node)}==`\n  },\n\n  parseMarkdown: (token, h) => {\n    return h.applyMark('highlight', h.parseInline(token.tokens || []))\n  },\n\n  markdownTokenizer: {\n    name: 'highlight',\n    level: 'inline',\n    start: (src: string) => src.indexOf('=='),\n    tokenize(src, _, h) {\n      const rule = /^(==)([^=]+)(==)/ // ==highlighted text==\n      const match = rule.exec(src)\n\n      if (match) {\n        const innerContent = match[2].trim()\n\n        const children = h.inlineTokens(innerContent)\n\n        return {\n          type: 'highlight',\n          raw: match[0],\n          text: innerContent,\n          tokens: children,\n        }\n      }\n    },\n  },\n\n  addCommands() {\n    return {\n      setHighlight:\n        attributes =>\n        ({ commands }) => {\n          return commands.setMark(this.name, attributes)\n        },\n      toggleHighlight:\n        attributes =>\n        ({ commands }) => {\n          return commands.toggleMark(this.name, attributes)\n        },\n      unsetHighlight:\n        () =>\n        ({ commands }) => {\n          return commands.unsetMark(this.name)\n        },\n    }\n  },\n\n  addKeyboardShortcuts() {\n    return {\n      'Mod-Shift-h': () => this.editor.commands.toggleHighlight(),\n    }\n  },\n\n  addInputRules() {\n    return [\n      markInputRule({\n        find: inputRegex,\n        type: this.type,\n      }),\n    ]\n  },\n\n  addPasteRules() {\n    return [\n      markPasteRule({\n        find: pasteRegex,\n        type: this.type,\n      }),\n    ]\n  },\n})\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,kBAAoE;AA6C7D,IAAM,aAAa;AAKnB,IAAM,aAAa;AAMnB,IAAM,YAAY,iBAAK,OAAyB;AAAA,EACrD,MAAM;AAAA,EAEN,aAAa;AACX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