Only allow a single H1 on the first line only #3068
Replies: 4 comments 3 replies
-
+1 on this I opted to create a completely separate editor for my title, limiting its input with const titleEditor = new Editor({
editorProps: {
attributes: {
class: 'article-title',
},
},
content: 'Untitled Article',
extensions: [
Document,
Text,
Heading.configure({ levels: [1] })
],
}) This makes it so that the title editor can only have I was originally just using a simple input, but I didn't want to bother with the hassle of implementing the height resizing logic. |
Beta Was this translation helpful? Give feedback.
-
I created a custom node extension called "Title" and then limited the document schema. All it does is wrap content within an h1 tag. You also have a bit more flexibility as with a custom extension, you can prevent adding marks if you want to the title. If I get the chance, I might post an update on how it works out later. import { mergeAttributes, Node } from "@tiptap/core";
export type Level = 1 | 2 | 3 | 4 | 5 | 6;
export interface ITitleOptions {
level: Level;
HTMLAttributes: Record<string, any>;
}
declare module "@tiptap/core" {
// eslint-disable-next-line @typescript-eslint/naming-convention
// interface Commands<ReturnType> {
// Custom commands go here. Leaving this snippet for any devs needing to make their own extension
// }
}
export const Title = Node.create<ITitleOptions>({
name: "title",
addOptions() {
return {
level: 1,
HTMLAttributes: {},
};
},
content: "text*",
marks: "",
group: "block",
defining: true,
renderHTML({ HTMLAttributes }) {
const level = this.options.level;
return [`h${level}`, mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0];
},
});
export default Title; Then for the schema I just did this: document: {
extend: {
content: "title block+",
},
},
` |
Beta Was this translation helpful? Give feedback.
-
I know this is an old thread @C-Hess thank you for that, is exactly what I needed, my one issue is hitting enter creates a new I am fairly new to TipTap so am struggling to find a suitable solution to only allow one UPDATE: for anyone else struggling with this I ended up preventing the user using enter in the custom title extension, I found the solution here: #2948 |
Beta Was this translation helpful? Give feedback.
-
Try extend the existing import Document from "@tiptap/extension-document";
import Heading from "@tiptap/extension-heading";
import Paragraph from "@tiptap/extension-paragraph";
import Text from "@tiptap/extension-text";
import { Editor } from "@tiptap/core";
const DocHeading = Heading.extend({
name: "h1",
group: "none", // <- prevent it from being considered as a `block` in the body of the document
}).configure({ levels: [1] });
const BodyHeading = Heading.extend({
name: "h23456",
}).configure({ levels: [2, 3, 4, 5, 6] });
const CustomDocument = Document.extend({ content: "docHeading block*" });
// ------ An example config in vanilla JS API ------
const editor = new Editor({
extensions: [CustomDocument, DocHeading, BodyHeading, Paragraph, Text],
}); |
Beta Was this translation helpful? Give feedback.
-
Hi guys,
Thanks for the great project! I'm wondering if it's possible to allow only a single H1 on the first line and for the rest of the editing experience limit the headings to H2's and H3's? The reason for this is we want our HTML to be as semantically correct as possible and not allow multiple H1's to be rendered on a page/post :)
Hope you can help!
Beta Was this translation helpful? Give feedback.
All reactions