[6a3a178] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | const { detachNodeFromParent } = require('../lib/xast.js');
|
---|
| 4 |
|
---|
| 5 | exports.name = 'removeElementsByAttr';
|
---|
| 6 | exports.type = 'visitor';
|
---|
| 7 | exports.active = false;
|
---|
| 8 | exports.description =
|
---|
| 9 | 'removes arbitrary elements by ID or className (disabled by default)';
|
---|
| 10 |
|
---|
| 11 | /**
|
---|
| 12 | * Remove arbitrary SVG elements by ID or className.
|
---|
| 13 | *
|
---|
| 14 | * @example id
|
---|
| 15 | * > single: remove element with ID of `elementID`
|
---|
| 16 | * ---
|
---|
| 17 | * removeElementsByAttr:
|
---|
| 18 | * id: 'elementID'
|
---|
| 19 | *
|
---|
| 20 | * > list: remove multiple elements by ID
|
---|
| 21 | * ---
|
---|
| 22 | * removeElementsByAttr:
|
---|
| 23 | * id:
|
---|
| 24 | * - 'elementID'
|
---|
| 25 | * - 'anotherID'
|
---|
| 26 | *
|
---|
| 27 | * @example class
|
---|
| 28 | * > single: remove all elements with class of `elementClass`
|
---|
| 29 | * ---
|
---|
| 30 | * removeElementsByAttr:
|
---|
| 31 | * class: 'elementClass'
|
---|
| 32 | *
|
---|
| 33 | * > list: remove all elements with class of `elementClass` or `anotherClass`
|
---|
| 34 | * ---
|
---|
| 35 | * removeElementsByAttr:
|
---|
| 36 | * class:
|
---|
| 37 | * - 'elementClass'
|
---|
| 38 | * - 'anotherClass'
|
---|
| 39 | *
|
---|
| 40 | * @author Eli Dupuis (@elidupuis)
|
---|
| 41 | *
|
---|
| 42 | * @type {import('../lib/types').Plugin<{
|
---|
| 43 | * id?: string | Array<string>,
|
---|
| 44 | * class?: string | Array<string>
|
---|
| 45 | * }>}
|
---|
| 46 | */
|
---|
| 47 | exports.fn = (root, params) => {
|
---|
| 48 | const ids =
|
---|
| 49 | params.id == null ? [] : Array.isArray(params.id) ? params.id : [params.id];
|
---|
| 50 | const classes =
|
---|
| 51 | params.class == null
|
---|
| 52 | ? []
|
---|
| 53 | : Array.isArray(params.class)
|
---|
| 54 | ? params.class
|
---|
| 55 | : [params.class];
|
---|
| 56 | return {
|
---|
| 57 | element: {
|
---|
| 58 | enter: (node, parentNode) => {
|
---|
| 59 | // remove element if it's `id` matches configured `id` params
|
---|
| 60 | if (node.attributes.id != null && ids.length !== 0) {
|
---|
| 61 | if (ids.includes(node.attributes.id)) {
|
---|
| 62 | detachNodeFromParent(node, parentNode);
|
---|
| 63 | }
|
---|
| 64 | }
|
---|
| 65 | // remove element if it's `class` contains any of the configured `class` params
|
---|
| 66 | if (node.attributes.class && classes.length !== 0) {
|
---|
| 67 | const classList = node.attributes.class.split(' ');
|
---|
| 68 | for (const item of classes) {
|
---|
| 69 | if (classList.includes(item)) {
|
---|
| 70 | detachNodeFromParent(node, parentNode);
|
---|
| 71 | break;
|
---|
| 72 | }
|
---|
| 73 | }
|
---|
| 74 | }
|
---|
| 75 | },
|
---|
| 76 | },
|
---|
| 77 | };
|
---|
| 78 | };
|
---|