Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
codeskyblue opened this issue Apr 19, 2023 · 4 comments
Open

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

codeskyblue opened this issue Apr 19, 2023 · 4 comments

Comments

@codeskyblue
Copy link

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

@x-cold
Copy link
Owner

x-cold commented Apr 19, 2023

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

@codeskyblue
Copy link
Author

我实现了一个非常简单的,勉强能用的版本。仅供参考(不好意思提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
Copy link

JasonMa0012 commented May 30, 2023

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

@ZY945
Copy link

ZY945 commented Dec 25, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants