Skip to content

Instantly share code, notes, and snippets.

@exdeniz
Last active August 29, 2015 14:17
Show Gist options
  • Save exdeniz/9af12ac815df02a63c16 to your computer and use it in GitHub Desktop.
Save exdeniz/9af12ac815df02a63c16 to your computer and use it in GitHub Desktop.
;( function( window, document )
{
'use strict';
var file = './img/svghtml.html',
revision = 1,
debug = true;
if( !document.createElementNS || !document.createElementNS( 'http://www.w3.org/2000/svg', 'svg' ).createSVGRect )
return true;
var isLocalStorage = 'localStorage' in window && window[ 'localStorage' ] !== null,
request,
data,
insertIT = function()
{
document.body.insertAdjacentHTML( 'afterbegin', data );
},
insert = function()
{
if( document.body ) insertIT();
else document.addEventListener( 'DOMContentLoaded', insertIT );
};
if( isLocalStorage && localStorage.getItem( 'inlineSVGrev' ) == revision & debug == false )
{
data = localStorage.getItem( 'inlineSVGdata' );
if( data )
{
insert();
return true;
}
}
try
{
request = new XMLHttpRequest();
request.open( 'GET', file, true );
request.onload = function()
{
if( request.status >= 200 && request.status < 400 )
{
data = request.responseText;
insert();
if( isLocalStorage )
{
localStorage.setItem( 'inlineSVGdata', data );
localStorage.setItem( 'inlineSVGrev', revision );
}
}
}
request.send();
}
catch( e ){}
}( window, document ) );
<body>
<svg xmlns="http://www.w3.org/2000/svg" style="display: none;"><% _.forEach( icons, function( icon ){ %>
<symbol id="<%= icon.id %>" viewBox="<%= icon.svg.viewBox %>"><% if (icon.title) {%>
<title><%= icon.title %></title><% }%>
<%= icon.svg.content %>
</symbol><%
}); %></svg>
</body>
mixin svgIcon(name)
- idname = "#" + name
svg
use(xlink:href=idname)
// Use +svgIcon('icon-jade')
var gulp = require('gulp'),
svgSymbols = require('gulp-svg-symbols'),
browserSync = require('browser-sync'),
cheerio = require('gulp-cheerio'),
path = require('path'),
reload = browserSync.reload,
customHTMLTemplate = path.join(__dirname, './config/template/svghtml.html');
gulp.task('svgicon', function() {
gulp.src('./assets/svg/**/*.svg')
.pipe(cheerio({
run: function($) {
$('[fill]').removeAttr('fill');
},
parserOptions: {
xmlMode: true
}
}))
.pipe(svgSymbols({
templates: ['./config/template/svghtml.html']
}))
.pipe(gulp.dest('./public/img/'))
.pipe(reload({
stream: true
}));
});
@exdeniz
Copy link
Author

exdeniz commented Mar 13, 2015

Add debug = true for develop, change to false fro production.

Remeber script request file via XMLHttpRequest() and it not work for file://.
Use direct include svghtml.html

other script
gulp task and template for gulp-svg-symbol
mixin for jade

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