Skip to content

Instantly share code, notes, and snippets.

@wernerkrauss
Forked from JacobDB/inline-svg-function.scss
Last active January 17, 2018 17:52
Show Gist options
  • Save wernerkrauss/00ef3c00ba7c51e76ed9237b411d7304 to your computer and use it in GitHub Desktop.
Save wernerkrauss/00ef3c00ba7c51e76ed9237b411d7304 to your computer and use it in GitHub Desktop.
Inline SVG function [SASS]
// Replace letters
@function str-replace($string, $search, $replace: '') {
$index: str-index($string, $search);
@if $index {
@return str-slice($string, 1, $index - 1) + $replace + str-replace(str-slice($string, $index + str-length($search)), $search, $replace);
}
@return $string;
}
// Encode symbols
@function url-encode($string) {
$map: (
"%": "%25",
"<": "%3C",
">": "%3E",
" ": "%20",
"!": "%21",
"*": "%2A",
"'": "%27",
'"': "%22",
"(": "%28",
")": "%29",
";": "%3B",
":": "%3A",
"@": "%40",
"&": "%26",
"=": "%3D",
"+": "%2B",
"$": "%24",
",": "%2C",
"/": "%2F",
"?": "%3F",
"#": "%23",
"[": "%5B",
"]": "%5D",
'\A': ""
);
$new: $string;
@each $search, $replace in $map {
$new: str-replace($new, $search, $replace);
}
@return $new;
}
// Format the SVG as a URL
@function inline-svg($string) {
@return url('data:image/svg+xml,#{url-encode($string)}');
}
@wernerkrauss
Copy link
Author

Added replacment for newlines to get it working with webpack / laravel-mix, as it always wraps the encoded SVG in double qoutes.

'\A': ""

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment