Skip to content

Instantly share code, notes, and snippets.

@javanigus
Created September 24, 2023 17:18
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 javanigus/1cc208722ee2f366505c6ec408364b1c to your computer and use it in GitHub Desktop.
Save javanigus/1cc208722ee2f366505c6ec408364b1c to your computer and use it in GitHub Desktop.
Convert document-relative image paths to root-relative paths
/*
This NodeJS script will recursively read files in a folder
and execute a series of search and replace commands.
It will convert document-relative image paths to root-relative paths, e.g.
../assets/images/home_page_banner.png
to
/public/assets/images/home_page_banner.png
Set the name of the folder as the value of the "dir" variable.
Run the script as follows: node recursive-replace.js
*/
const fs = require('fs');
const path = require('path');
const walk = dir => {
try {
let results = [];
const list = fs.readdirSync(dir);
list.forEach(file => {
file = path.join(dir, file);
const stat = fs.statSync(file);
if (stat && stat.isDirectory()) {
// Recurse into subdir
results = [...results, ...walk(file)];
} else {
// Is a file
results.push(file);
}
});
return results;
} catch (error) {
console.error(`Error when walking dir ${dir}`, error);
}
};
const edit = filePath => {
if (filePath.endsWith('htm')) {
let oldContent = fs.readFileSync(filePath, {encoding: 'utf8'});
let newContent = oldContent;
// REPLACE {{#if class}} with {% if class %}
let regex = /src=(.*?\.png)"/gi;
let result = oldContent.match(regex);
// console.log(result);
//let pathSplit = filePath.split("/");
/* console.log(pathSplit);
console.log(pathSplit.pop())
console.log(pathSplit);
console.log(pathSplit.pop())
console.log(pathSplit); */
if (result !== null) {
for (let i = 0; i < result.length; i++) {
console.log("-------------");
const iterator = result[i].matchAll(/\.\.\//g);
let oldImgPath = result[i];
console.log("oldImgPath = " + oldImgPath);
let numMatches = Array.from(iterator).length;
console.log("number of ../ = " + numMatches);
console.log("filePath = " + filePath);
let pathSplit = filePath.split("/");
for (let j = 0; j <= parseInt(numMatches); j++) {
console.log("j=" + j);
pathSplit.pop();
}
let imgBasePath = "/" + pathSplit.join('/') + "/";
console.log("imgBasePath = " + imgBasePath);
let regex2 = /(\.\.\/)+/gi;
let newImgPath = oldImgPath.replace(regex2, imgBasePath);
console.log("newImgPath = " + newImgPath);
//console.log(oldContent);
var re = new RegExp(oldImgPath, "g");
newContent = newContent.replace(re, newImgPath);
console.log("-------------");
}
}
//var replaceVal = 'src="{% if $1 %}';
//var newContent = oldContent.replace(regex, replaceVal);
fs.writeFileSync(filePath, newContent, {encoding: 'utf-8'});
console.log(`Edited file: ${filePath}`);
}
};
const main = () => {
const dir = 'public/'; // folder name containing files
const filePaths = walk(dir);
filePaths.forEach(filePath => edit(filePath));
};
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment