-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkdown_processor.js
50 lines (40 loc) · 1.41 KB
/
markdown_processor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const path = require('path');
const fs = require('fs');
const smmsClient = require('./smms_client');
function isLocalFile(filePath) {
return !filePath.startsWith('http') && !filePath.startsWith('https');
}
async function processImages(mdContent, config) {
const matches = mdContent.matchAll(config.local.imagePattern);
const imageUrls = [];
for (const match of matches) {
const [fullMatch, imagePath] = match;
if (isLocalFile(imagePath)) {
const absolutePath = path.resolve(path.dirname(config.local.mdFile), imagePath);
if (fs.existsSync(absolutePath)) {
const newUrl = await smmsClient.uploadImage(absolutePath, config.imageHost.headers);
if (newUrl) {
imageUrls.push({
original: fullMatch,
path: imagePath,
newUrl: newUrl
});
}
} else {
console.warn(`Warning: Image file not found: ${absolutePath}`);
}
}
}
return imageUrls;
}
async function updateMarkdown(mdContent, imageUrls) {
let newContent = mdContent;
imageUrls.forEach(({original, path, newUrl}) => {
newContent = newContent.replace(original, original.replace(path, newUrl));
});
return newContent;
}
module.exports = {
processImages,
updateMarkdown
};