Skip to content

Instantly share code, notes, and snippets.

@CSEKU160212
Last active February 19, 2024 18:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CSEKU160212/8f2bc1f11703cbbad9e8e002e1f18df3 to your computer and use it in GitHub Desktop.
Save CSEKU160212/8f2bc1f11703cbbad9e8e002e1f18df3 to your computer and use it in GitHub Desktop.
function getFileExtension(fullFileName){
const extension = fullFileName.split('.').pop();
return extension;
}
function getFileName(fullFileName){
var fileName = fullFileName.substr(0, fullFileName.lastIndexOf('.'));
return fileName;
}
function isImageFile(fullFileName){
return (/\.(gif|jpe?g|tiff?|png|webp|bmp)$/i).test(fullFileName);
}
function checkAnImageExists(dataSet,fileNameWithoutExtension, startIndex){
let imageIndex = -1;
for(let j=startIndex; j<dataSet.length; j++){
const currentFileName = getFileName(dataSet[j]);
if(isImageFile(dataSet[j]) && currentFileName == fileNameWithoutExtension){
imageIndex = j;
break;
}
}
return imageIndex;
}
let dataSet = ["a.md", "a.jpg", "b.md", "b.png"];
//let dataSet = ["a.md", "a.jpg", "b.md", "b.png", "c.md"];
let newData = [];
for(let i=0; i<dataSet.length; i++){
console.log("Loop: ", i);
if(["md", "MD"].indexOf(getFileExtension(dataSet[i])) > -1 ){
console.log("Content: ", dataSet[i]);
let content = dataSet[i];
let image = "";
let imageIndex = checkAnImageExists(dataSet, getFileName(dataSet[i]), i);
if(imageIndex != -1){
image = dataSet[imageIndex];
newData.push({content, image });
}else{
console.log(`Image file is missing for ${dataSet[i]}`);
newData = [];
break;
}
}else{
continue;
}
}
for(let i=0; i<newData.length; i++){
console.log(newData[i]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment