Skip to content

Instantly share code, notes, and snippets.

@Soreine
Last active November 6, 2017 00:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Soreine/b4a9413e635a40cda9509f1d213d4e41 to your computer and use it in GitHub Desktop.
Save Soreine/b4a9413e635a40cda9509f1d213d4e41 to your computer and use it in GitHub Desktop.
A function that convert an old Slate rule, to a single plugin object, containing the corresponding `validateNode` function
// Converts an old rule definition to an individual plugin with a "validateNode" function
//
// rule: {
// match,
// validate,
// normalize
// }
//
// returns {
// validateNode
// }
function toValidateNode(rule) {
return {
validateNode: (node) => {
if (!rule.match(node)) {
return undefined;
}
const validationResult = rule.validate(node);
if (validationResult == null) {
return undefined;
}
return change => rule.normalize(change, node, validationResult);
}
};
}
@sethmcleod
Copy link

Very nice! 👏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment