Skip to content

Commit

Permalink
feat: Provide cleanupTitle to trim text contents of title elements
Browse files Browse the repository at this point in the history
Fixes #2052
  • Loading branch information
jdforrester committed Jan 27, 2025
1 parent b1e50bd commit 6f74713
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 0 deletions.
9 changes: 9 additions & 0 deletions docs/04-plugins/cleanupTitle.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
title: cleanupTitle
svgo:
pluginId: cleanupTitle
---

Trims the text contents of [`<title>`](https://developer.mozilla.org/docs/Web/SVG/Element/title) elements.

:::
2 changes: 2 additions & 0 deletions lib/builtin.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as cleanupEnableBackground from '../plugins/cleanupEnableBackground.js'
import * as cleanupIds from '../plugins/cleanupIds.js';
import * as cleanupListOfValues from '../plugins/cleanupListOfValues.js';
import * as cleanupNumericValues from '../plugins/cleanupNumericValues.js';
import * as cleanupTitle from '../plugins/cleanupTitle.js';
import * as collapseGroups from '../plugins/collapseGroups.js';
import * as convertColors from '../plugins/convertColors.js';
import * as convertEllipseToCircle from '../plugins/convertEllipseToCircle.js';
Expand Down Expand Up @@ -62,6 +63,7 @@ export const builtin = Object.freeze([
cleanupIds,
cleanupListOfValues,
cleanupNumericValues,
cleanupTitle,
collapseGroups,
convertColors,
convertEllipseToCircle,
Expand Down
24 changes: 24 additions & 0 deletions plugins/cleanupTitle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export const name = 'cleanupTitle';
export const description = 'trims the text contents of <title> elements';

/**
* Trims the text contents of <title> elements.
*
* @type {import('./plugins-types.js').Plugin<'cleanupTitle'>}
*/
export const fn = () => {
return {
element: {
enter: (node) => {
if (node.name === 'title') {
node.children = node.children.map((child) => {
if (child.type === 'text') {
child.value = child.value.trim();
}
return child;
});
}
},
},
};
};
1 change: 1 addition & 0 deletions plugins/plugins-types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type DefaultPlugins = {
defaultPx?: boolean;
convertToPx?: boolean;
};
cleanupTitle: void;
collapseGroups: void;
convertColors: {
currentColor?: boolean | string | RegExp;
Expand Down
11 changes: 11 additions & 0 deletions test/plugins/cleanupTitle.01.svg.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<svg xmlns="http://www.w3.org/2000/svg">
<title> A title isn't indented, but is escaped and terminally trimmed. </title>
<g/>
</svg>

@@@

<svg xmlns="http://www.w3.org/2000/svg">
<title>A title isn&apos;t indented, but is escaped and terminally trimmed.</title>
<g/>
</svg>

0 comments on commit 6f74713

Please sign in to comment.