Thank you for your hard work.
I was touched that the nodebb community accepts users' needs quickly and positively, and reflects them immediately.
Thanks once again.
Thank you for your hard work.
I was touched that the nodebb community accepts users' needs quickly and positively, and reflects them immediately.
Thanks once again.
Thank you very much for your positive feedback.
It's a very useful feature improvement.
I want to make a plugin that works as below.
Please advise me on how to handle step 3.
It's the source I've worked on so far
library.js
'use strict';
const axios = require('axios');
const fs = require('fs');
const path = require('path');
const os = require('os');
const { mkdirSync } = require('fs');
const topics = require.main.require('./src/topics');
const plugin = module.exports;
var IMAGE_DIR = '/tmp/nodebb-images'
plugin.init = async function (params) {
//const { router /* , middleware , controllers */ } = params;
IMAGE_DIR = await path.join(os.tmpdir(), 'nodebb-images');
const hasImgDir = await fs.existsSync(IMAGE_DIR);
if (!hasImgDir) {
await mkdirSync(IMAGE_DIR);
}
};
plugin.onTopicCreate = async function(hookData) {
//await console.log("----------------------------- onTopicCreate -----------------------------------");
//console.log(hookData);
await setTopicThumbnail(hookData.topic.tid, hookData.data.content);
return hookData;
};
plugin.onTopicEdit = async function(hookData) {
await console.log("----------------------------- setTopicThumbnail -----------------------------------");
console.log(hookData);
await setTopicThumbnail(hookData.topic.tid, hookData.data.content);
return hookData;
};
async function setTopicThumbnail(tid, content) {
const imageUrl = extractImageUrl(content);
if (!imageUrl) {
//await console.log("Image URL Not found in markdown content");
return;
}
if (!imageUrl.startsWith('http')) {
return;
}
try {
const imagePath = await downloadFile(imageUrl, tid);
await topics.setThumbnail(tid, imagePath);
} catch (err) {
console.error('Error cound not download image:', err);
}
};
async function downloadFile(url, tid) {
try {
const response = await axios({
method: 'get',
url: url,
responseType: 'stream'
});
const ext = path.extname(url);
const fileName = `${tid}${ext}`;
const filePath = path.join(IMAGE_DIR, fileName);
return new Promise((resolve, reject) => {
const writer = fs.createWriteStream(filePath);
response.data.pipe(writer);
writer.on('finish', () => resolve(filePath));
writer.on('error', reject);
});
} catch (err) {
throw new Error('이미지 다운로드 실패: ' + err.message);
}
};
function extractImageUrl(content) {
const regex = /!\[.*?\]\((.*?)\)/;
const match = content.match(regex);
return match ? match[1] : null;
};
plugin.json
{
"id": "nodebb-plugin-autothumb",
"name": "Auto Image Thumbnail Plugin",
"version": "0.1.0",
"description": "Automatically sets the first image in a post as the topic thumbnail.",
"library": "./library.js",
"license": "MIT",
"hooks": [
{ "hook": "static:app.load", "method": "init" },
{ "hook": "filter:topic.create", "method": "onTopicCreate" },
{ "hook": "filter:topic.edit", "method": "onTopicEdit" }
],
"dependencies": {
"axios": "^0.21.1"
}
}
@baris WoW! Shazam! Thanks!
@baris How can I preview the github link? Is there any plugins involved?