Skip to content
This repository was archived by the owner on Aug 11, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions scripts/validation/domain/tutorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@ var Tutorial = class Tutorial {
}

get path(){
return this.basePath;
}

get contentFilePath(){
return this.basePath + "/content.md";
}

get markdown(){
if(!fs.existsSync(this.path)){
console.log("❌ File doens't exist " + this.path);
if(!fs.existsSync(this.contentFilePath)){
console.log("❌ File doens't exist " + this.contentFilePath);
return null;
}
let rawData = fs.readFileSync(this.path).toString();
let rawData = fs.readFileSync(this.contentFilePath).toString();
const content = fm(rawData);
return content.body;
}
Expand Down Expand Up @@ -80,11 +84,11 @@ var Tutorial = class Tutorial {

get metadata(){
try {
let rawData = fs.readFileSync(this.path).toString();
let rawData = fs.readFileSync(this.contentFilePath).toString();
const content = fm(rawData);
return content.attributes;
} catch (error) {
console.log("Error occurred while parsing " + this.path);
console.log("Error occurred while parsing " + this.contentFilePath);
console.log(error);
return null;
}
Expand Down
37 changes: 21 additions & 16 deletions scripts/validation/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,29 @@ validator.addValidation(async (tutorials) => {
let jsonData = tutorial.metadata;
if(!jsonData) {
const errorMessage = "No metadata found";
errorsOccurred.push(new ValidationError(errorMessage, tutorial.path));
errorsOccurred.push(new ValidationError(errorMessage, tutorial.contentFilePath));
return;
}

try {
if(!jsonData.coverImage){
const errorMessage = "No cover image found";
errorsOccurred.push(new ValidationError(errorMessage, tutorial.path));
errorsOccurred.push(new ValidationError(errorMessage, tutorial.contentFilePath));
} else if (jsonData.coverImage.indexOf(".svg") == -1) {
const errorMessage = "Cover image is not in SVG format.";
errorsOccurred.push(new ValidationError(errorMessage, tutorial.path));
errorsOccurred.push(new ValidationError(errorMessage, tutorial.contentFilePath));
}

let jsonSchema = JSON.parse(fs.readFileSync(config.metadataSchema));
let validationResult = validate(jsonData, jsonSchema);
if(validationResult.errors.length != 0){
const errorMessage = `An error occurred while validating the metadata ${validationResult}`;
errorsOccurred.push(new ValidationError(errorMessage, tutorial.path));
errorsOccurred.push(new ValidationError(errorMessage, tutorial.contentFilePath));
}

} catch (error) {
const errorMessage = "An error occurred while parsing the metadata";
errorsOccurred.push(new ValidationError(errorMessage, tutorial.path));
errorsOccurred.push(new ValidationError(errorMessage, tutorial.contentFilePath));
}
});
return errorsOccurred;
Expand All @@ -70,11 +70,11 @@ validator.addValidation(async (tutorials) => {
tutorial.headings.forEach(heading => {
if(tc.titleCase(heading) != heading){
const errorMessage = heading + "' is not title case";
errorsOccurred.push(new ValidationError(errorMessage, tutorial.path));
errorsOccurred.push(new ValidationError(errorMessage, tutorial.contentFilePath));
}
if(heading.length > config.headingMaxLength){
const errorMessage = heading + "' (" + heading.length + ") exceeds the max length (" + config.headingMaxLength + ")";
errorsOccurred.push(new ValidationError(errorMessage, tutorial.path));
errorsOccurred.push(new ValidationError(errorMessage, tutorial.contentFilePath));
}
});
});
Expand All @@ -97,7 +97,7 @@ validator.addValidation(async (tutorials) => {
// Detect if there are embedded images that are actually rendered
if(image.attributes.width || image.attributes.height){
const errorMessage = path + " contains embedded binary images.";
errorsOccurred.push(new ValidationError(errorMessage, tutorial.path, "warning"));
errorsOccurred.push(new ValidationError(errorMessage, tutorial.contentFilePath, "warning"));
}
}
});
Expand Down Expand Up @@ -131,7 +131,7 @@ validator.addValidation(async (tutorials) => {
console.log('👍 %s is alive', result.link);
} else if(result.status == "dead" && result.statusCode !== 0){
const errorMessage = `${result.link} is dead 💀 HTTP ${result.statusCode}`;
errorsOccurred.push(new ValidationError(errorMessage, tutorial.path));
errorsOccurred.push(new ValidationError(errorMessage, tutorial.contentFilePath));
}
});
resolve(errorsOccurred);
Expand Down Expand Up @@ -159,7 +159,7 @@ validator.addValidation(async (tutorials) => {
assetNames.forEach(asset => {
if(coverImageName == asset) return;
if(!imageNames.includes(asset) && !linkNames.includes(asset)){
const errorMessage = asset + " is not used";
const errorMessage = asset + " is not used.";
errorsOccurred.push(new ValidationError(errorMessage, tutorial.path));
}
});
Expand All @@ -169,7 +169,7 @@ validator.addValidation(async (tutorials) => {


/**
* Verify that the images don't have an absolute path
* Verify that the images exist and don't have an absolute path
*/
validator.addValidation(async (tutorials) => {
let errorsOccurred = [];
Expand All @@ -179,7 +179,12 @@ validator.addValidation(async (tutorials) => {
const errorMessage = "Image uses an absolute path: " + imagePath;
const content = tutorial.markdown;
const lineNumber = fileHelper.getLineNumberFromIndex(content.indexOf(imagePath), content);
errorsOccurred.push(new ValidationError(errorMessage, tutorial.path, lineNumber));
errorsOccurred.push(new ValidationError(errorMessage, tutorial.contentFilePath, "error", lineNumber));
} else if(!imagePath.startsWith("http") && !fs.existsSync(`${tutorial.path}/${imagePath}`)){
const errorMessage = "Image doesn't exist: " + imagePath;
const content = tutorial.markdown;
const lineNumber = fileHelper.getLineNumberFromIndex(content.indexOf(imagePath), content);
errorsOccurred.push(new ValidationError(errorMessage, tutorial.contentFilePath, "error", lineNumber));
}
});
});
Expand All @@ -197,7 +202,7 @@ validator.addValidation(async (tutorials) => {
let nodes = tutorial.html.querySelectorAll("li ul");
if(nodes && nodes.length > 0){
const errorMessage = "Content uses nested lists";
errorsOccurred.push(new ValidationError(errorMessage, tutorial.path));
errorsOccurred.push(new ValidationError(errorMessage, tutorial.contentFilePath));
}
});
return errorsOccurred;
Expand All @@ -214,7 +219,7 @@ validator.addValidation(async (tutorials) => {
const imageDescription = image.attributes.alt;
if(imageDescription.split(" ").length <= 1){
const errorMessage = "Image doesn't have a description: " + image.attributes.src;
errorsOccurred.push(new ValidationError(errorMessage, tutorial.path));
errorsOccurred.push(new ValidationError(errorMessage, tutorial.contentFilePath));
}
});
});
Expand All @@ -233,7 +238,7 @@ validator.addValidation(async (tutorials) => {
if(syntax) syntax = syntax.replace(PARSER_SYNTAX_PREFIX, '');
if(!config.allowedSyntaxSpecifiers.includes(syntax)){
const errorMessage = "Code block uses unsupported syntax: " + syntax;
errorsOccurred.push(new ValidationError(errorMessage, tutorial.path));
errorsOccurred.push(new ValidationError(errorMessage, tutorial.contentFilePath));
}
});
});
Expand Down Expand Up @@ -274,7 +279,7 @@ validator.addValidation(async (tutorials) => {
}
if((match === null && rule.shouldMatch) || (match !== null && !rule.shouldMatch)) {
const errorMessage = rule.errorMessage;
errorsOccurred.push(new ValidationError(errorMessage, tutorial.path, ruleType, lineNumber));
errorsOccurred.push(new ValidationError(errorMessage, tutorial.contentFilePath, ruleType, lineNumber));
}
}

Expand Down