| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | exports.__esModule = true;
|
|---|
| 4 | exports.default = void 0;
|
|---|
| 5 | const elements = ['svg', 'Svg'];
|
|---|
| 6 |
|
|---|
| 7 | const plugin = ({
|
|---|
| 8 | types: t
|
|---|
| 9 | }) => ({
|
|---|
| 10 | visitor: {
|
|---|
| 11 | JSXElement(path) {
|
|---|
| 12 | if (!elements.some(element => path.get('openingElement.name').isJSXIdentifier({
|
|---|
| 13 | name: element
|
|---|
| 14 | }))) {
|
|---|
| 15 | return;
|
|---|
| 16 | }
|
|---|
| 17 |
|
|---|
| 18 | function createTitle(children = [], attributes = []) {
|
|---|
| 19 | return t.jsxElement(t.jsxOpeningElement(t.jsxIdentifier('title'), attributes), t.jsxClosingElement(t.jsxIdentifier('title')), children);
|
|---|
| 20 | }
|
|---|
| 21 |
|
|---|
| 22 | function createTitleIdAttribute() {
|
|---|
| 23 | return t.jsxAttribute(t.jsxIdentifier('id'), t.jsxExpressionContainer(t.identifier('titleId')));
|
|---|
| 24 | }
|
|---|
| 25 |
|
|---|
| 26 | function enhanceAttributes(attributes) {
|
|---|
| 27 | const existingId = attributes.find(attribute => attribute.name.name === 'id');
|
|---|
| 28 |
|
|---|
| 29 | if (!existingId) {
|
|---|
| 30 | return [...attributes, createTitleIdAttribute()];
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | existingId.value = t.jsxExpressionContainer(t.logicalExpression('||', t.identifier('titleId'), existingId.value));
|
|---|
| 34 | return attributes;
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | function getTitleElement(existingTitle) {
|
|---|
| 38 | const titleExpression = t.identifier('title');
|
|---|
| 39 |
|
|---|
| 40 | if (existingTitle) {
|
|---|
| 41 | existingTitle.openingElement.attributes = enhanceAttributes(existingTitle.openingElement.attributes);
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | let titleElement = t.conditionalExpression(titleExpression, createTitle([t.jsxExpressionContainer(titleExpression)], existingTitle ? existingTitle.openingElement.attributes : [t.jsxAttribute(t.jsxIdentifier('id'), t.jsxExpressionContainer(t.identifier('titleId')))]), t.nullLiteral());
|
|---|
| 45 |
|
|---|
| 46 | if (existingTitle && existingTitle.children && existingTitle.children.length) {
|
|---|
| 47 | // if title already exists
|
|---|
| 48 | // render as follows
|
|---|
| 49 | const fallbackTitleElement = existingTitle; // {title === undefined ? fallbackTitleElement : titleElement}
|
|---|
| 50 |
|
|---|
| 51 | const conditionalExpressionForTitle = t.conditionalExpression(t.binaryExpression('===', titleExpression, t.identifier('undefined')), fallbackTitleElement, titleElement);
|
|---|
| 52 | titleElement = t.jsxExpressionContainer(conditionalExpressionForTitle);
|
|---|
| 53 | } else {
|
|---|
| 54 | titleElement = t.jsxExpressionContainer(titleElement);
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | return titleElement;
|
|---|
| 58 | } // store the title element
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 | let titleElement;
|
|---|
| 62 | const hasTitle = path.get('children').some(childPath => {
|
|---|
| 63 | if (!childPath.isJSXElement()) return false;
|
|---|
| 64 | if (childPath.node === titleElement) return false;
|
|---|
| 65 | if (childPath.node.openingElement.name.name !== 'title') return false;
|
|---|
| 66 | titleElement = getTitleElement(childPath.node);
|
|---|
| 67 | childPath.replaceWith(titleElement);
|
|---|
| 68 | return true;
|
|---|
| 69 | }); // create a title element if not already create
|
|---|
| 70 |
|
|---|
| 71 | titleElement = titleElement || getTitleElement();
|
|---|
| 72 |
|
|---|
| 73 | if (!hasTitle) {
|
|---|
| 74 | // path.unshiftContainer is not working well :(
|
|---|
| 75 | // path.unshiftContainer('children', titleElement)
|
|---|
| 76 | path.node.children.unshift(titleElement);
|
|---|
| 77 | path.replaceWith(path.node);
|
|---|
| 78 | }
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | }
|
|---|
| 82 | });
|
|---|
| 83 |
|
|---|
| 84 | var _default = plugin;
|
|---|
| 85 | exports.default = _default; |
|---|