-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepare.ts
99 lines (81 loc) · 2.86 KB
/
prepare.ts
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
/*
* @Author: lyyyd David.Jackson.Lyd@gmail.com
* @Date: 2023-05-05 21:24:55
* @LastEditors: lyyyd David.Jackson.Lyd@gmail.com
* @LastEditTime: 2023-05-14 19:14:05
* @FilePath: \nestleify\src\prepare.ts
* @Description:
*
* Copyright (c) 2023 by ${git_name_email}, All Rights Reserved.
*/
import path from 'path'
import { Context, FileInfo, ImageRef } from './types'
import fs from 'fs'
import { PicGo } from 'picgo'
const picgo = new PicGo()
export default async (ctx: Context): Promise<void> => {
// console.log('ctx', ctx);
/**
* fileInfoList的数据结构为
*
* example:
*
* [{
* path: '',
* info: [{
* ref: '',
* imgFilePath: ''
* }, {
* ref: '',
* imgFilePath: ''
* }]
* }]
*
*/
const readFileStream = (path: string): any => {
const data = fs.readFileSync(path, 'utf-8');
return data.toString();
}
const fileInfoList: FileInfo[] = new Array;
const getMdFiles = async () => {
// Gets a list of md files
await ctx.files.forEach(async (filePath) => {
// const fileMap: Map<string, Array<Map<string, string>>> = new Map();
// 文件所在目录
const dirname = path.dirname(filePath);
// 文件内容
const content = readFileStream(filePath);
// 图片引用列表
const imgRefArr: Array<string> = content.match(/\!\[.*\]\(.*\)/g) || [];
const imgRefList: ImageRef[] = new Array;
imgRefArr.forEach(async (imageRef) => {
let imageRefMap: Map<string, ImageRef> = new Map()
// 图片引用的相对路径
/**
* TODO: 网络图片进行阿里云上传
*/
const relativePath = path.normalize((decodeURI(imageRef).match(/[\u4E00-\u9FA5\w_\s\-]+[\\|\/]+[\u4E00-\u9FA5\w_\s\-]+\.+(jpg|png|JPG|PNG|jpeg|JPEG|gif|GIF)/g))![0]);
const fileFullPath = path.join(dirname, relativePath);
// const map: Map<string, string> = new Map()
// map.set('ref', imageRef)
// aaa.set('fileFullPath', fileFullPath)
imgRefList.push({
ref: imageRef,
imgFilePath: fileFullPath,
baseDir: path.dirname(fileFullPath),
imgName: path.parse(fileFullPath).name,
})
});
// console.log('1.imgRefList***', imgRefList)
fileInfoList.push(
{
path: filePath,
info: imgRefList
}
)
})
}
await getMdFiles()
// console.log('2.fileInfoList***', fileInfoList)
ctx.fileInfoList = fileInfoList
}