Skip to content

Remove implicit assets folder warning #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
25 changes: 8 additions & 17 deletions domain/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export class Article {
this._codeBlockData = null;
this._referencedAssetsPaths = null;
this._emphasizedTextData = null;
this._assetFolder = null;
}

/**
Expand Down Expand Up @@ -259,30 +260,20 @@ export class Article {
*/
get assetsFolder(){
if(this._assetFolder) return this._assetFolder;
const validDirectories = ["assets", "images"];

if (existsSync(`${this.path}/${validDirectories[0]}/`)){
this._assetFolder = validDirectories[0];
return this._assetFolder;
}
if (existsSync(`${this.path}/${validDirectories[1]}/`)){
console.log("😬 WARNING: Using deprecated 'images' directory to store assets. Location:", this.path);
this._assetFolder = validDirectories[1];
return this._assetFolder;
}

console.log(`😬 WARNING: No standard assets directory (${validDirectories.join(" | ")}) found in: ${this.path}`);

// Try to figure out assets path from the referenced images
// Figure out assets path(s) from the referenced images
const usedAssetPaths = this.referencedImages.map((assetPath) => {
const directory = path.dirname(assetPath)
if(!directory) return null;
return directory.split("/")[0];
})
});

const uniqueAssetPaths = usedAssetPaths.filter((element, index) => { return usedAssetPaths.indexOf(element) == index; });
if(uniqueAssetPaths.length == 1) return uniqueAssetPaths[0];
return null;
if(uniqueAssetPaths.length == 1) {
this._assetFolder = uniqueAssetPaths[0];
}

return this._assetFolder;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ import { validateSyntaxSpecifiers } from './validations/code-blocks.js';
import { validateNestedLists } from './validations/lists.js';
import { validateBrokenLinks } from './validations/links.js';
import { ConfigManager } from './logic/config-manager.js';
import { validateFolderName } from './validations/naming.js';
import { validateFolderName, validateAssetsFolderName } from './validations/naming.js';

export { Validator, ArticleManager, validateDuplicatedOpeningHeading, validateHeadingsNesting, validateMaxLength, validateNumberedHeadings, validateOpeningHeadingLevel, validateSpacing, validateTitleCase, validateMetaData, validateRules, validateImageDescriptions, validateImagePaths, validateReferencedAssets, validateSVGFiles, validateSyntaxSpecifiers, validateNestedLists, validateBrokenLinks, ConfigManager, validateFolderName }
export { Validator, ArticleManager, validateDuplicatedOpeningHeading, validateHeadingsNesting, validateMaxLength, validateNumberedHeadings, validateOpeningHeadingLevel, validateSpacing, validateTitleCase, validateMetaData, validateRules, validateImageDescriptions, validateImagePaths, validateReferencedAssets, validateSVGFiles, validateAssetsFolderName, validateSyntaxSpecifiers, validateNestedLists, validateBrokenLinks, ConfigManager, validateFolderName }
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "content-lint",
"version": "1.1.1",
"version": "1.2.0",
"type": "module",
"main": "index.js",
"description": "A node.js infrastructure to validate markdown content.",
Expand Down
6 changes: 3 additions & 3 deletions test/headings.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,23 @@ test('Test if forbidden opening heading level is detected', () => {

test('Test if missing title case is detected', () => {
const errors = validateTitleCase(article);
console.log(errors);
// console.log(errors);
expect(errors.length).toBe(1);
expect(errors[0].lineNumber).toBe(16);
expect(errors[0].column).toBe(1);
});

test('Test if too long titles are detected', () => {
const errors = validateMaxLength(article, 30);
console.log(errors);
// console.log(errors);
expect(errors.length).toBe(1);
expect(errors[0].lineNumber).toBe(18);
expect(errors[0].column).toBe(1);
});

test('Test if duplicated opening titles are detected', () => {
const errors = validateDuplicatedOpeningHeading(article);
console.log(errors);
// console.log(errors);
expect(errors.length).toBe(1);
expect(errors[0].lineNumber).toBe(5);
expect(errors[0].column).toBe(1);
Expand Down
4 changes: 2 additions & 2 deletions test/metadata.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ const schemaPath = "./test/resources/tutorial-metadata-schema.json";

test('Tests if missing/superfluous property is detected', () => {
const errors = validateMetaData(articleA, schemaPath);
console.log(errors);
// console.log(errors);
expect(errors.length).toBe(2);
});


test('Tests if title lenght restriction is detected', () => {
const errors = validateMetaData(articleB, schemaPath);
console.log(errors);
// console.log(errors);
expect(errors.length).toBe(1);
});
23 changes: 20 additions & 3 deletions test/naming.test.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,37 @@
import { Article } from '../domain/article.js'
import { validateFolderName } from '../validations/naming.js';
import { validateFolderName, validateAssetsFolderName } from '../validations/naming.js';

const articleA = new Article();
articleA.path = "./demo/this_is_Not_compliant";

const articleB = new Article();
articleB.path = "./demo/this-should-pass";
articleB.rawData = "![](assets/dummy.png)"

const articleC = new Article();
articleC.rawData = "![](img/dummy.png)";

test('Tests if non-compliant article folder name is detected', () => {
const errors = validateFolderName(articleA);
console.log(errors);
// console.log(errors);
expect(errors.length).toBe(2);
});

test('Tests if compliant article folder name passes validation', () => {
const errors = validateFolderName(articleB);
console.log(errors);
// console.log(errors);
expect(errors.length).toBe(0);
});


test('Tests if incorrect assets name fails validation', () => {
const errors = validateAssetsFolderName(articleC, "assets");
// console.log(errors);
expect(errors.length).toBe(1);
});

test('Tests if correct assets name passes validation', () => {
const errors = validateAssetsFolderName(articleB, "assets");
// console.log(errors);
expect(errors.length).toBe(0);
});
26 changes: 25 additions & 1 deletion validations/naming.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,28 @@ function validateFolderName(article) {
return errorsOccurred;
}

export { validateFolderName }

/**
* Checks if the article uses one of the allowed assets folder name.
* @param {Article} article
* @param {String} expectedFolderName the expected assets folder name
* @returns an array of ValidationIssue objects for the found issues.
*/
function validateAssetsFolderName(article, expectedFolderName){

if(article.referencedImages.length == 0) return [];

if(article.assetsFolder === null){
const errorMessage = "Multiple asset directories used";
return [new ValidationIssue(errorMessage, article.contentFilePath)];
}

if(article.assetsFolder !== expectedFolderName){
const errorMessage = `Unexpected or deprecated assets directory '${article.assetsFolder}' used`;
return [new ValidationIssue(errorMessage, article.contentFilePath, ValidationIssue.Type.WARNING)];
}

return [];
}

export { validateFolderName, validateAssetsFolderName }