Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
sgc
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
sgc
Merge requests
!14
Task update2024
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Task update2024
task_update2024
into
master
Overview
0
Commits
29
Changes
32
Merged
Johannes Kreiner
requested to merge
task_update2024
into
master
3 months ago
Overview
0
Commits
29
Changes
18
Expand
0
0
Merge request reports
Compare
version 9
version 26
3ee75916
3 months ago
version 25
ab491710
3 months ago
version 24
2ec149cd
3 months ago
version 23
c581832e
3 months ago
version 22
3d5660b1
3 months ago
version 21
7121b885
3 months ago
version 20
06e44ae7
3 months ago
version 19
55f0bc83
3 months ago
version 18
dd2b14e2
3 months ago
version 17
41eb22c0
3 months ago
version 16
4d206fbe
3 months ago
version 15
4584d93b
3 months ago
version 14
7253856a
3 months ago
version 13
74691778
3 months ago
version 12
a9589473
3 months ago
version 11
162d2a9a
3 months ago
version 10
1e6dcef3
3 months ago
version 9
0bdbf01b
3 months ago
version 8
2114ea2b
3 months ago
version 7
f2862719
3 months ago
version 6
5984b56a
3 months ago
version 5
e4b97dbf
3 months ago
version 4
035d04b8
3 months ago
version 3
f2c1f04c
3 months ago
version 2
144e89a5
3 months ago
version 1
b63d41a1
3 months ago
master (base)
and
version 12
latest version
cffcf282
29 commits,
3 months ago
version 26
3ee75916
28 commits,
3 months ago
version 25
ab491710
27 commits,
3 months ago
version 24
2ec149cd
26 commits,
3 months ago
version 23
c581832e
25 commits,
3 months ago
version 22
3d5660b1
24 commits,
3 months ago
version 21
7121b885
23 commits,
3 months ago
version 20
06e44ae7
22 commits,
3 months ago
version 19
55f0bc83
21 commits,
3 months ago
version 18
dd2b14e2
20 commits,
3 months ago
version 17
41eb22c0
19 commits,
3 months ago
version 16
4d206fbe
18 commits,
3 months ago
version 15
4584d93b
17 commits,
3 months ago
version 14
7253856a
16 commits,
3 months ago
version 13
74691778
15 commits,
3 months ago
version 12
a9589473
14 commits,
3 months ago
version 11
162d2a9a
13 commits,
3 months ago
version 10
1e6dcef3
12 commits,
3 months ago
version 9
0bdbf01b
11 commits,
3 months ago
version 8
2114ea2b
10 commits,
3 months ago
version 7
f2862719
9 commits,
3 months ago
version 6
5984b56a
8 commits,
3 months ago
version 5
e4b97dbf
7 commits,
3 months ago
version 4
035d04b8
6 commits,
3 months ago
version 3
f2c1f04c
5 commits,
3 months ago
version 2
144e89a5
4 commits,
3 months ago
version 1
b63d41a1
3 commits,
3 months ago
Show latest version
18 files
+
130
−
143
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
18
Search (e.g. *.vue) (Ctrl+P)
core/modules/inline-svg/index.js
+
85
−
70
Options
cons
t
path
=
require
(
'
path
'
)
;
cons
t
mustache
=
require
(
'
mustache
'
)
;
const
fs
=
require
(
'
fs
'
)
;
const
parseXmlStringSync
=
require
(
'
xml2js-parser
'
).
parseStringSync
;
const
globby
=
require
(
'
globby
'
)
;
const
_
=
require
(
'
underscore
'
)
;
const
svgToMiniDataURI
=
require
(
'
mini-svg-data-uri
'
)
;
const
imagemin
=
require
(
'
imagemin
'
)
;
cons
t
imagemin
Svgo
=
require
(
'
imagemin
-svgo
'
)
;
module
.
exports
=
class
InlineSvg
{
impor
t
path
from
'
node:
path
'
;
impor
t
mustache
from
'
mustache
'
;
import
fs
from
'
node:
fs
'
;
import
pkg
from
'
xml2js-parser
'
;
const
{
parseStringSync
}
=
pkg
;
import
*
as
globby
from
'
globby
'
;
import
_
from
'
underscore
'
;
import
svgToMiniDataURI
from
'
mini-svg-data-uri
'
;
impor
t
imagemin
from
'
imagemin
'
;
import
imageminSvgo
from
'
imagemin-svgo
'
;
import
{
Buffer
}
from
'
node:buffer
'
;
export
class
InlineSvg
{
/**
* Kick things off
*
* @param {String} _svgFilePath The path to the folder containing the SVGs
* @param {Object} _options Configuration object
* @returns {Promise}
*/
constructor
(
_svgFilePath
,
_options
)
{
let
defaultOptions
=
{
template
:
__dirname
+
'
/template.mustache
'
,
template
:
'
.
/template.mustache
'
,
context
:
{},
interceptor
:
null
interceptor
:
null
,
};
this
.
_files
=
{};
this
.
_svgs
=
[];
this
.
_options
=
_
.
extend
(
defaultOptions
,
_options
);
return
this
.
_process
(
_svgFilePath
);
this
.
_svgFilePath
=
_svgFilePath
;
}
static
async
create
(
_svgFilePath
,
_options
)
{
const
instance
=
new
InlineSvg
(
_svgFilePath
,
_options
);
return
instance
.
_process
(
_svgFilePath
);
}
/**
* Process all SVG files
*
* @param {
S
tring} _svgFilePath The path to the folder containing the SVGs
* @param {
s
tring} _svgFilePath The path to the folder containing the SVGs
*/
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
,
_svgFilePath
));
let
template
=
mustache
.
render
(
templateContent
.
toString
(),
_
.
extend
(
{},
this
.
_options
.
context
,
{
svgs
:
this
.
_svgs
}
)
);
_resolve
(
template
);
}
catch
(
e
)
{
console
.
log
(
e
);
}
});
try
{
let
svgs
=
await
this
.
_readFiles
(
_svgFilePath
);
let
templateContent
=
this
.
_getTemplateContent
();
// Convert string to Buffer before passing to imagemin
let
optimizedTemplateBuffer
=
await
imagemin
.
buffer
(
Buffer
.
from
(
templateContent
),
{
plugins
:
[
imageminSvgo
()],
});
svgs
.
forEach
((
svg
)
=>
this
.
_processSvg
(
svg
,
_svgFilePath
));
let
template
=
mustache
.
render
(
Buffer
.
from
(
optimizedTemplateBuffer
).
toString
(
'
utf8
'
),
_
.
extend
({},
this
.
_options
.
context
,
{
svgs
:
this
.
_svgs
,
}),
);
return
template
;
}
catch
(
error
)
{
console
.
error
(
'
Template processing error:
'
,
error
);
throw
error
;
}
}
/**
@@ -72,48 +74,63 @@ module.exports = class InlineSvg {
* @param {String} _svgFilePath Original File Path from the fetched SVG
*/
_processSvg
(
_data
,
_svgFilePath
)
{
const
xmlString
=
parse
Xml
StringSync
(
_data
.
content
.
toString
(),
{
attrNameProcessors
:
[
name
=>
name
.
toLowerCase
()]
const
xmlString
=
parseStringSync
(
_data
.
content
.
toString
(),
{
attrNameProcessors
:
[
(
name
)
=>
name
.
toLowerCase
()]
,
});
const
svgDimensions
=
this
.
_getSvgDimensions
(
xmlString
);
const
_svgFilePathAdjusted
=
_svgFilePath
.
replace
(
'
**
'
,
''
);
const
svgData
=
{
name
:
_data
.
fileName
.
replace
(
_svgFilePathAdjusted
,
''
).
replace
(
/
\/
/g
,
'
-
'
).
replace
(
'
.svg
'
,
''
).
replace
(
'
fontawesome-
'
,
''
),
name
:
_data
.
fileName
.
replace
(
_svgFilePathAdjusted
,
''
)
.
replaceAll
(
'
/
'
,
'
-
'
)
.
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
inline
:
svgToMiniDataURI
(
_data
.
content
.
includes
(
'
fill=
'
)
?
_data
.
content
:
_data
.
content
.
replace
(
'
<svg
'
,
'
<svg fill="white"
'
),
),
width
:
Number
.
parseInt
(
svgDimensions
.
width
)
+
'
px
'
,
height
:
Number
.
parseInt
(
svgDimensions
.
height
)
+
'
px
'
,
dimensions
:
svgDimensions
,
};
// store this svg data
this
.
_svgs
.
push
(
_
.
isFunction
(
this
.
_options
.
interceptor
)
?
_
.
extend
({},
svgData
,
this
.
_options
.
interceptor
(
svgData
)
||
svgData
)
:
svgData
:
svgData
,
);
}
/**
* Extracts the dimensions from the SVG source
*
* @param {
String
} xmlString The
SVG source
* @param {
Object
} xmlString The
parsed SVG XML object
* @returns {Object}
*/
_getSvgDimensions
(
xmlString
)
{
const
hasWidthHeightAttr
=
xmlString
.
svg
.
$
[
'
width
'
]
&&
xmlString
.
svg
.
$
[
'
width
'
];
const
hasWidthHeightAttr
ibute
=
xmlString
.
svg
.
$
[
'
width
'
]
&&
xmlString
.
svg
.
$
[
'
height
'
];
let
width
;
let
height
;
if
(
hasWidthHeightAttr
)
{
width
=
parseInt
(
xmlString
.
svg
.
$
[
'
width
'
]);
height
=
parseInt
(
xmlString
.
svg
.
$
[
'
height
'
]);
if
(
hasWidthHeightAttr
ibute
)
{
width
=
Number
.
parseInt
(
xmlString
.
svg
.
$
[
'
width
'
]);
height
=
Number
.
parseInt
(
xmlString
.
svg
.
$
[
'
height
'
]);
}
else
{
width
=
parseInt
(
xmlString
.
svg
.
$
[
'
viewbox
'
].
toString
().
replace
(
/^
\d
+
\s\d
+
\s(\d
+
\.?[\d])\s(\d
+
\.?[\d])
/
,
"
$1
"
));
height
=
parseInt
(
xmlString
.
svg
.
$
[
'
viewbox
'
].
toString
().
replace
(
/^
\d
+
\s\d
+
\s(\d
+
\.?[\d])\s(\d
+
\.?[\d])
/
,
"
$2
"
));
width
=
Number
.
parseInt
(
xmlString
.
svg
.
$
[
'
viewbox
'
]
.
toString
()
.
replace
(
/^
\d
+
\s\d
+
\s(\d
+
\.?[\d])\s(\d
+
\.?[\d])
/
,
'
$1
'
),
);
height
=
Number
.
parseInt
(
xmlString
.
svg
.
$
[
'
viewbox
'
]
.
toString
()
.
replace
(
/^
\d
+
\s\d
+
\s(\d
+
\.?[\d])\s(\d
+
\.?[\d])
/
,
'
$2
'
),
);
}
return
{
width
,
height
height
,
};
}
@@ -124,18 +141,16 @@ module.exports = class InlineSvg {
* @returns {Promise}
*/
async
_readFiles
(
_path
)
{
return
new
Promise
(
async
(
resolve
,
reject
)
=>
{
let
files
=
await
globby
(
path
.
join
(
_path
,
'
*.svg
'
));
let
svgs
=
[];
await
files
.
forEach
(
async
_file
=>
{
let
content
=
fs
.
readFileSync
(
_file
,
'
utf-8
'
);
svgs
.
push
({
fileName
:
_file
,
content
:
content
});
const
files
=
await
globby
.
globby
(
path
.
join
(
_path
,
'
*.svg
'
));
const
svgs
=
[];
files
.
forEach
((
_file
)
=>
{
const
content
=
fs
.
readFileSync
(
_file
,
'
utf8
'
);
svgs
.
push
({
fileName
:
_file
,
content
:
content
,
});
resolve
(
svgs
);
})
}
);
return
svgs
;
}
/**
@@ -144,6 +159,6 @@ module.exports = class InlineSvg {
* @returns {String}
*/
_getTemplateContent
()
{
return
fs
.
readFileSync
(
this
.
_options
.
template
);
return
fs
.
readFileSync
(
this
.
_options
.
template
,
'
utf8
'
);
}
}
;
}
Loading