英文:
Custom attributes gets removed when inserted from already designed template in to tiptap editor
问题
描述
我尝试插入一个预定义的模板,其中包含根据元素需要的自定义属性,比如data-element-name
,在插入到tiptap编辑器中时被删除。此外,一些HTML元素也以类似的方式被删除。
我们如何防止这些元素属性被删除?
屏幕截图、视频或GIF
- 预期插入到tiptap编辑器时的元素结构。
- 当tiptap编辑器修改它时的元素结构。
初始内容是通过在配置内容中使用content
键插入的。
英文:
Description
I was trying to insert a pre-defined template which contains custom attributes for each elements based on the element needs like data-element-name are getting removed when inserted inside the tiptap editor. Also some of the html elements are also getting removed in the similar manner.
How can we prevent the removal of these element attributes from being getting removed?
Screenshot, video, or GIF
- Element structure which expected to be as when data is being inserted to tiptap editor.
- Element structure when tiptap editor modifies it.
Initial Content was inserted from the template by using content key in the configure content
答案1
得分: 1
Tiptap(以及底层的ProseMirror)编辑器确保在加载到编辑器中时,任何未显式列入白名单的标记,包括属性、标签等,都会被去除。这种行为有助于保持标记的干净。这意味着您需要显式列入白名单这些属性(只列入您需要的属性)。
您可能正在使用Tiptap的标题扩展来处理标题标记,您可以扩展Tiptap扩展以将这些属性列入白名单 -
import { Heading as TipTapHeading } from "@tiptap/extension-heading";
export const Heading = TipTapHeading.extend({
addAttributes() {
return {
id: {
default: null, // 如果您需要不同的默认值,请更改此值
parseHTML: (element: HTMLElement) => {
// 如果存在标记值,则从标记中获取属性值,否则将属性值设为null
return element.hasAttribute("id") ? element.getAttribute("id") : null;
},
},
"data-element-name": {
default: null, // 如果您需要不同的默认值,请更改此值
parseHTML: (element: HTMLElement) => {
// 如果存在标记值,则从标记中获取属性值,否则将属性值设为null
return element.hasAttribute("data-element-name") ? element.getAttribute("data-element-name") : null;
},
}
};
}
});
然后在编辑器扩展列表中使用这个扩展,并从Tiptap中删除Heading
扩展 -
import { Editor } from '@tiptap/core';
import Document from '@tiptap/extension-document';
import Paragraph from '@tiptap/extension-paragraph';
import Text from '@tiptap/extension-text';
// import Heading from '@tiptap/extension-heading';
import Heading from './path/to/your/extension/file';
const editorInstance = new Editor({
extensions: [
Document,
Paragraph,
Text,
Heading.configure({
levels: [1, 2, 3],
}),
],
});
同样,对于从最终标记中移除的标记,您需要检查相关扩展是否在编辑器配置中(参见Nodes和Marks),如果没有,请添加它们。如果找不到支持您的标记的任何扩展,那么您需要创建自己的扩展。
此外,请查看自定义扩展文档和扩展文档,了解在扩展现有扩展或创建新扩展时可以使用的其他选项。
英文:
Tiptap (and the underlying ProseMirror) editor ensures that any markup, including attributes, tags etc., that is not explicitly whitelisted gets stripped off when it's loaded in the editor. This behaviour is desirable to keep your markup clean. That means you will need to whitelist these attributes explicitly (only the ones you want).
You are probably using the Heading extension from Tiptap to handle your heading tags, you can extend the Tiptap extension to whitelist these attributes -
import { Heading as TipTapHeading } from "@tiptap/extension-heading";
export const Heading = TipTapHeading.extend({
addAttributes() {
return {
id: {
default: null, // change this if you want a different default value
parseHTML: (element: HTMLElement) => {
// Get the attribute value from markup if it's available, otherwise assign null as the attribute value
return element.hasAttribute("id") ? element.getAttribute("id") : null;
},
},
"data-element-name": {
default: null, // change this if you want a different default value
parseHTML: (element: HTMLElement) => {
// Get the attribute value from markup if it's available, otherwise assign null as the attribute value
return element.hasAttribute("data-element-name") ? element.getAttribute("data-element-name") : null;
},
}
};
}
});
Then use this extension in editor extensions list and remove the Heading
extension from Tiptap -
import { Editor } from '@tiptap/core';
import Document from '@tiptap/extension-document';
import Paragraph from '@tiptap/extension-paragraph';
import Text from '@tiptap/extension-text';
// import Heading from '@tiptap/extension-heading';
import Heading from './path/to/your/extension/file';
const editorInstance = new Editor({
extensions: [
Document,
Paragraph,
Text,
Heading.configure({
levels: [1, 2, 3],
}),
],
});
Similarly, for the tags that are removed from the final markup, you'll need to check if the relevant extensions (see Nodes and Marks) are part of the editor config, if not add them. If you can't find any extension that supports your tag then you'll need to create your own extension.
Also, go through the custom extension documentation and extensions documentation to see other options you can use while extending an existing extension or creating a new one.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论