I'm making a program using Node JS
and I'm using fs module
. I want to do something like I've a folder in which I have SVG icons.
const fs = require('fs');
const path = require('path');
const dir = 'icons/'
fs.readdir(dir, (err, files) => {
if (err) throw err;
files.forEach((file) => {
if (path.extname(file) == '.svg') {
fs.readFile(dir + file, 'utf-8', (e, res) => {
if (e) throw e;
console.log(res);
});
}
});
});
So, in the above mentioned code I want to do something like, the program will check in the DOM that if any <span></span>
tag have classname similar to icon or file name and if its true then it will read the respective file content and append it in the span tag.
For Example
Filename = "bx bx-at.svg"
and <span class="bx bx-at.svg"></span>
then output will be
<span class="bx bx-at">
<svg width="24" height="24" viewBox="0 0 24 24">
<path d="M12,2C6.486,2,2,6.486,2,12s4.486,10,10,10c1.466,0,2.961-0.371,4.442-1.104l-0.885-1.793C14.353,19.698,13.156,20,12,20 c-4.411,0-8-3.589-8-8s3.589-8,8-8s8,3.589,8,8v1c0,0.692-0.313,2-1.5,2c-1.396,0-1.494-1.819-1.5-2V8h-2v0.025 C14.162,7.391,13.13,7,12,7c-2.757,0-5,2.243-5,5s2.243,5,5,5c1.45,0,2.748-0.631,3.662-1.621C16.186,16.269,17.07,17,18.5,17 c2.273,0,3.5-2.061,3.5-4v-1C22,6.486,17.514,2,12,2z M12,15c-1.654,0-3-1.346-3-3s1.346-3,3-3s3,1.346,3,3S13.654,15,12,15z">
</path>
</svg>
</span>