x-cold / yuque-hexo

同步语雀的文章到你的 Hexo 项目吧!
http://blog.lxstart.net/
MIT License
711 stars 83 forks source link

增加下载文件到 source/images 目录的支持 #140

Open codeskyblue opened 1 year ago

codeskyblue commented 1 year ago

现在还需要配置图床,感觉很麻烦。所以在想是否可以直接将文件直接下载到本地。 我可以提pr,如果需要的话。

x-cold commented 1 year ago

可以的,看能不能在现有的图床方案扩展

codeskyblue commented 1 year ago

我实现了一个非常简单的,勉强能用的版本。仅供参考(不好意思提pr,感觉我js写的不太好)

diff --git a/node_modules/yuque-hexo/adapter/hexo.js b/node_modules/yuque-hexo/adapter/hexo.js
index b42469a..9422c21 100644
--- a/node_modules/yuque-hexo/adapter/hexo.js
+++ b/node_modules/yuque-hexo/adapter/hexo.js
@@ -35,7 +35,7 @@ function parseMatter(body) {
   body = entities.decode(body);
   try {
     // front matter信息的<br/>换成 \n
-    const regex = /(title:|layout:|tags:|date:|categories:){1}(\S|\s)+?---/gi;
+    const regex = /(title:|layout:|tags:|date:|categories:|toc:){1}(\S|\s)+?---/gi;
     body = body.replace(regex, a =>
       a.replace(/(<br \/>|<br>|<br\/>)/gi, '\n')
     );
@@ -76,6 +76,7 @@ module.exports = async function(post) {
   const date = data.date || formatDate(created_at);
   const tags = data.tags || [];
   const categories = data.categories || [];
+  const toc = data.toc || true;
   const props = {
     title: title.replace(/"/g, ''), // 临时去掉标题中的引号,至少保证文章页面是正常可访问的
     urlname,
@@ -83,6 +84,7 @@ module.exports = async function(post) {
     ...data,
     tags,
     categories,
+    toc,
   };
   const text = ejs.render(template, {
     raw,
diff --git a/node_modules/yuque-hexo/util/imageBeds/index.js b/node_modules/yuque-hexo/util/imageBeds/index.js
index 4af4909..9cac9a3 100644
--- a/node_modules/yuque-hexo/util/imageBeds/index.js
+++ b/node_modules/yuque-hexo/util/imageBeds/index.js
@@ -5,10 +5,11 @@ const OssClient = require('./oss');
 const QiniuClient = require('./qiniu');
 const UPClient = require('./upyun');
 const GithubClient = require('./github');
+const LocalClient = require("./local");
 const out = require('../../lib/out');

 // 目前已适配图床列表
-const imageBedList = [ 'qiniu', 'cos', 'oss', 'upyun', 'github' ];
+const imageBedList = [ 'qiniu', 'cos', 'oss', 'upyun', 'github', 'local' ];

 class ImageBeds {
   constructor(config) {
@@ -45,6 +46,8 @@ class ImageBeds {
         return UPClient.getInstance(this.config);
       case 'github':
         return GithubClient.getInstance(this.config);
+      case 'local':
+        return LocalClient.getInstance(this.config);
       default:
         return QiniuClient.getInstance(this.config);
     }
diff --git a/node_modules/yuque-hexo/util/imageBeds/local.js b/node_modules/yuque-hexo/util/imageBeds/local.js
new file mode 100644
index 0000000..9224ee4
--- /dev/null
+++ b/node_modules/yuque-hexo/util/imageBeds/local.js
@@ -0,0 +1,45 @@
+'use strict';
+
+const out = require('../../lib/out');
+const fs = require("fs");
+
+class LocalClient {
+    constructor(config) {
+        this.config = config;
+    }
+
+    static getInstance(config) {
+        if (!this.instance) {
+            this.instance = new LocalClient(config);
+        }
+        return this.instance;
+    }
+
+    async hasImage(fileName) {
+        try {
+            const cwd = process.cwd();
+            const imgDir = `${cwd}/source/images`
+            const imgUrl = `/images/${fileName}`
+            if (fs.existsSync(`${imgDir}/${fileName}`)) {
+                return imgUrl
+            }
+        } catch(e) {
+            return '';
+        }
+    }
+
+    async uploadImg(imgBuffer, fileName) {
+        const imgDir = `${process.cwd()}/source/images`
+        const imgPath = `${imgDir}/${fileName}`
+        const imgUrl = `/images/${fileName}`
+
+        if (!fs.existsSync(imgDir)) {
+            fs.mkdirSync(imgDir)
+        }
+        
+        fs.writeFileSync(imgPath, imgBuffer)
+        return imgUrl
+    }
+}
+
+module.exports = LocalClient;
JasonMa0012 commented 1 year ago

+10086, 太需要这个功能了, 上传github图床这个操作其实是多此一举, 只会遇到一堆网络错误

ZY945 commented 6 months ago

有相关进度吗,真的很需要这个,感谢大佬们