@@ -937,27 +937,65 @@ module.exports = {
937937 // Quickly replaces inline image placeholder URLs with
938938 // actual, SEO-friendly URLs based on `widget._relatedDocs`.
939939 linkImages ( widget , content ) {
940+ console . log ( 'content' , content ) ;
940941 // "Why no regexps?" We need to do this as quickly as we can.
941942 // indexOf and lastIndexOf are much faster.
942943 let i ;
943944 for ( const doc of ( widget . _relatedDocs || [ ] ) ) {
944945 let offset = 0 ;
945946 while ( true ) {
946947 const target = `${ self . apos . modules [ '@apostrophecms/image' ] . action } /${ doc . aposDocId } /src` ;
948+ console . log ( 'target' , target ) ;
947949 i = content . indexOf ( target , offset ) ;
950+ console . log ( 'i' , i ) ;
948951 if ( i === - 1 ) {
952+ console . log ( '---' ) ;
949953 break ;
950954 }
951955 offset = i + target . length ;
956+ console . log ( 'offset' , offset ) ;
952957 // If you can edit the widget, you don't want the link replaced,
953958 // as that would lose the image if you edit the widget
954959 const left = content . lastIndexOf ( '<' , i ) ;
960+ console . log ( 'left' , left ) ;
955961 const src = content . indexOf ( ' src="' , left ) ;
962+ console . log ( 'src' , src ) ;
956963 const close = content . indexOf ( '"' , src + 6 ) ;
964+ console . log ( 'close' , close ) ;
957965 if ( ( left !== - 1 ) && ( src !== - 1 ) && ( close !== - 1 ) ) {
958966 content = content . substring ( 0 , src + 5 ) + doc . attachment . _urls [ self . apos . modules [ '@apostrophecms/image' ] . getLargestSize ( ) ] + content . substring ( close + 1 ) ;
967+ console . log ( 'content' , content ) ;
968+ console . log ( '---' ) ;
969+
970+ // update or insert alt attribute
971+ const tagEnd = content . indexOf ( '>' , left ) ;
972+ if ( tagEnd !== - 1 ) {
973+ let imgTag = content . substring ( left , tagEnd + 1 ) ;
974+ // Look for alt attribute in the tag
975+ const altAttr = ' alt="' ;
976+ const altIndex = imgTag . indexOf ( altAttr ) ;
977+ if ( altIndex !== - 1 ) {
978+ // Replace the existing alt value
979+ const altValueStart = altIndex + altAttr . length ;
980+ const altValueEnd = imgTag . indexOf ( '"' , altValueStart ) ;
981+ imgTag = imgTag . substring ( 0 , altValueStart ) +
982+ self . apos . util . escapeHtml ( doc . alt || '' ) +
983+ imgTag . substring ( altValueEnd ) ;
984+ } else {
985+ // Insert alt attribute before closing >
986+ imgTag = imgTag . replace (
987+ / \/ ? > $ / ,
988+ ` alt="${ self . apos . util . escapeHtml ( doc . alt || '' ) } "$&`
989+ ) ;
990+ }
991+ // Replace the tag in content
992+ content = content . substring ( 0 , left ) +
993+ imgTag + content . substring ( tagEnd + 1 ) ;
994+ }
995+
959996 } else {
960997 // So we don't get stuck in an infinite loop
998+ console . log ( '---' ) ;
961999 break ;
9621000 }
9631001 }
0 commit comments