-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathreplace-image.js
233 lines (201 loc) · 6.79 KB
/
replace-image.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
const fs = require('fs')
const path = require('path')
const request = require('request')
const playwright = require('playwright') // "playwright": "^1.32.3"
const chromium = playwright.chromium
const owner = 'levy9527'
const repo = 'image-holder'
const githubPrefix = `https://raw.githubusercontent.com/${owner}/${repo}/main/`
const yuquePrefix = 'https://cdn.nlark.com'
const imageDir = 'download-images'
const imageSuffix = 'png'
const accessToken = process.env.GITHUB_TOKEN
if (!accessToken) {
console.error('GITHUB_TOKEN is undefined')
process.exit(1)
}
let pathToMarkdownFile
if (process.argv.length < 3) {
console.error('Markdown file is not specified')
process.exit(1)
}
pathToMarkdownFile = process.argv[2]
if (!fs.existsSync(pathToMarkdownFile)) {
console.error('Markdown file is not exist')
process.exit(1)
}
if (!fs.existsSync(imageDir)) {
fs.mkdirSync(imageDir);
}
const args = require('minimist')(process.argv.slice(2))
console.log('Processing ', pathToMarkdownFile)
replaceLocalImagesInMarkdown()
replaceYuqueImagesInMarkdown()
/**
upload download-images/img*.png
get its github url
find local images by startWiths(../)
replace
remove local images
// this function can be combined into another function
// the difference lies in checking url counts equality and use another name to upload
// after upload, remove file
*/
async function replaceLocalImagesInMarkdown() {
let markdownContent = fs.readFileSync(pathToMarkdownFile).toString()
let imageUrls = extractImageUrls(markdownContent, '../')
if (!imageUrls.length) {
console.log('No local image need to be replaced!')
return
}
const directoryPath = path.join(__dirname, imageDir);
const localImages = fs.readdirSync(directoryPath)
if (imageUrls.length !== localImages.length) {
console.error('Markdown images count is not equal to local images count', imageUrls.length, localImages.length)
process.exit(1)
}
for (let i = 0; i < localImages.length; i++) {
const imageUrl = imageUrls[i]
const imagePath = imageDir + '/' + localImages[i]
let retry = true
while (retry) {
try {
githubImageUrl = await uploadImage(imagePath, pathToMarkdownFile)
retry = false
} catch(e) {
console.log(e, '\nRetry uploading')
}
}
githubImageUrl = githubImageUrl.replace('githubusercontent', 'gitmirror')
markdownContent = markdownContent.replace(imageUrl, githubImageUrl)
console.log('Rewriting md file...\n')
fs.writeFileSync(pathToMarkdownFile, markdownContent)
}
// TODO delete images
console.log('Replacing local images is done!')
}
async function replaceYuqueImagesInMarkdown(isLocal) {
let markdownContent = fs.readFileSync(pathToMarkdownFile).toString()
const imageUrls = extractImageUrls(markdownContent, yuquePrefix)
const directoryPath = path.join(__dirname, imageDir);
const localImages = fs.readdirSync(directoryPath)
for (let i = 0; i < imageUrls.length; i++) {
const imageUrl = imageUrls[i]
if (imageUrl.startsWith(yuquePrefix)) {
const imagePath = imageDir + '/' + new Date().getTime() + '.' + imageSuffix
try {
let githubImageUrl = ''
if (!isLocal) {
console.log('Downloading image: ', imageUrl)
await downloadImage(imageUrl, imagePath)
let retry = true
while (retry) {
try {
githubImageUrl = await uploadImage(imagePath, pathToMarkdownFile)
retry = false
} catch(e) {
console.log(e, '\nRetry uploading')
}
}
}
else {
console.log('Using local image...')
if (localImages[i]) {
githubImageUrl = githubPrefix + getDirWithForwardSlash(pathToMarkdownFile) + getFileName(localImages[i])
}
else {
break;
}
}
// use proxy address
githubImageUrl = githubImageUrl.replace('githubusercontent', 'gitmirror')
markdownContent = markdownContent.replace(imageUrl, githubImageUrl)
// save ASAP, in case of github api connecting timeout
console.log('Rewriting md file...\n')
fs.writeFileSync(pathToMarkdownFile, markdownContent)
} catch(e) {
console.error(e)
process.exit(1)
}
}
}
// TODO remove all local images
console.log('Replacing all images is done!')
}
async function downloadImage(imageUrl, imagePath) {
const browser = await chromium.launch({headless: !args['headed']});
const context = await browser.newContext();
const page = await context.newPage();
const downloadPromise = page.waitForEvent('download');
try {
await page.goto(imageUrl);
} catch(e) {
// yuque 特殊情况处理:load 事件不会触发,只会直接下载图片
}
const download = await downloadPromise;
console.log('downloaded', await download.path());
await download.saveAs(imagePath);
// Teardown
await context.close();
await browser.close();
}
async function uploadImage(imagePath, pathToMarkdownFile) {
const content = fs.readFileSync(imagePath)
const encodedContent = content.toString('base64')
const distPath = getDirWithForwardSlash(pathToMarkdownFile) + getFileName(imagePath)
const url = `https://api.github.com/repos/${owner}/${repo}/contents/${distPath}`
const headers = {
Authorization: `token ${accessToken}`,
Accept: 'application/vnd.github.v3+json',
'User-Agent': 'request',
}
const data = {
message: 'Add image',
content: encodedContent,
}
console.log(`Uploading image: ${imagePath}`)
return new Promise((resolve, reject) => {
request.put(
{
url,
headers,
body: JSON.stringify(data),
},
(error, response) => {
if (error) {
reject(error)
} else {
//console.log(JSON.parse(response.body))
const githubImageUrl = JSON.parse(response.body).content.download_url
console.log(`Uploaded image ${imagePath} to ${githubImageUrl}`)
resolve(githubImageUrl)
}
}
)
})
}
function extractImageUrls (markdownContent, imagePrefix) {
const imageMarks = markdownContent.match(/!\[.*\]\((.+)\)/g)
if (!imageMarks) {
console.log('No image needs to be replaced!')
process.exit(0)
}
return imageMarks.map(mark => {
// mark format: ![](url)
const array = mark.split('(')
const imgFullUrl = array[1].substring(0, array[1].length - 1)
return imgFullUrl
//return imgFullUrl.split('#')[0]
}).filter(v => v.startsWith(imagePrefix))
}
function getDirWithForwardSlash(path) {
if (!path) return ''
let lastSlashIndex = path.lastIndexOf('/');
//substring exclusive end
return path.substring(0, lastSlashIndex + 1);
}
function getFileName(path) {
if (!path) return ''
let lastSlashIndex = path.lastIndexOf('/');
return path.substring(lastSlashIndex + 1);
}