-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
67 lines (61 loc) · 2.72 KB
/
index.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
const PATH = require('path');
const FS = require('fs');
const write = require('write');
const markdownImageReferencesRE = /(!\[[^\]]*\]\((?!(?:https?:)?\/\/)[^)]+\))/g
const imagePathRE = /^(!\[[^\]]*\]\()((?!(?:https?:)?\/\/)[^)]+)(\))$/
const codeRE = /^(([ \t]*`{3,4})([^\n]*)([\s\S]+?)(^[ \t]*\2))/gm
const imgTagReferencesRE = /<img.*?src="(.*?)"/g
const imgTagRE = /<img.*?src="(.*?)"/
// [link text](/path/to/img.jpg "Optional title")
// 1 2 3 <--- captures
// This will capture up to the last paren in the block. We then pull
// var m = text.match(/^\s*\([ \t]*([^"']*)(?:[ \t]+(["'])(.*?)\2)?[ \t]*\)/);
module.exports = class CopyMarkdownImageWebpackPlugin {
constructor(options) {
this.option = options || { dir: [], toDir: '' };
}
apply(compiler) {
compiler.hooks.afterCompile.tapAsync(this.constructor.name, (compilation, callback) => {
compilation.fileDependencies.forEach((filePath) => {
if (/\.(md|markdown)$/.test(filePath) && this.option.toDir) {
const isIndexOf = this.option.dir.some(item => filePath.indexOf(item) > -1)
if (this.option.dir.some(item => filePath.indexOf(item) > -1)) {
if (!FS.existsSync(filePath)) return;
let content = FS.readFileSync(filePath).toString();
// 替换代码高亮部分
content = content.replace(codeRE, '');
let imgPaths = content.match(markdownImageReferencesRE);
if (imgPaths && imgPaths.length > 0) {
// ![MDN web docs](./guides/canvas.jpg)
// ![MDN web docs](../assets/canvas.jpg)
imgPaths.forEach((imgPath) => {
let [, mdImageStart, mdImagePath, mdImageEnd] = imagePathRE.exec(imgPath) || [];
this.copyImageFile(mdImagePath);
})
}
let imgTagPaths = content.match(imgTagReferencesRE);
if (imgTagPaths && imgTagPaths.length > 0) {
// <img alt="node" width="18" height="18" src="./assets/node-logo.svg"/>
imgTagPaths.forEach((imgPath) => {
let [, mdImagePath] = imgTagRE.exec(imgPath) || [];
this.copyImageFile(mdImagePath);
})
}
}
}
})
callback()
});
}
copyImageFile(mdImagePath) {
if (!mdImagePath) return;
mdImagePath = mdImagePath.replace(/^\.\.\//, '').replace(/^\.\//, '');
const rootPath = this.option.dir.filter((fileRootPath) => {
// 图片存在
if (FS.existsSync(PATH.join(fileRootPath, mdImagePath))) {
const imageData = FS.readFileSync(PATH.join(fileRootPath, mdImagePath))
write.sync(PATH.join(this.option.toDir, mdImagePath), imageData)
}
})
}
}