Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Provide cleanupTitle to trim text contents of title elements #2091

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
13 changes: 13 additions & 0 deletions test/plugins/removeDesc.02.svg.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<svg xmlns="http://www.w3.org/2000/svg">
<desc> A non-standard description isn't removed, but is escaped, terminally trimmed and indented. </desc>
<g/>
</svg>

@@@

<svg xmlns="http://www.w3.org/2000/svg">
<desc>
A non-standard description isn&apos;t removed, but is escaped, terminally trimmed and indented.
</desc>
<g/>
</svg>
Loading