Last active
February 19, 2024 18:33
-
-
Save CSEKU160212/8f2bc1f11703cbbad9e8e002e1f18df3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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