@@ -17,6 +17,10 @@ const iconPacks = {
1717 fab : require ( '@fortawesome/free-brands-svg-icons' ) ,
1818}
1919iconPacks . fa = iconPacks . fas
20+ const iconShims = require ( '@fortawesome/fontawesome-free/js/v4-shims' ) . reduce ( ( accum , it ) => {
21+ accum [ it [ 0 ] ] = [ it [ 1 ] || 'fas' , it [ 2 ] || it [ 0 ] ]
22+ return accum
23+ } , { } )
2024const { obj : map } = require ( 'through2' )
2125const merge = require ( 'merge-stream' )
2226const ospath = require ( 'path' )
@@ -200,21 +204,27 @@ function findNavPath (currentUrl, node = [], current_path = [], root = true) {
200204function injectIconDefs ( file ) {
201205 const contents = file . contents
202206 if ( ! contents . includes ( '<i class="fa' ) ) return file
203- const contentsString = contents . toString ( )
204- const iconNames = contentsString . match ( new RegExp ( '<i class="fa[brs]? fa-[^" ]+' , 'g' ) ) . map ( ( it ) => {
205- let [ iconPrefix , iconName , ] = it . substr ( 10 ) . split ( ' fa-' )
206- iconName = iconName . replace ( / (?: ^ | - ) ( .) / g, function ( _ , l ) { return l . toUpperCase ( ) } )
207- return iconPrefix + '|' + iconName
207+ const stringContents = contents . toString ( )
208+ const iconNames = stringContents . match ( new RegExp ( '<i class="fa[brs]? fa-[^" ]+' , 'g' ) ) . map ( ( it ) => {
209+ return it . substr ( 10 ) . replace ( ' fa-' , ' ' )
208210 } )
209211 if ( ! iconNames . length ) return file
210212 const iconDefs = [ ...new Set ( iconNames ) ] . reduce ( ( accum , it ) => {
211- const [ iconPrefix , iconName , ] = it . split ( '|' )
212- const iconDef = iconPacks [ iconPrefix ] && iconPacks [ iconPrefix ] [ 'fa' + iconName ]
213- return iconDef ? accum . concat ( { ...iconDef , prefix : iconPrefix } ) : accum
213+ const [ iconPrefix , iconName ] = it . split ( ' ' ) . slice ( 0 , 2 )
214+ let iconDef = ( iconPacks [ iconPrefix ] || { } ) [ 'fa' + camelCase ( iconName ) ]
215+ if ( iconDef ) {
216+ return accum . concat ( { ...iconDef , prefix : iconPrefix } )
217+ } else if ( iconPrefix === 'fa' ) {
218+ const [ realIconPrefix , realIconName ] = iconShims [ iconName ] || [ ]
219+ if ( realIconName && ( iconDef = ( iconPacks [ realIconPrefix ] || { } ) [ 'fa' + camelCase ( realIconName ) ] ) ) {
220+ return accum . concat ( { ...iconDef , prefix : realIconPrefix } )
221+ }
222+ }
223+ return accum
214224 } , [ ] )
215225 const iconData = `<script>\nwindow.FontAwesomeIconDefs = ${ JSON . stringify ( iconDefs ) } \n</script>`
216226 file . contents = Buffer . from (
217- contentsString . replace ( new RegExp ( '<script async src="[^"]*_/js/vendor/fontawesome.js"></script>' ) , function ( m ) {
227+ stringContents . replace ( new RegExp ( '<script async src="[^"]*_/js/vendor/fontawesome.js"></script>' ) , function ( m ) {
218228 return [ iconData , m ] . join ( '\n' )
219229 } )
220230 )
@@ -249,3 +259,7 @@ function toPromise (stream) {
249259 . on ( 'finish' , ( ) => resolve ( data ) )
250260 )
251261}
262+
263+ function camelCase ( str ) {
264+ return str . replace ( / (?: ^ | - ) ( .) / g, function ( _ , l ) { return l . toUpperCase ( ) } )
265+ }
0 commit comments