diff --git a/docs/src/demos/Extensions/Highlight/index.vue b/docs/src/demos/Extensions/Highlight/index.vue index aed1d5d7..1028d415 100644 --- a/docs/src/demos/Extensions/Highlight/index.vue +++ b/docs/src/demos/Extensions/Highlight/index.vue @@ -69,7 +69,8 @@ export default { content: `

This isn’t highlighted.

But that one is.

-

And this is highlighted too, but in a different color.

+

And this is highlighted too, but in a different color.

+

And this one has a data attribute.

`, }) }, diff --git a/packages/core/src/utils/markHasAttributes.ts b/packages/core/src/utils/markHasAttributes.ts index 0c22314c..415f9084 100644 --- a/packages/core/src/utils/markHasAttributes.ts +++ b/packages/core/src/utils/markHasAttributes.ts @@ -13,7 +13,7 @@ export default function markHasAttributes(state: EditorState, type: MarkType, at // @ts-ignore return Object.keys(attrs).filter((key: string) => { // @ts-ignore - console.log(attrs[key], originalAttrs[key], attrs[key] === originalAttrs[key]) + // console.log(attrs[key], originalAttrs[key], attrs[key] === originalAttrs[key]) return attrs[key] === originalAttrs[key] }).length } diff --git a/packages/extension-highlight/index.ts b/packages/extension-highlight/index.ts index fa7c7e69..113c39f9 100644 --- a/packages/extension-highlight/index.ts +++ b/packages/extension-highlight/index.ts @@ -1,11 +1,14 @@ import { - Command, createMark, + Command, createMark, markInputRule, markPasteRule, } from '@tiptap/core' export interface HighlightOptions { color: string, } +export const inputRegex = /(?:^|\s)((?:==)((?:[^~]+))(?:==))$/gm +export const pasteRegex = /(?:^|\s)((?:==)((?:[^~]+))(?:==))/gm + const Highlight = createMark({ name: 'highlight', @@ -14,8 +17,11 @@ const Highlight = createMark({ color: { default: null, parseHTML: element => { + console.log(element.getAttribute('data-color')) return { - color: element.style.backgroundColor, + color: + element.getAttribute('data-color') + || element.style.backgroundColor, } }, renderHTML: attributes => { @@ -24,6 +30,7 @@ const Highlight = createMark({ } return { + 'data-color': attributes.color, style: `background-color: ${attributes.color}`, } }, @@ -36,6 +43,14 @@ const Highlight = createMark({ { tag: 'mark', }, + { + style: 'background-color', + // getAttrs: value => { + // return { + // color: value, + // } + // }, + }, ] }, @@ -56,6 +71,18 @@ const Highlight = createMark({ 'Mod-e': () => this.editor.highlight(), } }, + + addInputRules() { + return [ + markInputRule(inputRegex, this.type), + ] + }, + + addPasteRules() { + return [ + markPasteRule(inputRegex, this.type), + ] + }, }) export default Highlight