-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
34 lines (29 loc) · 867 Bytes
/
utils.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
const fetch = require("node-fetch");
/**
* 获取 markdown 中的 image 标签
* @param {string} input
*/
function parseMarkdownImagesUrls(input, filter) {
const reg = /\!\[.*\]\((.*)\)/g;
const matchs = [...input.matchAll(reg)];
let urls = matchs.map((m) => m[1]).filter((url) => url.startsWith("http")); // 过滤掉 base64 和 相对路径
if (filter) {
urls = urls.filter((url) => filter.test(url));
}
return urls;
}
/**
* 图片下载转base64
* @param {string} url
* @param {number} quality // 图片压缩比
*/
async function imageUrlConvertToBase64(url,quality) {
const response = await fetch(url);
const result = await response.arrayBuffer();
const base64 = Buffer.from(result).toString("base64");
return `data:image/jpeg;base64,${base64}`;
}
module.exports = {
parseMarkdownImagesUrls,
imageUrlConvertToBase64,
};