-
-
Notifications
You must be signed in to change notification settings - Fork 159
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 macro to inline SVG files #1761
Conversation
Forgot to mention I'll add the necessary documentation on the website as well when this is approved. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I love this idea. I use a lot of SVGs and have lamented that I can't easily inline them as part of compilation.
inlined_svg.should contain %(fill="none" stroke="#2a2a2a" class="logo") | ||
inlined_svg.should_not contain %(<?xml version="1.0" encoding="UTF-8"?>) | ||
inlined_svg.should_not contain %(<!-- lucky logo -->) | ||
inlined_svg.should_not contain "\n" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like that this strips out un-needed stuff from the XML. Should these should_not
cases be broken out into an it "strips out comments"
and it "strips out the xml header"
set of tests? As it is, if something comes up and someone needs to modify the filter, they don't have a test which directly correlates to that feature.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. I'll add separate specs for those cases separately.
end | ||
|
||
@[Lucky::SvgInliner::Path("src/svgs")] | ||
@[Lucky::SvgInliner::StripRegex(/(class|fill|stroke|stroke-width|style)="[^"]+" ?/)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you help me understand what's going on with these annotations? It seems like a roundabout way to accomplish arguments to the macro.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. I think having those parameters passed as arguments would be tedious. Let's say the user wants to use a different directory for the SVG files, then it suffices to override the annotation in an initializer:
# config/svg_inliner.cr
@[Lucky::SvgInliner::Path("src/icons")]
module Lucky::SvgInliner
end
As opposed to (for every single SVG):
inline_svg("menu-social/twitter.svg", false, "src/icons")
inline_svg("menu-social/discord.svg", false, "src/icons")
Does that answer your question? Or am I missing the point?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh interesting, I hadn't considered that. I'm not opposed. Another possibility for this would be to have a Habitat config parameter. @jwoertink @stephendolan thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tried that first, but settings from Habitat are not available at compile-time. As far as I know, the only way to allow compile-time configuration is by using annotations or constants. And using constants is not favourable because it's impossible to override them.
I think these are sensible defaults, so in 99% of the cases, it wouldn't be necessary to change them. The annotations are just an escape hatch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's different for sure, but I'm willing to give it a shot. I don't have a strong opinion on it vs just using Habitat.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@wout yeah, you present a use case I can't argue with. Thank you for walking me through it. Let's go!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it looks good 👍
Purpose
Adds a macro to inline SVG files at compile time.
Description
SVG logos and icons can be inlined with the
inline_svg
macro in pages and components:This will:
src/svgs
at compile-time (path configurable with an annotation)data-inline-svg
attribute with the path of the icon (e.g.<svg data-inline-svg="buttons/round.svg">
)Icons can then be styled with:
Or:
I realise Lucky does not use annotations a lot, but I think they are helpful in this case. The settings defined by the annotations are reasonable defaults, I think, but if necessary, users can jump in and override the defaults.
This PR implements #1760.
Checklist
crystal tool format spec src
./script/setup
./script/test