Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
I
inline-svg
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Toolchain
inline-svg
Commits
be28cb6b
Commit
be28cb6b
authored
5 years ago
by
Stefan Galinski
Browse files
Options
Downloads
Patches
Plain Diff
[FEATURE] Allow to work with recent fontawesome version that has subdirectories
parent
1411e9b3
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
index.js
+39
-34
39 additions, 34 deletions
index.js
with
39 additions
and
34 deletions
index.js
+
39
−
34
View file @
be28cb6b
...
...
@@ -4,16 +4,15 @@ const fs = require('fs');
const
parseXmlStringSync
=
require
(
'
xml2js-parser
'
).
parseStringSync
;
const
globby
=
require
(
'
globby
'
);
const
_
=
require
(
'
underscore
'
);
const
util
=
require
(
'
util
'
);
const
svgToMiniDataURI
=
require
(
'
mini-svg-data-uri
'
);
const
imagemin
=
require
(
'
imagemin
'
);
const
imageminSvgo
=
require
(
'
imagemin-svgo
'
);
module
.
exports
=
class
InlineSvg
{
/**
* Kick things off
*
*
* @param {String} _svgFilePath The path to the folder containing the SVGs
* @param {Object} _options Configuration object
* @returns {Promise}
...
...
@@ -32,49 +31,56 @@ module.exports = class InlineSvg {
/**
* Process all SVG files
*
*
* @param {String} _svgFilePath The path to the folder containing the SVGs
*/
async
_process
(
_svgFilePath
)
{
return
new
Promise
(
async
(
_resolve
,
_reject
)
=>
{
let
svgs
=
await
this
.
_readFiles
(
_svgFilePath
);
let
templateContent
=
await
imagemin
.
buffer
(
this
.
_getTemplateContent
(),
{
plugins
:
[
imageminSvgo
()
]
});
svgs
.
forEach
(
svg
=>
this
.
_processSvg
(
svg
))
let
template
=
mustache
.
render
(
templateContent
.
toString
(),
_
.
extend
(
{},
this
.
_options
.
context
,
{
svgs
:
this
.
_svgs
}
)
);
_resolve
(
template
);
try
{
let
svgs
=
await
this
.
_readFiles
(
_svgFilePath
);
let
templateContent
=
await
imagemin
.
buffer
(
this
.
_getTemplateContent
(),
{
plugins
:
[
imageminSvgo
()
]
});
svgs
.
forEach
(
svg
=>
this
.
_processSvg
(
svg
,
_svgFilePath
));
let
template
=
mustache
.
render
(
templateContent
.
toString
(),
_
.
extend
(
{},
this
.
_options
.
context
,
{
svgs
:
this
.
_svgs
}
)
);
_resolve
(
template
);
}
catch
(
e
)
{
console
.
log
(
e
);
}
});
}
/**
* Processes a single SVG icon
*
*
* _data is expected to carry two properties:
*
data.content: A string containing the SVG source
*
data.fileName: A string containing the file name
*
*
data.content: A string containing the SVG source
*
data.fileName: A string containing the file name
*
* @param {Object} _data Object containing info about the SVG
* @param {String} _svgFilePath Original File Path from the fetched SVG
*/
_processSvg
(
_data
)
{
_processSvg
(
_data
,
_svgFilePath
)
{
const
xmlString
=
parseXmlStringSync
(
_data
.
content
.
toString
(),
{
attrNameProcessors
:
[
name
=>
name
.
toLowerCase
()]
});
const
svgDimensions
=
this
.
_getSvgDimensions
(
xmlString
);
const
_svgFilePathAdjusted
=
_svgFilePath
.
replace
(
'
**
'
,
''
);
const
svgData
=
{
name
:
path
.
basename
(
_data
.
fileName
,
'
.svg
'
),
inline
:
svgToMiniDataURI
(
_data
.
content
),
name
:
_data
.
fileName
.
replace
(
_svgFilePathAdjusted
,
''
).
replace
(
/
\/
/g
,
'
-
'
).
replace
(
'
.svg
'
,
''
).
replace
(
'
fontawesome-
'
,
''
),
// add fill="white" to allow later color changes based on this value
inline
:
svgToMiniDataURI
(
_data
.
content
.
indexOf
(
'
fill=
'
)
===
-
1
?
_data
.
content
.
replace
(
'
<svg
'
,
'
<svg fill="white"
'
)
:
_data
.
content
),
width
:
parseInt
(
svgDimensions
.
width
)
+
'
px
'
,
height
:
parseInt
(
svgDimensions
.
height
)
+
'
px
'
,
dimensions
:
svgDimensions
...
...
@@ -90,9 +96,9 @@ module.exports = class InlineSvg {
/**
* Extracts the dimensions from the SVG source
*
*
* @param {String} xmlString The SVG source
* @returns {Object}
* @returns {Object}
*/
_getSvgDimensions
(
xmlString
)
{
const
hasWidthHeightAttr
=
xmlString
.
svg
.
$
[
'
width
'
]
&&
xmlString
.
svg
.
$
[
'
width
'
];
...
...
@@ -113,7 +119,7 @@ module.exports = class InlineSvg {
/**
* Reads all the SVGs that are located in _path
*
*
* @param {String} _path The location to read from
* @returns {Promise}
*/
...
...
@@ -134,11 +140,10 @@ module.exports = class InlineSvg {
/**
* Returns the mustache template as a string
*
*
* @returns {String}
*/
_getTemplateContent
()
{
return
fs
.
readFileSync
(
this
.
_options
.
template
);
}
};
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment