fix: add attributes to pre tag instead of code tag by default, fix #2440
This commit is contained in:
@@ -25,26 +25,20 @@ export function injectExtensionAttributesToParseRule(parseRule: ParseRule, exten
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
const newAttributes = extensionAttributes
|
const newAttributes = extensionAttributes.reduce((items, item) => {
|
||||||
.filter(item => item.attribute.rendered)
|
const value = item.attribute.parseHTML
|
||||||
.reduce((items, item) => {
|
? item.attribute.parseHTML(node as HTMLElement)
|
||||||
const value = item.attribute.parseHTML
|
: fromString((node as HTMLElement).getAttribute(item.name))
|
||||||
? item.attribute.parseHTML(node as HTMLElement)
|
|
||||||
: fromString((node as HTMLElement).getAttribute(item.name))
|
|
||||||
|
|
||||||
if (isObject(value)) {
|
if (value === null || value === undefined) {
|
||||||
console.warn(`[tiptap warn]: BREAKING CHANGE: "parseHTML" for your attribute "${item.name}" returns an object but should return the value itself. If this is expected you can ignore this message. This warning will be removed in one of the next releases. Further information: https://github.com/ueberdosis/tiptap/issues/1863`)
|
return items
|
||||||
}
|
}
|
||||||
|
|
||||||
if (value === null || value === undefined) {
|
return {
|
||||||
return items
|
...items,
|
||||||
}
|
[item.name]: value,
|
||||||
|
}
|
||||||
return {
|
}, {})
|
||||||
...items,
|
|
||||||
[item.name]: value,
|
|
||||||
}
|
|
||||||
}, {})
|
|
||||||
|
|
||||||
return { ...oldAttributes, ...newAttributes }
|
return { ...oldAttributes, ...newAttributes }
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { Node, textblockTypeInputRule } from '@tiptap/core'
|
import { Node, textblockTypeInputRule, mergeAttributes } from '@tiptap/core'
|
||||||
import { Plugin, PluginKey, TextSelection } from 'prosemirror-state'
|
import { Plugin, PluginKey, TextSelection } from 'prosemirror-state'
|
||||||
|
|
||||||
export interface CodeBlockOptions {
|
export interface CodeBlockOptions {
|
||||||
@@ -81,15 +81,7 @@ export const CodeBlock = Node.create<CodeBlockOptions>({
|
|||||||
|
|
||||||
return language
|
return language
|
||||||
},
|
},
|
||||||
renderHTML: attributes => {
|
rendered: false,
|
||||||
if (!attributes.language) {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
class: this.options.languageClassPrefix + attributes.language,
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -103,8 +95,20 @@ export const CodeBlock = Node.create<CodeBlockOptions>({
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|
||||||
renderHTML({ HTMLAttributes }) {
|
renderHTML({ node, HTMLAttributes }) {
|
||||||
return ['pre', this.options.HTMLAttributes, ['code', HTMLAttributes, 0]]
|
return [
|
||||||
|
'pre',
|
||||||
|
mergeAttributes(this.options.HTMLAttributes, HTMLAttributes),
|
||||||
|
[
|
||||||
|
'code',
|
||||||
|
{
|
||||||
|
class: node.attrs.language
|
||||||
|
? this.options.languageClassPrefix + node.attrs.language
|
||||||
|
: null,
|
||||||
|
},
|
||||||
|
0,
|
||||||
|
],
|
||||||
|
]
|
||||||
},
|
},
|
||||||
|
|
||||||
addCommands() {
|
addCommands() {
|
||||||
|
|||||||
Reference in New Issue
Block a user