全局替换图片地址的脚本,运行时执行;
const path = require('path');
const express = require('express');
const axios = require('axios');
const fs = require('fs');
const bodyParser = require('body-parser');//解析,用req.body获取post参数
const app = express();
app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));
const createPngFile = async (url, filePath) => {
const writer = fs.createWriteStream(filePath);
try {
await axios(
{
url,
responseType: "stream",
}).then(response => {
response.data.pipe(writer)
return new Promise((resolve, reject) => {
writer.on("finish", resolve)
writer.on("error", reject)
})
}).catch((error)=> {
console.log('--------error',error);
})
} catch (err) {
throw err;
}
}
// 获取与id对应的md文档路径并替换
const getMdPath = (mdList, mdId, url, imageName, docDirPath) => {
mdList.forEach(({children, id, content}) => {
if (children && children.length) {
getMdPath(children, mdId, url, imageName, docDirPath)
} else {
if (mdId === id) {
// TODO: 读取md文件
// 去掉content中的点
const _docPath = content.replace(/^./,'');
const docPath = `${docDirPath}${_docPath}`;
const data = fs.readFileSync(docPath,'utf-8');
const picUrlReg = RegExp("!\\[\(.*?\)\\]\\(" + url + "\\)","g");
const repContent = data.replace(picUrlReg, ``);
fs.writeFileSync(docPath, repContent, {encoding: 'utf8'})
}
}
})
}
const testGET = async (mdId, imgList, imagePath, docDirPath) => {
const data = fs.readFileSync(`${docDirPath}/index.ts`);
// TODO:解析md菜单
const _data = data.toString();
const _strsListStr = _data.split("=")[3].replace(/require\(|\)|\).default|.default|;/g,'');
const func = new Function('return (' + _strsListStr + ')');
const mdList = func();
fs.readdir(imagePath, (err, files) => {
if(err) throw err;
const imgDirName = `${imagePath}/${mdId}`;
const dirIsExist = files.some(file => mdId == file);
if (!dirIsExist) {
fs.mkdir(imgDirName, (err) => {
if (!err) {
imgList.forEach(({url, imageName}, i) => {
let _imageName = imageName;
if (_imageName === 'image.png') {
_imageName = `image${i}.png`;
}
createPngFile(url,`${imagePath}/${mdId}/${_imageName}`);
getMdPath(mdList, mdId, url, _imageName, docDirPath)
})
} else {
throw err;
}
})
} else {
fs.readdir(imgDirName, (err, imgFiles) => {
if(err) throw err;
imgList.forEach(({url, imageName}, i) => {
if (imgFiles.length) {
(imgFiles || []).forEach(item => {
if (item && imageName === item) {
// TODO: 先删文件再创建
fs.unlinkSync(`${imagePath}/${mdId}/${item}`);
createPngFile(url,`${imagePath}/${mdId}/${item}`);
getMdPath(mdList, mdId, url, item, docDirPath);
} else {
let _imageName = imageName;
if (_imageName === 'image.png') {
_imageName = `image${i}.png`;
}
createPngFile(url,`${imagePath}/${mdId}/${_imageName}`);
getMdPath(mdList, mdId, url, _imageName, docDirPath);
}
})
} else {
let _imageName = imageName;
if (_imageName === 'image.png') {
_imageName = `image${i}.png`;
}
createPngFile(url,`${imagePath}/${mdId}/${imageName}`);
getMdPath(mdList, mdId, url, imageName, docDirPath);
}
})
})
}
})
}
app.post('/pic',function(req,res){
const data = req.body;
const { id, imgList, imagePath, docDirPath } = data;
testGET(id, imgList, imagePath, docDirPath).then(() => {
console.log('0000000000000','写入成功了吗?')
})
const responseObject = {//也可以是数组 数组也会转化为json
success: true,
code: 200
}
res.json(responseObject)
});
app.listen("8888", 'localhost', function(err) {
if(err){
console.log(err);
return;
}
console.log('Listening at http://localhost:8888');
});