-
Notifications
You must be signed in to change notification settings - Fork 254
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
Add option to customize symbol id based on file name #37
Comments
Hi @troywarr, thanks a lot for your feedback and for supporting svg-sprite by adopting it — I very much appreciate that! :) Regarding your question: There is already a config option that lets you customize the IDs in whatever way you want, although this might not be obvious at first. Please have a look at the function(name) {
return path.basename(name.split(path.sep).join(this.separator), '.svg');
} Replace it with a callback that has the same signature (i.e. accepting the "local path" as it's sole argument and returning the ID). It might seem a little overdone to use a callback for this (in comparison to function(name) {
return 'icon-' + path.basename(name.split(path.sep).join(this.separator), '.svg');
} Please let me know if this works for you. For simpler cases, I might consider adding support for passing in template strings as well. Stay tuned ... Cheers, |
As of release ac5f923, svg-sprite now supports template strings for the Cheers, |
This is excellent - thanks so much, Joschi! Both approaches (function and template string) work great. I stuck with the latter for simplicity's sake. Thanks for adding that ability! Sorry I missed this feature in the docs - I read through them but must not have grokked that section. I think I somehow had the impression that all of the config options pertinent to my use case were nested within the Anyhow, thanks again! |
I have a similar problem, but i want to edit the id based in file path. Example: The shape.generator.id option used as a function only pass name as argument. Would be good pass the path too? Or is there another solution? Thank you |
I just switched to gulp-svg-sprite from gulp-svg-sprites; it was pretty seamless for my use case, but one useful feature of the latter that doesn't appear to be supported in the former is config-based control over the value of the
id
attributes of the rendered<symbol>
s.gulp-svg-sprites actually doesn't appear to directly support this either, but there I was able to set the
selector
config option to something likeicon-%f
, and that would also affect the generated<symbol>
'sid
(not just the class names of the<svg>
elements used in the HTML to include the sprite output), giving me output like:when sourcing a directory of SVG files like:
This is handy because it keeps the file names concise and DRY (i.e., I don't need to name them
icons/icon-my-logo.svg
), while also "namespacing" the generatedid
attributes so that I don't have to worry about a naming collision unless I give another element on the page anid
starting withicon-
as well (which is unlikely anyway, and easier to remember not to do).Would it be possible to add such a config option here? Or is there already a way to do this of which I'm unaware?
Offhand, I'd think maybe a good way to do it would be to provide an
id
configuration option with a similar string format:where
%f
is replaced by theid
that's generated by the.svg
source file name.Hope that makes sense - please let me know if not.
Incidentally, great work here!
The text was updated successfully, but these errors were encountered: