@@ -190,8 +190,7 @@ export class Article {
190
190
*/
191
191
get referencedAssetsPaths ( ) {
192
192
if ( this . _referencedAssetsPaths ) return this . _referencedAssetsPaths ;
193
- const images = this . html . querySelectorAll ( "img" ) ;
194
- const imagePaths = images . map ( image => image . attributes . src ) ;
193
+ const imagePaths = this . referencedImages ;
195
194
196
195
const pathRegex = new RegExp ( `^(?!http).*(${ this . assetsFolder } )\/.*(?:\..{1,4})$` ) ;
197
196
const filteredFilePaths = this . links . filter ( ( link ) => link . match ( pathRegex ) ) ;
@@ -210,11 +209,14 @@ export class Article {
210
209
* Returns all hyperlinks in the document
211
210
*/
212
211
get links ( ) {
213
- let links = this . html . querySelectorAll ( "a" ) ;
214
- return links . map ( link => link . attributes . href ) ;
212
+ let linkElements = this . html . querySelectorAll ( "a" ) ;
213
+ return linkElements . map ( element => element . attributes . href ) ;
215
214
}
216
215
217
216
217
+ /**
218
+ * Determines the assets folder used by an article
219
+ */
218
220
get assetsFolder ( ) {
219
221
if ( this . _assetFolder ) return this . _assetFolder ;
220
222
const validDirectories = [ "assets" , "images" ] ;
@@ -227,11 +229,30 @@ export class Article {
227
229
console . log ( "😬 WARNING: Using deprecated 'images' directory to store assets. Location:" , this . path ) ;
228
230
this . _assetFolder = validDirectories [ 1 ] ;
229
231
return this . _assetFolder ;
230
- }
231
- console . log ( `😬 WARNING: No standard assets directory (${ validDirectories . join ( " | " ) } ) found in: ${ this . path } ` ) ;
232
+ }
233
+
234
+ console . log ( `😬 WARNING: No standard assets directory (${ validDirectories . join ( " | " ) } ) found in: ${ this . path } ` ) ;
235
+
236
+ // Try to figure out assets path from the referenced images
237
+ const usedAssetPaths = this . referencedImages . map ( ( assetPath ) => {
238
+ const directory = path . dirname ( assetPath )
239
+ if ( ! directory ) return null ;
240
+ return directory . split ( "/" ) [ 0 ] ;
241
+ } )
242
+
243
+ const uniqueAssetPaths = usedAssetPaths . filter ( ( element , index ) => { return usedAssetPaths . indexOf ( element ) == index ; } ) ;
244
+ if ( uniqueAssetPaths . length == 1 ) return uniqueAssetPaths [ 0 ] ;
232
245
return null ;
233
246
}
234
247
248
+ /**
249
+ * Returns a list of referenced images in the article
250
+ */
251
+ get referencedImages ( ) {
252
+ const images = this . html . querySelectorAll ( "img" ) ;
253
+ return images . map ( image => image . attributes . src ) ;
254
+ }
255
+
235
256
/**
236
257
* Returns the assets path if it's one of the standard ones 'assets' or 'images', null otherwise.
237
258
*/
0 commit comments