add editor rule to ensure plain text in code blocks

This commit is contained in:
Shawn Erquhart 2017-09-28 11:11:43 -04:00 committed by Benaiah Mischenko
parent fd606938e2
commit a89427dd8b

View File

@ -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;