Browse Source

Fix log file can't be wrote.
Fix Highlight parse Html bug
Fix Image base file

zhuzhuyule 7 years ago
parent
commit
f10c35b6b3

+ 1 - 1
app/index.js

@@ -47,7 +47,7 @@ app.on('open-file', (e, file) => {
 });
 
 app.on('activate', () => {
-    if (moeApp.windows.length == 0) {
+    if (moeApp != null && moeApp.windows && moeApp.windows.length == 0) {
         moeApp.open();
     }
 });

+ 7 - 3
app/moe-app.js

@@ -39,12 +39,14 @@ class MoeditorApplication {
     constructor() {
         this.windows = new Array();
         this.hexoWindow = null;
+
+        global.storePath = path.join(require('os').homedir(),'Documents','HexoEditor');
         log4js.configure({
             appenders: {
                 out: {type: 'stdout', level: 'all'},//设置是否在控制台打印日志
-                debugs: {type: 'file', filename: './logs/debug.log', level: 'debug',maxLogSize: 1024*1024*5},
-                infos: {type: 'file', filename: './logs/log.log', level: 'debug',maxLogSize: 1024*1024*5},
-                errors: { type: 'file', filename: './logs/error.log' ,maxLogSize: 1024*1024*2},
+                debugs: {type: 'file', filename: path.join(storePath,'logs/debug.log'), level: 'debug',maxLogSize: 1024*1024*5},
+                infos: {type: 'file', filename: path.join(storePath,'logs/log.log'), level: 'debug',maxLogSize: 1024*1024*5},
+                errors: { type: 'file', filename: path.join(storePath,'logs/error.log') ,maxLogSize: 1024*1024*2},
                 debug: { type: 'logLevelFilter', appender: 'debugs', level: 'debug' },
                 info: { type: 'logLevelFilter', appender: 'infos', level: 'info' },
                 error: { type: 'logLevelFilter', appender: 'errors', level: 'error' }
@@ -92,6 +94,8 @@ class MoeditorApplication {
         this.locale = new MoeditorLocale();
         global.__ = str => this.locale.get(str);
         this.flag = new Object();
+        moeApp.storePath = storePath;
+
 
         const a = process.argv;
         if (a[0].endsWith('electron') && a[1] == '.') a.shift(), a.shift();

+ 2 - 0
app/tool/hexo-uploadServer.js

@@ -101,6 +101,8 @@ module.exports = (function () {
                     } else {
                         asyncUploadToSmms(imgPath, callback);
                     }
+                } else {
+                    asyncUploadToSmms(imgPath, callback);
                 }
             }
         }

+ 43 - 26
views/main/hexo-image.js

@@ -35,38 +35,54 @@ class ImgManager {
         } else {
             rootPaht = hexoWindow.directory;
         }
-        rootPaht = rootPaht.replace(/\\/g, '/');
-        this.imgPathDir = path.join(rootPaht, this.postName);
-        if (this.imgBaseDir && this.imgBaseDir !== rootPaht) {
-            const oldPath = path.join(this.imgBaseDir, this.postName);
-            if (fs.existsSync(oldPath)) {
-                try {
-                    fs.renameSync(oldPath, this.imgPathDir);
-                } catch (e) {
-                    fs.renameSync(oldPath, this.imgPathDir);
+        fs.access(rootPaht, fs.constants.R_OK | fs.constants.W_OK, (err) => {
+            if(err){
+                log.warn(`setting [${rootPaht}] failed.` + err)
+                rootPaht = path.join(moeApp.storePath,'images');
+            }   else {
+                log.info(`setting [${rootPaht}] success.`)
+            }
+            rootPaht = rootPaht.replace(/\\/g, '/');
+            this.imgPathDir = path.join(rootPaht, this.postName);
+            if (this.imgBaseDir && this.imgBaseDir !== rootPaht) {
+                const oldPath = path.join(this.imgBaseDir, this.postName);
+                if (fs.existsSync(oldPath)) {
+                    try {
+                        fs.renameSync(oldPath, this.imgPathDir);
+                    } catch (e) {
+                        fs.renameSync(oldPath, this.imgPathDir);
+                    }
+                    this.updateDictionary(this.imgBaseDir, rootPaht)
                 }
-                this.updateDictionary(this.imgBaseDir, rootPaht)
             }
-        }
-        this.imgBaseDir = rootPaht;
+            this.imgBaseDir = rootPaht;
+        });
     }
 
     mkdirsSync(dirpath, mode) {
-        if (!fs.existsSync(dirpath)) {
-            var pathtmp;
-            dirpath.split(path.sep).forEach(function (dirname) {
-                if (pathtmp) {
-                    pathtmp = path.join(pathtmp, dirname);
-                }
-                else {
-                    pathtmp = dirname;
-                }
-                if (!fs.existsSync(pathtmp)) {
-                    if (!fs.mkdirSync(pathtmp, mode)) {
-                        return false;
+        try{
+            if (!fs.existsSync(dirpath)) {
+                var pathtmp;
+                dirpath.split(path.sep).forEach(function (dirname) {
+                    if (pathtmp) {
+                        pathtmp = path.join(pathtmp, dirname);
                     }
-                }
-            });
+                    else {
+                        pathtmp = dirname;
+                    }
+
+                    if (fs.existsSync(pathtmp)) {
+                        fs.accessSync(pathtmp, fs.constants.R_OK | fs.constants.W_OK);
+                    }else
+                        if (!fs.mkdirSync(pathtmp, mode)) {
+                            return false;
+                    }
+                });
+            }
+            log.info(`setting [${dirpath}] success.`)
+        } catch (e) {
+            log.warn(`setting [${dirpath}] failed.` + e)
+            return false;
         }
         return true;
     }
@@ -136,6 +152,7 @@ class ImgManager {
                     }
                 }
             } catch (e) {
+                log.error(e);
             } finally {
                 return relativePath
             }

+ 0 - 39
views/main/hexo/plugins/filter/after_post_render/external_link.js

@@ -1,39 +0,0 @@
-'use strict';
-
-var url = require('url');
-var cheerio;
-
-function externalLinkFilter(data) {
-  var config = this.config;
-  if (!config.external_link) return;
-
-  if (!cheerio) cheerio = require('cheerio');
-
-  var $ = cheerio.load(data.content, {decodeEntities: false});
-  var siteHost = url.parse(config.url).hostname || config.url;
-
-  $('a').each(function() {
-    // Exit if the link has target attribute
-    if ($(this).attr('target')) return;
-
-    // Exit if the href attribute doesn't exists
-    var href = $(this).attr('href');
-    if (!href) return;
-
-    var data = url.parse(href);
-
-    // Exit if the link doesn't have protocol, which means it's a internal link
-    if (!data.protocol) return;
-
-    // Exit if the url has same host with config.url
-    if (data.hostname === siteHost) return;
-
-    $(this)
-      .attr('target', '_blank')
-      .attr('rel', 'external');
-  });
-
-  data.content = $.html();
-}
-
-module.exports = externalLinkFilter;

+ 0 - 1
views/main/hexo/plugins/filter/after_post_render/index.js

@@ -3,6 +3,5 @@
 module.exports = function(ctx) {
   var filter = ctx.extend.filter;
 
-  filter.register('after_post_render', require('./external_link'));
   filter.register('after_post_render', require('./excerpt'));
 };

+ 1 - 1
views/main/hexo/plugins/filter/before_post_render/highlight/backtick_code_block.js

@@ -63,7 +63,7 @@ function backtickCodeBlock(data) {
             .replace(/{/g, '{')
             .replace(/}/g, '}');
 
-        return start + '<escape>' + content + '</escape>' + (end ? '\n\n' : '');
+        return start  + content + (end ? '\n\n' : '');
     });
 }
 

+ 2 - 3
views/main/hexo/plugins/filter/before_post_render/highlightEx/backtick_code_block.js

@@ -160,10 +160,9 @@ function backtickCodeBlock(data) {
             autoDetect: config.auto_detect
         })
             .replace(/{/g, '&#123;')
-            .replace(/}/g, '&#125;');;
+            .replace(/}/g, '&#125;');
 
-
-        return start + '<escape>' + content + '</escape>' + (end ? '\n\n' : '');
+        return start + content + (end ? '\n\n' : '');
     });
 }
 

+ 4 - 0
views/main/hexo/previewer.js

@@ -99,6 +99,10 @@ Previewer.prototype.render = function (content, MoeMark, options, callback) {
                     //最后查询文档所在目录
                     if (!srcLocal)
                         srcLocal = path.join(hexoWindow.directory, src);
+                    //最后查询文档所在目录
+                    if (!srcLocal)
+                        srcLocal = path.join(moeApp.storePath,'images');
+
                     img.id = src;
                     img.setAttribute('localImg', true);
                     src = url.resolve('file://', srcLocal);

+ 26 - 9
views/settings/moe-settings.js

@@ -473,18 +473,35 @@ document.addEventListener('DOMContentLoaded', () => {
     imageSourceButton.addEventListener('click', () => {
         dialog.showOpenDialog(window.hexoWindow, {
             properties: ['openDirectory'],
-            filters: [
-                {name: __('All Files'), extensions: ['yml']},
-                {name: __('All Files'), extensions: ['*']}
-            ]
         }, (fileName) => {
             if (!fileName || fileName.length === 0) return;
             fileName = fileName[0];
-            if (imageSourceInput.value !== fileName) {
-                moeApp.config.set(imageSourceInput.id, fileName);
-                ipcRenderer.send('setting-changed', {key: imageSourceInput.id, val: fileName});
-                imageSourceInput.value = fileName;
-            }
+
+            fs.access(fileName, fs.constants.R_OK | fs.constants.W_OK, (err) => {
+                     if(err){
+                         log.error(`setting [${fileName}] failed.`+err)
+                         dialog.showMessageBox(
+                             window.hexoWindow,
+                             {
+                                 type: 'warning',
+                                 title: __('Waring'),
+                                 message: __("WaringNoFile"),
+                                 detail: 'Directory prohibit reading or writing!'
+                             },
+                             function (res) {
+                             }
+                         )
+                     } else{
+                         log.info(`setting [${fileName}] success.`)
+                         if (imageSourceInput.value !== fileName) {
+                             moeApp.config.set(imageSourceInput.id, fileName);
+                             ipcRenderer.send('setting-changed', {key: imageSourceInput.id, val: fileName});
+                             imageSourceInput.value = fileName;
+                         }
+                     }
+
+            });
+
         });
     });
 

+ 1 - 0
views/settings/settings.html

@@ -30,6 +30,7 @@
         window.w = require('electron').remote.getCurrentWindow();
         const fs = require('fs');
         const path = require('path');
+        const log = moeApp.getLog4js().getLogger('Settings');
         </script>
 
         <link rel="stylesheet" href="moe-scrollbar.css">