-
Another question, sorry! In our project the final resting format is markdown, markdown only supports a subset of the availble formatting within tables, for example: Supported:
Not supported
So the question is; is it currently possible to restrict certain elements from being created in a table. And if it's not how would you envisage implementing that functionality. Thanks for the help once again! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
So after some deadends and more documentation digging I stumbled across "Slate schemas": https://docs.slatejs.org/v/v0.47/guides/schemas (docs) https://github.com/ianstormtaylor/slate/blob/405cef0225c314b4162d587c74cfce6b65a7b257/examples/forced-layout/index.js#L62 (example usage) How would I go abouts exposing the 'schema' Slate editor prop, it doesn't look like I can pass it into the Plate component directly, nor via editableProps. |
Beta Was this translation helpful? Give feedback.
-
For anyone thats wondering: To strip out unwanted elements from tables// See: https://docs.slatejs.org/concepts/11-normalizing
export const withPreventTableChildren = (
disallowedBlockTypes = [ELEMENT_CODE_BLOCK, ELEMENT_BLOCKQUOTE, ELEMENT_HR],
disallowedInlineTypes = [ELEMENT_H1, ELEMENT_H2, ELEMENT_H3, ELEMENT_H4, ELEMENT_H5, ELEMENT_H6]
) => (editor) => {
const { normalizeNode } = editor
editor.normalizeNode = ([currentNode, currentPath]) => {
if (!Element.isElement(currentNode) || (currentNode.type !== ELEMENT_TD && currentNode.type !== ELEMENT_TH)) {
return normalizeNode([currentNode, currentPath])
}
for (const [childNode, childPath] of Node.children(editor, currentPath)) {
const childType = childNode.type
const isInvalidBlockElement = disallowedBlockTypes.includes(childType) // Note: could combine these, but anticipating divergent use-cases
const isInvalidInlineElement = disallowedInlineTypes.includes(childType)
if (isInvalidBlockElement || isInvalidInlineElement) {
const content = childNode.children[0].text || ''
Transforms.delete(editor, { at: childPath })
Transforms.insertNodes(editor, { type: ELEMENT_PARAGRAPH, children: [{ text: content }] }, { at: childPath })
Transforms.select(editor, Editor.start(editor, childPath))
return
}
}
return normalizeNode([currentNode, currentPath])
}
return editor
}
export const createPreventTableChildrenPlugin = getPlatePluginWithOverrides(
withPreventTableChildren
) To normalize content onLoadexport const convertHtmlToPlate = (editor, plugins, html, returnDefault = undefined) => {
if (html === '') {
return returnDefault
}
// Convert HTML to plate
editor.children = deserializeHTMLToDocumentFragment(editor, {
plugins,
element: html,
stripWhitespace: false
})
Editor.normalize(editor, { force: true });
return editor.children
} Thanks to @karthikcodes6 and @dylans |
Beta Was this translation helpful? Give feedback.
For anyone thats wondering:
To strip out unwanted elements from tables