From ddc71f792a0317ff4104cea6660e621924be922f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Wed, 25 Nov 2020 09:50:54 +0100 Subject: [PATCH] manually merge HTMLAttributes --- packages/core/src/utils/getSchema.ts | 10 ++-------- packages/extension-blockquote/src/index.ts | 4 ++-- packages/extension-bold/src/index.ts | 3 ++- packages/extension-bullet-list/src/index.ts | 4 ++-- packages/extension-code-block/src/index.ts | 2 +- packages/extension-code/src/index.ts | 3 ++- packages/extension-collaboration/src/index.ts | 5 ++++- packages/extension-hard-break/src/index.ts | 15 +++++++++++++-- packages/extension-heading/src/index.ts | 4 ++-- packages/extension-highlight/src/index.ts | 3 ++- .../extension-horizontal-rule/src/index.ts | 9 +++++++-- packages/extension-image/src/index.ts | 9 +++++++-- packages/extension-italic/src/index.ts | 3 ++- packages/extension-link/src/index.ts | 9 +++++++-- packages/extension-list-item/src/index.ts | 4 ++-- packages/extension-ordered-list/src/index.ts | 6 +++--- packages/extension-paragraph/src/index.ts | 4 ++-- packages/extension-strike/src/index.ts | 3 ++- packages/extension-task-item/src/index.ts | 6 +++++- packages/extension-text-style/src/index.ts | 19 +++++++++++++++++-- packages/extension-underline/src/index.ts | 4 ++-- 21 files changed, 88 insertions(+), 41 deletions(-) diff --git a/packages/core/src/utils/getSchema.ts b/packages/core/src/utils/getSchema.ts index 39bb24c4..c7b2e436 100644 --- a/packages/core/src/utils/getSchema.ts +++ b/packages/core/src/utils/getSchema.ts @@ -51,10 +51,7 @@ export default function getSchema(extensions: Extensions): Schema { if (extension.config.renderHTML) { schema.toDOM = node => (extension.config.renderHTML as Function)?.bind(context)({ node, - HTMLAttributes: mergeAttributes( - extension.options.HTMLAttributes, - getRenderedAttributes(node, extensionAttributes), - ), + HTMLAttributes: getRenderedAttributes(node, extensionAttributes), }) } @@ -83,10 +80,7 @@ export default function getSchema(extensions: Extensions): Schema { if (extension.config.renderHTML) { schema.toDOM = mark => (extension.config.renderHTML as Function)?.bind(context)({ mark, - HTMLAttributes: mergeAttributes( - extension.options.HTMLAttributes, - getRenderedAttributes(mark, extensionAttributes), - ), + HTMLAttributes: getRenderedAttributes(mark, extensionAttributes), }) } diff --git a/packages/extension-blockquote/src/index.ts b/packages/extension-blockquote/src/index.ts index 970cc230..6e9bcf41 100644 --- a/packages/extension-blockquote/src/index.ts +++ b/packages/extension-blockquote/src/index.ts @@ -1,4 +1,4 @@ -import { Command, Node } from '@tiptap/core' +import { Command, Node, mergeAttributes } from '@tiptap/core' import { wrappingInputRule } from 'prosemirror-inputrules' export interface BlockquoteOptions { @@ -29,7 +29,7 @@ const Blockquote = Node.create({ }, renderHTML({ HTMLAttributes }) { - return ['blockquote', HTMLAttributes, 0] + return ['blockquote', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0] }, addCommands() { diff --git a/packages/extension-bold/src/index.ts b/packages/extension-bold/src/index.ts index 07908f2d..eb5f5f7f 100644 --- a/packages/extension-bold/src/index.ts +++ b/packages/extension-bold/src/index.ts @@ -3,6 +3,7 @@ import { Mark, markInputRule, markPasteRule, + mergeAttributes, } from '@tiptap/core' export interface BoldOptions { @@ -40,7 +41,7 @@ const Bold = Mark.create({ }, renderHTML({ HTMLAttributes }) { - return ['strong', HTMLAttributes, 0] + return ['strong', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0] }, addCommands() { diff --git a/packages/extension-bullet-list/src/index.ts b/packages/extension-bullet-list/src/index.ts index 679c685c..5954a862 100644 --- a/packages/extension-bullet-list/src/index.ts +++ b/packages/extension-bullet-list/src/index.ts @@ -1,4 +1,4 @@ -import { Command, Node } from '@tiptap/core' +import { Command, Node, mergeAttributes } from '@tiptap/core' import { wrappingInputRule } from 'prosemirror-inputrules' export interface BulletListOptions { @@ -27,7 +27,7 @@ const BulletList = Node.create({ }, renderHTML({ HTMLAttributes }) { - return ['ul', HTMLAttributes, 0] + return ['ul', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0] }, addCommands() { diff --git a/packages/extension-code-block/src/index.ts b/packages/extension-code-block/src/index.ts index 12856e88..623abf19 100644 --- a/packages/extension-code-block/src/index.ts +++ b/packages/extension-code-block/src/index.ts @@ -69,7 +69,7 @@ const CodeBlock = Node.create({ }, renderHTML({ HTMLAttributes }) { - return ['pre', ['code', HTMLAttributes, 0]] + return ['pre', this.options.HTMLAttributes, ['code', HTMLAttributes, 0]] }, addCommands() { diff --git a/packages/extension-code/src/index.ts b/packages/extension-code/src/index.ts index 575c89a1..c3cc4fac 100644 --- a/packages/extension-code/src/index.ts +++ b/packages/extension-code/src/index.ts @@ -3,6 +3,7 @@ import { Mark, markInputRule, markPasteRule, + mergeAttributes, } from '@tiptap/core' export interface CodeOptions { @@ -30,7 +31,7 @@ const Code = Mark.create({ }, renderHTML({ HTMLAttributes }) { - return ['code', HTMLAttributes, 0] + return ['code', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0] }, addCommands() { diff --git a/packages/extension-collaboration/src/index.ts b/packages/extension-collaboration/src/index.ts index 485b64f5..d0e686c0 100644 --- a/packages/extension-collaboration/src/index.ts +++ b/packages/extension-collaboration/src/index.ts @@ -1,6 +1,9 @@ import { Extension } from '@tiptap/core' import { - redo, undo, ySyncPlugin, yUndoPlugin, + redo, + undo, + ySyncPlugin, + yUndoPlugin, } from 'y-prosemirror' export interface CollaborationOptions { diff --git a/packages/extension-hard-break/src/index.ts b/packages/extension-hard-break/src/index.ts index b150d5d9..19dd32b2 100644 --- a/packages/extension-hard-break/src/index.ts +++ b/packages/extension-hard-break/src/index.ts @@ -1,9 +1,20 @@ -import { Command, Node } from '@tiptap/core' +import { Command, Node, mergeAttributes } from '@tiptap/core' import { exitCode } from 'prosemirror-commands' +export interface HardBreakOptions { + HTMLAttributes: { + [key: string]: any + }, +} + const HardBreak = Node.create({ name: 'hardBreak', + defaultOptions: { + languageClassPrefix: 'language-', + HTMLAttributes: {}, + }, + inline: true, group: 'inline', @@ -17,7 +28,7 @@ const HardBreak = Node.create({ }, renderHTML({ HTMLAttributes }) { - return ['br', HTMLAttributes] + return ['br', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes)] }, addCommands() { diff --git a/packages/extension-heading/src/index.ts b/packages/extension-heading/src/index.ts index 553468d1..33b3fa31 100644 --- a/packages/extension-heading/src/index.ts +++ b/packages/extension-heading/src/index.ts @@ -1,4 +1,4 @@ -import { Command, Node } from '@tiptap/core' +import { Command, Node, mergeAttributes } from '@tiptap/core' import { textblockTypeInputRule } from 'prosemirror-inputrules' type Level = 1 | 2 | 3 | 4 | 5 | 6 @@ -47,7 +47,7 @@ const Heading = Node.create({ ? node.attrs.level : this.options.levels[0] - return [`h${level}`, HTMLAttributes, 0] + return [`h${level}`, mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0] }, addCommands() { diff --git a/packages/extension-highlight/src/index.ts b/packages/extension-highlight/src/index.ts index e0117574..cd172077 100644 --- a/packages/extension-highlight/src/index.ts +++ b/packages/extension-highlight/src/index.ts @@ -3,6 +3,7 @@ import { Mark, markInputRule, markPasteRule, + mergeAttributes, } from '@tiptap/core' export interface HighlightOptions { @@ -53,7 +54,7 @@ const Highlight = Mark.create({ }, renderHTML({ HTMLAttributes }) { - return ['mark', HTMLAttributes, 0] + return ['mark', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0] }, addCommands() { diff --git a/packages/extension-horizontal-rule/src/index.ts b/packages/extension-horizontal-rule/src/index.ts index a6765ddc..1831e637 100644 --- a/packages/extension-horizontal-rule/src/index.ts +++ b/packages/extension-horizontal-rule/src/index.ts @@ -1,4 +1,9 @@ -import { Command, Node, nodeInputRule } from '@tiptap/core' +import { + Command, + Node, + nodeInputRule, + mergeAttributes, +} from '@tiptap/core' export interface HorizontalRuleOptions { HTMLAttributes: { @@ -22,7 +27,7 @@ const HorizontalRule = Node.create({ }, renderHTML({ HTMLAttributes }) { - return ['hr', HTMLAttributes] + return ['hr', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes)] }, addCommands() { diff --git a/packages/extension-image/src/index.ts b/packages/extension-image/src/index.ts index a4c67761..4b4efc52 100644 --- a/packages/extension-image/src/index.ts +++ b/packages/extension-image/src/index.ts @@ -1,4 +1,9 @@ -import { Command, Node, nodeInputRule } from '@tiptap/core' +import { + Command, + Node, + nodeInputRule, + mergeAttributes, +} from '@tiptap/core' export interface ImageOptions { inline: boolean, @@ -50,7 +55,7 @@ const Image = Node.create({ }, renderHTML({ HTMLAttributes }) { - return ['img', HTMLAttributes] + return ['img', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes)] }, addCommands() { diff --git a/packages/extension-italic/src/index.ts b/packages/extension-italic/src/index.ts index 43656a1e..2378d43f 100644 --- a/packages/extension-italic/src/index.ts +++ b/packages/extension-italic/src/index.ts @@ -3,6 +3,7 @@ import { Mark, markInputRule, markPasteRule, + mergeAttributes, } from '@tiptap/core' export interface ItalicOptions { @@ -39,7 +40,7 @@ const Italic = Mark.create({ }, renderHTML({ HTMLAttributes }) { - return ['em', HTMLAttributes, 0] + return ['em', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0] }, addCommands() { diff --git a/packages/extension-link/src/index.ts b/packages/extension-link/src/index.ts index c1ec0d9c..8287cdae 100644 --- a/packages/extension-link/src/index.ts +++ b/packages/extension-link/src/index.ts @@ -1,4 +1,9 @@ -import { Command, Mark, markPasteRule } from '@tiptap/core' +import { + Command, + Mark, + markPasteRule, + mergeAttributes, +} from '@tiptap/core' import { Plugin, PluginKey } from 'prosemirror-state' export interface LinkOptions { @@ -42,7 +47,7 @@ const Link = Mark.create({ }, renderHTML({ HTMLAttributes }) { - return ['a', HTMLAttributes, 0] + return ['a', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0] }, addCommands() { diff --git a/packages/extension-list-item/src/index.ts b/packages/extension-list-item/src/index.ts index b0d88118..73a2b9e5 100644 --- a/packages/extension-list-item/src/index.ts +++ b/packages/extension-list-item/src/index.ts @@ -1,4 +1,4 @@ -import { Node } from '@tiptap/core' +import { Node, mergeAttributes } from '@tiptap/core' export interface ListItemOptions { HTMLAttributes: { @@ -26,7 +26,7 @@ const ListItem = Node.create({ }, renderHTML({ HTMLAttributes }) { - return ['li', HTMLAttributes, 0] + return ['li', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0] }, addKeyboardShortcuts() { diff --git a/packages/extension-ordered-list/src/index.ts b/packages/extension-ordered-list/src/index.ts index 71e13bb1..b97af728 100644 --- a/packages/extension-ordered-list/src/index.ts +++ b/packages/extension-ordered-list/src/index.ts @@ -1,4 +1,4 @@ -import { Command, Node } from '@tiptap/core' +import { Command, Node, mergeAttributes } from '@tiptap/core' import { wrappingInputRule } from 'prosemirror-inputrules' export interface OrderedListOptions { @@ -45,8 +45,8 @@ const OrderedList = Node.create({ const { start, ...attributesWithoutStart } = HTMLAttributes return start === 1 - ? ['ol', attributesWithoutStart, 0] - : ['ol', HTMLAttributes, 0] + ? ['ol', mergeAttributes(this.options.HTMLAttributes, attributesWithoutStart), 0] + : ['ol', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0] }, addCommands() { diff --git a/packages/extension-paragraph/src/index.ts b/packages/extension-paragraph/src/index.ts index b8804177..e0104a8b 100644 --- a/packages/extension-paragraph/src/index.ts +++ b/packages/extension-paragraph/src/index.ts @@ -1,4 +1,4 @@ -import { Command, Node } from '@tiptap/core' +import { Command, Node, mergeAttributes } from '@tiptap/core' export interface ParagraphOptions { HTMLAttributes: { @@ -24,7 +24,7 @@ const Paragraph = Node.create({ }, renderHTML({ HTMLAttributes }) { - return ['p', HTMLAttributes, 0] + return ['p', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0] }, addCommands() { diff --git a/packages/extension-strike/src/index.ts b/packages/extension-strike/src/index.ts index 9d194623..793cea54 100644 --- a/packages/extension-strike/src/index.ts +++ b/packages/extension-strike/src/index.ts @@ -3,6 +3,7 @@ import { Mark, markInputRule, markPasteRule, + mergeAttributes, } from '@tiptap/core' export interface StrikeOptions { @@ -39,7 +40,7 @@ const Strike = Mark.create({ }, renderHTML({ HTMLAttributes }) { - return ['s', HTMLAttributes, 0] + return ['s', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0] }, addCommands() { diff --git a/packages/extension-task-item/src/index.ts b/packages/extension-task-item/src/index.ts index ce7c0ab3..85312070 100644 --- a/packages/extension-task-item/src/index.ts +++ b/packages/extension-task-item/src/index.ts @@ -48,7 +48,11 @@ const TaskItem = Node.create({ }, renderHTML({ HTMLAttributes }) { - return ['li', mergeAttributes(HTMLAttributes, { 'data-type': 'taskItem' }), 0] + return ['li', mergeAttributes( + this.options.HTMLAttributes, + HTMLAttributes, + { 'data-type': 'taskItem' }, + ), 0] }, addKeyboardShortcuts() { diff --git a/packages/extension-text-style/src/index.ts b/packages/extension-text-style/src/index.ts index d32bb276..bd7abd6a 100644 --- a/packages/extension-text-style/src/index.ts +++ b/packages/extension-text-style/src/index.ts @@ -1,8 +1,23 @@ -import { Command, Mark, getMarkAttributes } from '@tiptap/core' +import { + Command, + Mark, + getMarkAttributes, + mergeAttributes, +} from '@tiptap/core' + +export interface TextStyleOptions { + HTMLAttributes: { + [key: string]: any + }, +} const TextStyle = Mark.create({ name: 'textStyle', + defaultOptions: { + HTMLAttributes: {}, + }, + parseHTML() { return [ { @@ -21,7 +36,7 @@ const TextStyle = Mark.create({ }, renderHTML({ HTMLAttributes }) { - return ['span', HTMLAttributes, 0] + return ['span', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0] }, addCommands() { diff --git a/packages/extension-underline/src/index.ts b/packages/extension-underline/src/index.ts index 11ac411c..45fde3a1 100644 --- a/packages/extension-underline/src/index.ts +++ b/packages/extension-underline/src/index.ts @@ -1,4 +1,4 @@ -import { Command, Mark } from '@tiptap/core' +import { Command, Mark, mergeAttributes } from '@tiptap/core' export interface UnderlineOptions { HTMLAttributes: { @@ -25,7 +25,7 @@ const Underline = Mark.create({ }, renderHTML({ HTMLAttributes }) { - return ['u', HTMLAttributes, 0] + return ['u', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0] }, addCommands() {