-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
59 lines (53 loc) · 1.91 KB
/
main.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
51
52
53
54
55
56
57
58
59
const fs = require('fs');
const markdownProcessor = require('./markdown_processor');
const smmsClient = require('./smms_client');
const userConfig = require('./config');
const config = {
imageHost: {
uploadUrl: 'https://sm.ms/api/v2/upload',
headers: {
'Authorization': '',
'Content-Type': 'multipart/form-data',
'Accept': 'application/json'
}
},
local: {
mdFile: userConfig.markdown.filePath,
imagePattern: /!\[.*?\]\((.*?)\)/g,
}
};
async function initializeConfig() {
try {
config.imageHost.headers.Authorization = await smmsClient.getAPIToken();
return true;
} catch (error) {
console.error('Failed to initialize config:', error);
return false;
}
}
async function processMarkdownImages() {
try {
console.log('Starting markdown image processing...');
const initialized = await initializeConfig();
if (!initialized) {
throw new Error('Failed to initialize configuration');
}
const mdContent = fs.readFileSync(config.local.mdFile, 'utf8');
const imageUrls = await markdownProcessor.processImages(mdContent, config);
console.log(`Processed ${imageUrls.length} images`);
const newContent = await markdownProcessor.updateMarkdown(mdContent, imageUrls);
const newFilePath = config.local.mdFile.replace('.md', '_uploaded.md');
fs.writeFileSync(newFilePath, newContent);
console.log(`Process completed. New file saved as: ${newFilePath}`);
return true;
} catch (error) {
console.error('Error in main process:', error);
return false;
}
}
async function main() {
console.log('Starting image upload process...');
const success = await processMarkdownImages();
console.log(success ? 'Process finished successfully.' : 'Process finished with errors.');
}
main();