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

removeComments Plugin not working as expected. #1811

Closed
futuremotiondev opened this issue Oct 19, 2023 · 2 comments · Fixed by #1812
Closed

removeComments Plugin not working as expected. #1811

futuremotiondev opened this issue Oct 19, 2023 · 2 comments · Fixed by #1812
Labels

Comments

@futuremotiondev
Copy link

Describe the bug
I want to remove comments from my SVG files:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512">
<!--! Font Awesome Free 6.4.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license Copyright 2023 Fonticons, Inc.-->
  <path d="M384 64c17.7 0 32 14.3 32 32v320c0 17.7-14.3 32-32 32H64c-17.7 0-32-14.3-32-32V96c0-17.7 14.3-32 32-32h320zM64 32C28.7 32 0 60.7 0 96v320c0 35.3 28.7 64 64 64h320c35.3 0 64-28.7 64-64V96c0-35.3-28.7-64-64-64H64zm160 96a128 128 0 1 1 0 256 128 128 0 1 1 0-256zm0 288a160 160 0 1 0 0-320 160 160 0 1 0 0 320zm0-136a24 24 0 1 0 0-48 24 24 0 1 0 0 48z"/>
</svg>

The removeComments plugin is supposedly enabled by default, but the comments remain after processing. This is what I want to remove:

<!--! Font Awesome Free 6.4.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license Copyright 2023 Fonticons, Inc.-->

To Reproduce
Steps to reproduce the behavior:

  1. Create svgo.config.js as outlined:
// svgo.config.js
module.exports = {
    multipass: true,
    js2svg: {
      indent: 2, 
      pretty: true,
    },
    plugins: [  
        { name: 'removeComments' }
    ]
  };
  1. Run svgo --config=svgo.config.js "FontAwesome Free 6 Light Album Circle Plus.svg"
  2. Open the resulting SVG and notice that the comment is still present.

Expected behavior
All comments to be nuked from the SVG file.

Desktop (please complete the following information):

  • SVGO Version 3.0.2
  • NodeJs Version v20.8.0
  • OS: Windows 10 Pro x64 Version 22H2 (Build 19045.3448)

Additional context
Not sure what I'm doing wrong. All I want to do is strip comments.

@futuremotiondev
Copy link
Author

Update:

It works if I open removeComments.js in Roaming\nvm\v20.8.0\node_modules\svgo\plugins and then comment out the conditional: if (node.value.charAt(0) !== '!')

I think the problem is, since the comment starts with an !, removeComments doesn't eliminate it by default. But this is really hackish. I don't want to edit plugin definitions in fear of breaking things.

Is there a way to do what I need without editing the removeComments plugin definition file?

@SethFalco
Copy link
Member

SethFalco commented Oct 21, 2023

You aren't doing anything wrong with SVGO.

By default, the plugin preserves legal comments, which are prefixed with !, as this is often used for important information like copyright, licensing, or attribution.

I can add a parameter to removeComments to preserve comments that match certain patterns. Then you can override it to an empty array or false to remove comments indiscriminately.

In this scenario, the comment is for copyright/licensing and allows you to meet the attribution requirement set out by the Font Awesome and Creative Commons licenses. By removing this you may be breaching the license terms, so unless you're paying for Font Awesome Pro, where attribution is not required, I'd discourage you from removing that comment.

If you'd like an immediate solution, you can add a custom plugin for now:

module.exports = {
  plugins: [
    {
      name: "customRemoveComments",
      fn: () => {
        return {
          comment: {
            enter: (node, parentNode) => {
              parentNode.children = parentNode.children.filter((child) => child !== node);
            },
          },
        };
      }
    }
  ],
};

In v3.0.3 this will be possible without a custom plugin. The following will be possible to remove all comments indiscriminately:

module.exports = {
  plugins: [
    {
      name: 'preset-default',
      params: {
        overrides: {
          removeComments: {
            preservePatterns: false,
          },
        },
      },
    },
  ],
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants