Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Toolchain
inline-svg
Commits
be28cb6b
Commit
be28cb6b
authored
Aug 22, 2019
by
Stefan Galinski
🎮
Browse files
[FEATURE] Allow to work with recent fontawesome version that has subdirectories
parent
1411e9b3
Changes
1
Show whitespace changes
Inline
Side-by-side
index.js
View file @
be28cb6b
...
...
@@ -4,7 +4,6 @@ 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
'
);
...
...
@@ -37,13 +36,14 @@ module.exports = class InlineSvg {
*/
async
_process
(
_svgFilePath
)
{
return
new
Promise
(
async
(
_resolve
,
_reject
)
=>
{
try
{
let
svgs
=
await
this
.
_readFiles
(
_svgFilePath
);
let
templateContent
=
await
imagemin
.
buffer
(
this
.
_getTemplateContent
(),
{
plugins
:
[
imageminSvgo
()
]
});
svgs
.
forEach
(
svg
=>
this
.
_processSvg
(
svg
))
svgs
.
forEach
(
svg
=>
this
.
_processSvg
(
svg
,
_svgFilePath
))
;
let
template
=
mustache
.
render
(
templateContent
.
toString
(),
_
.
extend
(
...
...
@@ -55,6 +55,9 @@ module.exports = class InlineSvg {
)
);
_resolve
(
template
);
}
catch
(
e
)
{
console
.
log
(
e
);
}
});
}
...
...
@@ -66,15 +69,18 @@ module.exports = class InlineSvg {
* 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
...
...
@@ -140,5 +146,4 @@ module.exports = class InlineSvg {
_getTemplateContent
()
{
return
fs
.
readFileSync
(
this
.
_options
.
template
);
}
};
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment