removeDesc.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. 'use strict';
  2. const { detachNodeFromParent } = require('../lib/xast.js');
  3. exports.name = 'removeDesc';
  4. exports.type = 'visitor';
  5. exports.active = true;
  6. exports.description = 'removes <desc>';
  7. const standardDescs = /^(Created with|Created using)/;
  8. /**
  9. * Removes <desc>.
  10. * Removes only standard editors content or empty elements 'cause it can be used for accessibility.
  11. * Enable parameter 'removeAny' to remove any description.
  12. *
  13. * https://developer.mozilla.org/en-US/docs/Web/SVG/Element/desc
  14. *
  15. * @author Daniel Wabyick
  16. *
  17. * @type {import('../lib/types').Plugin<{ removeAny?: boolean }>}
  18. */
  19. exports.fn = (root, params) => {
  20. const { removeAny = true } = params;
  21. return {
  22. element: {
  23. enter: (node, parentNode) => {
  24. if (node.name === 'desc') {
  25. if (
  26. removeAny ||
  27. node.children.length === 0 ||
  28. (node.children[0].type === 'text' &&
  29. standardDescs.test(node.children[0].value))
  30. ) {
  31. detachNodeFromParent(node, parentNode);
  32. }
  33. }
  34. },
  35. },
  36. };
  37. };