Skip to content

Commit e1fbd4f

Browse files
committed
Add fuzzy determination of assets folder
1 parent 6f0d386 commit e1fbd4f

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

scripts/validation/domain/article.js

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,7 @@ export class Article {
190190
*/
191191
get referencedAssetsPaths(){
192192
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;
195194

196195
const pathRegex = new RegExp(`^(?!http).*(${this.assetsFolder})\/.*(?:\..{1,4})$`);
197196
const filteredFilePaths = this.links.filter((link) => link.match(pathRegex));
@@ -210,11 +209,14 @@ export class Article {
210209
* Returns all hyperlinks in the document
211210
*/
212211
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);
215214
}
216215

217216

217+
/**
218+
* Determines the assets folder used by an article
219+
*/
218220
get assetsFolder(){
219221
if(this._assetFolder) return this._assetFolder;
220222
const validDirectories = ["assets", "images"];
@@ -227,11 +229,30 @@ export class Article {
227229
console.log("😬 WARNING: Using deprecated 'images' directory to store assets. Location:", this.path);
228230
this._assetFolder = validDirectories[1];
229231
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];
232245
return null;
233246
}
234247

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+
235256
/**
236257
* Returns the assets path if it's one of the standard ones 'assets' or 'images', null otherwise.
237258
*/

0 commit comments

Comments
 (0)