1+ const fs = require ( 'fs' ) ;
2+ const path = require ( 'path' ) ;
3+ const dir = '/Users/yashdalwani/Documents/BrowserStack-Demo-Hub' ;
4+ const svg = fs . readFileSync ( path . join ( dir , 'product-section-color-icons-v6.svg' ) , 'utf8' ) ;
5+
6+ // 1. Parse all gradient definitions
7+ const gradDefs = { } ;
8+ const gradDefRe = / < l i n e a r G r a d i e n t i d = " ( p a i n t \d + _ l i n e a r _ 1 0 1 3 _ 9 8 ) " [ \s \S ] * ?< \/ l i n e a r G r a d i e n t > / g;
9+ let m ;
10+ while ( ( m = gradDefRe . exec ( svg ) ) !== null ) {
11+ const id = m [ 0 ] . match ( / i d = " ( p a i n t \d + _ l i n e a r _ 1 0 1 3 _ 9 8 ) " / ) [ 1 ] ;
12+ gradDefs [ id ] = m [ 0 ] ;
13+ }
14+
15+ // 2. Parse gradient y-positions to identify icon groups
16+ const gradYRe = / < l i n e a r G r a d i e n t i d = " ( p a i n t \d + _ l i n e a r _ 1 0 1 3 _ 9 8 ) " x 1 = " [ \d . ] + ?" y 1 = " ( [ \d . ] + ?) " / g;
17+ const grads = [ ] ;
18+ while ( ( m = gradYRe . exec ( svg ) ) !== null ) grads . push ( { id : m [ 1 ] , y : parseFloat ( m [ 2 ] ) } ) ;
19+
20+ const iconGroups = [ ] ;
21+ let group = [ grads [ 0 ] ] ;
22+ for ( let i = 1 ; i < grads . length ; i ++ ) {
23+ if ( grads [ i ] . y - grads [ i - 1 ] . y > 20 ) { iconGroups . push ( group ) ; group = [ grads [ i ] ] ; }
24+ else group . push ( grads [ i ] ) ;
25+ }
26+ iconGroups . push ( group ) ;
27+
28+ // 3. Collect all drawable elements with their position in the SVG
29+ const elemRe = / < ( p a t h | e l l i p s e | c i r c l e | r e c t ) ( \s [ ^ > ] * ) \/ > / g;
30+ const allElems = [ ] ;
31+ while ( ( m = elemRe . exec ( svg ) ) !== null ) {
32+ allElems . push ( { tag : m [ 0 ] , pos : m . index } ) ;
33+ }
34+
35+ // 4. For each icon group, find the SVG slice between first and last element referencing its paints
36+ const iconNames = [
37+ 'icon-01' , 'icon-02' , 'icon-03' , 'icon-04-lca' , 'icon-05' , 'icon-06' ,
38+ 'icon-07' , 'icon-08' , 'icon-09' , 'icon-10' , 'icon-11' , 'icon-12' ,
39+ 'icon-13' , 'icon-14-live' , 'icon-15' , 'icon-16' , 'icon-17' , 'icon-18' ,
40+ 'icon-19' , 'icon-20' , 'icon-21' , 'icon-22' , 'icon-23'
41+ ] ;
42+
43+ fs . mkdirSync ( path . join ( dir , 'icons' ) , { recursive : true } ) ;
44+
45+ iconGroups . forEach ( ( grp , idx ) => {
46+ const paintIds = new Set ( grp . map ( g => g . id ) ) ;
47+ const minY = Math . min ( ...grp . map ( g => g . y ) ) ;
48+ const nextMinY = idx + 1 < iconGroups . length
49+ ? Math . min ( ...iconGroups [ idx + 1 ] . map ( g => g . y ) )
50+ : 1677 ;
51+ const iconY = Math . max ( 0 , minY - 14 ) ;
52+ const iconH = Math . min ( 44 , nextMinY - iconY ) ;
53+
54+ // Find positions of elements referencing this icon's paints
55+ const myElems = allElems . filter ( e => {
56+ for ( const pid of paintIds ) {
57+ if ( e . tag . includes ( pid ) ) return true ;
58+ }
59+ return false ;
60+ } ) ;
61+
62+ if ( myElems . length === 0 ) {
63+ console . log ( `⚠ ${ iconNames [ idx ] } : no elements found` ) ;
64+ return ;
65+ }
66+
67+ const firstPos = myElems [ 0 ] . pos ;
68+ const lastElem = myElems [ myElems . length - 1 ] ;
69+ const lastPos = lastElem . pos + lastElem . tag . length ;
70+
71+ // Grab all elements in the SVG between firstPos and lastPos (includes white overlays)
72+ const sliceElems = allElems . filter ( e => e . pos >= firstPos && e . pos <= lastPos ) ;
73+
74+ // Collect gradient defs for this icon
75+ const prefix = 'i' + ( idx + 1 ) ;
76+ let defsOut = grp . map ( g => ( gradDefs [ g . id ] || '' ) ) . join ( '\n ' )
77+ . replace ( / p a i n t ( \d + ) _ l i n e a r _ 1 0 1 3 _ 9 8 / g, prefix + '_p$1' ) ;
78+ let contentOut = sliceElems . map ( e => e . tag ) . join ( '\n ' )
79+ . replace ( / p a i n t ( \d + ) _ l i n e a r _ 1 0 1 3 _ 9 8 / g, prefix + '_p$1' ) ;
80+
81+ const out = [
82+ '<svg xmlns="http://www.w3.org/2000/svg" width="44" height="44" viewBox="0 0 44 ' + iconH . toFixed ( 1 ) + '" fill="none">' ,
83+ ' <defs>' ,
84+ ' ' + defsOut ,
85+ ' </defs>' ,
86+ ' <g transform="translate(0,' + ( - iconY ) . toFixed ( 1 ) + ')">' ,
87+ ' ' + contentOut ,
88+ ' </g>' ,
89+ '</svg>'
90+ ] . join ( '\n' ) ;
91+
92+ const fname = path . join ( dir , 'icons' , iconNames [ idx ] + '.svg' ) ;
93+ fs . writeFileSync ( fname , out ) ;
94+ console . log ( '✓ ' + iconNames [ idx ] + '.svg (' + sliceElems . length + ' elems, y=' + iconY . toFixed ( 0 ) + ')' ) ;
95+ } ) ;
0 commit comments