Extension to allow spans #3136
Unanswered
IHIutch
asked this question in
Questions & Help
Replies: 2 comments 3 replies
-
The following worked for me: import { Editor, StarterKit, Extension, Mark, mergeAttributes } from '@tiptap/core';
/** This allows a plain `<span>` (but not any attributes on it) */
export interface SpanOptions {
HTMLAttributes: Record<string, any>,
}
export const Span = Mark.create<SpanOptions>({
name: 'span',
addOptions() {
return {
HTMLAttributes: {},
};
},
parseHTML() {
return [
{
tag: 'span',
getAttrs: element => {
return {};
},
},
]
},
renderHTML({ HTMLAttributes }) {
return ['span', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0];
},
});
/** This allows a `class="..."` attribute specifically on `<p>` only. */
export const ParagraphAttributes = Extension.create({
name: 'paragraph-attributes',
addGlobalAttributes() {
return [{
types: [
'paragraph',
],
attributes: {
class: {
default: undefined,
},
},
}];
},
});
...
let editor = new Editor({
element: editorContentEl,
extensions: [
StarterKit,
Span,
ParagraphAttributes,
],
content: html,
...
}); |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Having a little trouble with custom extensions. All I want to do is allow
<span>
tags inside a text block. Would anyone be able to point me in the right direction?Beta Was this translation helpful? Give feedback.
All reactions