add editor rule to ensure plain text in code blocks
This commit is contained in:
parent
fd606938e2
commit
a89427dd8b
@ -40,6 +40,39 @@ const shortcodesAtRoot = {
|
||||
},
|
||||
};
|
||||
|
||||
const rules = [ enforceNeverEmpty, shortcodesAtRoot ];
|
||||
/**
|
||||
* Ensure that trailing shortcodes are followed by an empty paragraph.
|
||||
*/
|
||||
const noTrailingShortcodes = {
|
||||
match: object => object.kind === 'document',
|
||||
validate: doc => {
|
||||
return doc.findDescendant(node => {
|
||||
return node.type === 'shortcode' && doc.getBlocks().last().key === node.key;
|
||||
});
|
||||
},
|
||||
normalize: (change, doc, node) => {
|
||||
const text = Text.create('');
|
||||
const block = Block.create({ type: 'paragraph', nodes: [ text ] });
|
||||
return change.insertNodeByKey(doc.key, doc.get('nodes').size, block);
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* Ensure that code blocks contain no marks.
|
||||
*/
|
||||
const codeBlocksContainPlainText = {
|
||||
match: node => node.type === 'code',
|
||||
validate: node => {
|
||||
const invalidChild = node.getTexts().find(text => !text.getMarks().isEmpty());
|
||||
return invalidChild || null;
|
||||
},
|
||||
normalize: (change, node, invalidChild) => {
|
||||
invalidChild.getMarks().forEach(mark => {
|
||||
change.removeMarkByKey(invalidChild.key, 0, invalidChild.get('characters').size, mark);
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
const rules = [ enforceNeverEmpty, shortcodesAtRoot, codeBlocksContainPlainText ];
|
||||
|
||||
export default rules;
|
||||
|
Loading…
x
Reference in New Issue
Block a user