
Product
Redesigned Repositories Page: A Faster Way to Prioritize Security Risk
Our redesigned Repositories page adds alert severity, filtering, and tabs for faster triage and clearer insights across all your projects.
grunt-svg2string
Advanced tools
Transforms a SVG file into a JavaScript string
This plugin requires Grunt >=0.4.0
If you haven’t used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:
npm install grunt-svg2string --save-dev
Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:
grunt.loadNpmTasks('grunt-svg2string');
In your project’s Gruntfile, add a section named svg2string
to the data object passed into grunt.initConfig()
.
grunt.initConfig({
svg2string: {
options: {
// Task-specific options go here.
},
your_target: {
// Target-specific file lists and/or options go here.
}
}
});
Type: String
Default value: 'var SVG_[%= capitalized %] =\n[%= content %];'
A string value that is used as a template for JavaScript file. You can use following placeholders inside the template:
[%= content %]
— processed content;[%= filepath %]
— path and file name of the current file (eg. test/fixtures/elements.svg
);[%= filename %]
— name of the current file (eg. elements
);[%= sanitized %]
— sanitized name (eg. icon_set
);[%= capitalized %]
— sanitized and capitalized name (eg. ELEMENTS
);[%= ext %]
— extension of the current file (eg. svg
).See examples of the templates below.
(App.SVG = App.SVG || {})["[%= sanitized %]"] = [%= content %];
App.defaults("App.SVG", {"[%= sanitized %]": [%= content %]});
Type: Boolean
Default value: true
A boolean value that instructs to wrap long lines or not.
Type: Number
Default value: 120
A numerical value that is used as maximum length of the line of the processed content including quotes and concatenation sign.
Please, bear in mind that this option is not applicable if wrapLines
is false
.
Type: String
Default value: none
A string value that is used as name for group of icons represented as symbols.
All matched SVG files converted to <symbol>
and joined together into one SVG file. Next, this file processed as usual.
Source files:
icon-set.svg
<svg xmlns="http://www.w3.org/2000/svg" width="0" height="0">
<defs>
<polygon id="icon-arrow-right" points="15,7 11,11 20,20 11,29 15,33 28,20"></polygon>
<polygon id="icon-arrow-left" points="25,33 29,29 20,20 29,11 25,7 12,20"></polygon>
<path id="icon-circle-arrow-right" d="M20 10c5.5 0 10 4.5 10 10s-4.5 10-10 10-10-4.5-10-10 4.5-10 10-10zm-2.8 13.5c-.3.3-.3.8 0 1l1 1c.3.3.7.3 1 0l4.8-5c.3-.3.3-.8 0-1l-4.9-5.1c-.3-.3-.7-.3-1 0l-1 1c-.3.3-.3.8 0 1l3.4 3.5-3.3 3.6z"></path>
<path id="icon-circle-arrow-left" d="M20 10c-5.5 0-10 4.5-10 10s4.5 10 10 10 10-4.5 10-10-4.5-10-10-10zm2.8 13.5c.3.3.3.8 0 1l-1 1c-.3.3-.7.3-1 0l-4.8-5c-.3-.3-.3-.8 0-1l4.9-5.1c.3-.3.7-.3 1 0l1 1c.3.3.3.8 0 1l-3.4 3.6 3.3 3.5z"></path>
<path id="icon-play" d="M20 5c-8.3 0-15 6.7-15 15s6.7 15 15 15 15-6.7 15-15-6.7-15-15-15zm0 27c-6.6 0-12-5.4-12-12s5.4-12 12-12 12 5.4 12 12-5.4 12-12 12zm-3-6l9-6-9-6v12z"></path>
<filter id="filter-shadow">
<feOffset dx="0" dy="1"></feOffset>
<feGaussianBlur stdDeviation="1"></feGaussianBlur>
<feColorMatrix type="matrix" values="0 0 0 .75 0, 0 0 0 .75 0, 0 0 0 .75 0, 0 0 0 .75 0" result="shadow"></feColorMatrix>
<feMerge>
<feMergeNode in="shadow"></feMergeNode>
<feMergeNode in="SourceGraphic"></feMergeNode>
</feMerge>
</filter>
</defs>
</svg>
navigation.svg
<svg xmlns="http://www.w3.org/2000/svg">
<defs>
<path id="nav-down" d="M0 1l5 5 5-5-1-1-4 4-4-4-1 1z"></path>
<path id="nav-up" d="M10 5l-5-5-5 5 1 1 4-4 4 4 1-1z"></path>
</defs>
</svg>
In this example, the default options are used transform two SVG files into the JavaScript file.
grunt.initConfig({
svg2string: {
elements: {
files: {
'assets/_/js/elements.js': [
'assets/images/icon-set.svg',
'assets/images/navigation.svg'
]
}
}
}
});
Output file elements.js
var SVG_ICON_SET =
'<svg xmlns="http://www.w3.org/2000/svg" width="0" height="0"><defs><polygon id="icon-arrow-right" points="15,7 11,11 '+
'20,20 11,29 15,33 28,20"></polygon><polygon id="icon-arrow-left" points="25,33 29,29 20,20 29,11 25,7 12,20"></polygo'+
'n><path id="icon-circle-arrow-right" d="M20 10c5.5 0 10 4.5 10 10s-4.5 10-10 10-10-4.5-10-10 4.5-10 10-10zm-2.8 13.5c'+
'-.3.3-.3.8 0 1l1 1c.3.3.7.3 1 0l4.8-5c.3-.3.3-.8 0-1l-4.9-5.1c-.3-.3-.7-.3-1 0l-1 1c-.3.3-.3.8 0 1l3.4 3.5-3.3 3.6z">'+
'</path><path id="icon-circle-arrow-left" d="M20 10c-5.5 0-10 4.5-10 10s4.5 10 10 10 10-4.5 10-10-4.5-10-10-10zm2.8 13'+
'.5c.3.3.3.8 0 1l-1 1c-.3.3-.7.3-1 0l-4.8-5c-.3-.3-.3-.8 0-1l4.9-5.1c.3-.3.7-.3 1 0l1 1c.3.3.3.8 0 1l-3.4 3.6 3.3 3.5z'+
'"></path><path id="icon-play" d="M20 5c-8.3 0-15 6.7-15 15s6.7 15 15 15 15-6.7 15-15-6.7-15-15-15zm0 27c-6.6 0-12-5.4'+
'-12-12s5.4-12 12-12 12 5.4 12 12-5.4 12-12 12zm-3-6l9-6-9-6v12z"></path><filter id="filter-shadow"><feOffset dx="0" d'+
'y="1"></feOffset><feGaussianBlur stdDeviation="1"></feGaussianBlur><feColorMatrix type="matrix" values="0 0 0 .75 0, '+
'0 0 0 .75 0, 0 0 0 .75 0, 0 0 0 .75 0" result="shadow"></feColorMatrix><feMerge><feMergeNode in="shadow"></feMergeNod'+
'e><feMergeNode in="SourceGraphic"></feMergeNode></feMerge></filter></defs></svg>';
var SVG_NAVIGATION =
'<svg xmlns="http://www.w3.org/2000/svg"><defs><path id="nav-down" d="M0 1l5 5 5-5-1-1-4 4-4-4-1 1z"></path><path id="'+
'nav-up" d="M10 5l-5-5-5 5 1 1 4-4 4 4 1-1z"></path></defs></svg>';
In this example, we create custom JavaScript file out of two SVG files using template and no wrap option.
grunt.initConfig({
svg2string: {
elements: {
options: {
template: '(App.SVG = App.SVG || {})["[%= filename %]"] = [%= content %];',
wrapLines: false
},
files: {
'assets/_/js/elements.js': [
'assets/images/icon-set.svg',
'assets/images/navigation.svg'
]
}
}
}
});
Output file elements.js
(App.SVG = App.SVG || {})["icon-set"] = '<svg xmlns="http://www.w3.org/2000/svg" width="0" height="0"><defs><polygon id="icon-arrow-right" points="15,7 11,11 20,20 11,29 15,33 28,20"></polygon><polygon id="icon-arrow-left" points="25,33 29,29 20,20 29,11 25,7 12,20"></polygon><path id="icon-circle-arrow-right" d="M20 10c5.5 0 10 4.5 10 10s-4.5 10-10 10-10-4.5-10-10 4.5-10 10-10zm-2.8 13.5c-.3.3-.3.8 0 1l1 1c.3.3.7.3 1 0l4.8-5c.3-.3.3-.8 0-1l-4.9-5.1c-.3-.3-.7-.3-1 0l-1 1c-.3.3-.3.8 0 1l3.4 3.5-3.3 3.6z"></path><path id="icon-circle-arrow-left" d="M20 10c-5.5 0-10 4.5-10 10s4.5 10 10 10 10-4.5 10-10-4.5-10-10-10zm2.8 13.5c.3.3.3.8 0 1l-1 1c-.3.3-.7.3-1 0l-4.8-5c-.3-.3-.3-.8 0-1l4.9-5.1c.3-.3.7-.3 1 0l1 1c.3.3.3.8 0 1l-3.4 3.6 3.3 3.5z"></path><path id="icon-play" d="M20 5c-8.3 0-15 6.7-15 15s6.7 15 15 15 15-6.7 15-15-6.7-15-15-15zm0 27c-6.6 0-12-5.4-12-12s5.4-12 12-12 12 5.4 12 12-5.4 12-12 12zm-3-6l9-6-9-6v12z"></path><filter id="filter-shadow"><feOffset dx="0" dy="1"></feOffset><feGaussianBlur stdDeviation="1"></feGaussianBlur><feColorMatrix type="matrix" values="0 0 0 .75 0, 0 0 0 .75 0, 0 0 0 .75 0, 0 0 0 .75 0" result="shadow"></feColorMatrix><feMerge><feMergeNode in="shadow"></feMergeNode><feMergeNode in="SourceGraphic"></feMergeNode></feMerge></filter></defs></svg>';
(App.SVG = App.SVG || {})["navigation"] = '<svg xmlns="http://www.w3.org/2000/svg"><defs><path id="nav-down" d="M0 1l5 5 5-5-1-1-4 4-4-4-1 1z"></path><path id="nav-up" d="M10 5l-5-5-5 5 1 1 4-4 4 4 1-1z"></path></defs></svg>';
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.
FAQs
Transforms a SVG file into a JavaScript string
We found that grunt-svg2string demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Our redesigned Repositories page adds alert severity, filtering, and tabs for faster triage and clearer insights across all your projects.
Security News
Slopsquatting is a new supply chain threat where AI-assisted code generators recommend hallucinated packages that attackers register and weaponize.
Security News
Multiple deserialization flaws in PyTorch Lightning could allow remote code execution when loading untrusted model files, affecting versions up to 2.4.0.