@baris said in Wow Style tags in posts:
You haven't posted the entire code so not sure where that code is running. I am assuming it is running when posts are being parsed and you read the correct item json file and generate the img html.
There are a few things you can do to make this better.
- Don't use the sync methods these are bad for performance as they will halt the nodejs thread waiting for the operation to finish. Use fs.promises.* functions with await.
let rawItemData = await fs.promises.readFile(itemPath);
- You don't have to check if the file exists and then read it. If you try to read a file that doesn't exist it will throw an error so you can get rid of the fs.exists call.
try { let rawItemData = await fs.promises.readFile(itemPath); let itemData = JSON.parse(rawItemData.toString()); return `<img src="http://api.delteria.com/resource/${itemData.properties.icon}" alt="Avatar" class="avatar"><span class="extended-markdown-tooltip" data-toggle="tooltip" title="${itemData.properties.description}"><span style="color: ${getRarity(itemData.controllerProperties.rarity)};">${itemData.properties.itemName}</span></span>`; } catch(err) { console.error(err) }
- Reading from disk everytime the same item is rendered is probably wasteful you can cache the json data for the items after you load them. You can use the built in cache module we have.
const cache = require.main.require('./src/cache'); let item_id = code; try { let itemData = cache.get(`item_id:${item_id}`); if (!itemData) { const rawItemData = await fs.promises.readFile(itemPath); itemData = JSON.parse(rawItemData.toString()); cache.set(`item_id:${item_id}`, itemData); } return `<img src="http://api.delteria.com/resource/${itemData.properties.icon}" alt="Avatar" class="avatar"><span class="extended-markdown-tooltip" data-toggle="tooltip" title="${itemData.properties.description}"><span style="color: ${getRarity(itemData.controllerProperties.rarity)};">${itemData.properties.itemName}</span></span>`; } catch (err) { console.error(err) }
Make sure to test the code since I wrote it off the top of my head. For await to work the containing function needs to have the
async
keyword.
Firstly I would like to thank you for such a rapid response, and very helpful pointers. It led me to go and read up on the two different cache libraries it uses.
But it seems I couldn't get it to work, this portion of the edit is a plugin and not hard coded. I did intend to use your exact method but fs."promises" can't be recognized and I'm using node:LTS as my docker image (not sure if that matters). So I ended up going with a very similar method "const readFile = util.promisify(fs.readFile);".
if (typeof (code) !== "undefined") {
let item_id = code;
try {
let itemData = cache.get(`item_id:${item_id}`);
if (!itemData) {
const itemPath = path.join(__dirname, "../../../app/public/items/" + item_id + ".json");
const readFile = util.promisify(fs.readFile);
let rawItemData = await readFile(itemPath, `utf-8`);
itemData = JSON.parse(rawItemData.toString());
cache.set(`item_id:${item_id}`, itemData);
}
return `<img src="http://api.delteria.com/resource/${itemData.properties.icon}" alt="Avatar" class="avatar"><span class="extended-markdown-tooltip" data-toggle="tooltip" title="${itemData.properties.description}"><span style="color: ${getRarity(itemData.controllerProperties.rarity)};">${itemData.properties.itemName}</span></span>`;
} catch (err) {
console.error("Issue Loading Item Tag: " + err);
}
/*
let item_id = code;
const itemPath = path.join(__dirname, "../../../app/public/items/"+ item_id +".json");
try {
if (fs.existsSync(itemPath)) {
let rawItemData = fs.readFileSync(itemPath);
let itemData = JSON.parse(rawItemData.toString());
return `<img src="http://api.delteria.com/resource/${itemData.properties.icon}" alt="Avatar" class="avatar"><span class="extended-markdown-tooltip" data-toggle="tooltip" title="${itemData.properties.description}"><span style="color: ${getRarity(itemData.controllerProperties.rarity)};">${itemData.properties.itemName}</span></span>`;
}
} catch(err) {
console.error(err)
}
*/
}
But I'll continue to work on it tomorrow after work, to see if i can figure it out.