Преглед изворни кода

BugFix determining lsxPrefix flow

Yuki Takei пре 9 година
родитељ
комит
64ee554b79

+ 20 - 12
packages/growi-plugin-lsx/src/lib/util/Lsx.js

@@ -13,19 +13,27 @@ class Lsx {
 
 
     // TODO try-catch
     // TODO try-catch
 
 
-    // create {Key: Value} Objects
-    const splittedArgs = args.split(',');
+    // initialize
+    let lsxPrefix = args || currentPath;
     let lsxOptions = {};
     let lsxOptions = {};
-    splittedArgs.forEach((arg) => {
-      // see https://regex101.com/r/pYHcOM/1
-      const match = arg.match(/([^=]+)=?(.+)?/);
-      const value = match[2] || true;
-      lsxOptions[match[1]] = value;
-    });
-
-    // determine prefix
-    // 'prefix=foo' pattern or the first argument
-    const lsxPrefix = lsxOptions.prefix || splittedArgs[0];
+
+    // if args is a String like 'key1=value1, key2=value2, ...'
+    const splittedArgs = args.split(',');
+    if (splittedArgs.length > 1) {
+      splittedArgs.forEach((arg) => {
+        arg = arg.trim();
+
+        // see https://regex101.com/r/pYHcOM/1
+        const match = arg.match(/([^=]+)=?(.+)?/);
+        const value = match[2] || true;
+        lsxOptions[match[1]] = value;
+      });
+
+      // determine prefix
+      // 'prefix=foo' pattern or the first argument
+      lsxPrefix = lsxOptions.prefix || splittedArgs[0];
+    }
+
     // resolve path
     // resolve path
     const pagePath = path.resolve(currentPath, lsxPrefix);
     const pagePath = path.resolve(currentPath, lsxPrefix);
     const queryOptions = {}
     const queryOptions = {}

+ 16 - 13
packages/growi-plugin-lsx/src/resource/js/util/PreProcessor/LsxPreProcessor.js

@@ -33,21 +33,24 @@ export class LsxPreProcessor {
     // get and replace
     // get and replace
     crowi.apiGet('/plugins/lsx', {currentPath: currentPath, args: lsxArgs})
     crowi.apiGet('/plugins/lsx', {currentPath: currentPath, args: lsxArgs})
       .then((res) => {
       .then((res) => {
-        // get original content
-        const orgContent = this.crowiForJquery.getRevisionBodyContent();
-
-        // create pattern from escaped html
-        const tempHtmlRegexp = new RegExp(this.regexpEscape(tempHtml), 'g');
-
-        let replacedContent;
         if (res.ok) {
         if (res.ok) {
-          replacedContent = orgContent.replace(tempHtmlRegexp, res.html)
+          return res.html;
         }
         }
         else {
         else {
-          const errorHtml = this.createErrorHtml(tagExpression, res.error);
-          replacedContent = orgContent.replace(tempHtmlRegexp, errorHtml)
+          return Promise.reject({message: res.error});
         }
         }
-
+      })
+      .catch((reason) => {
+        return this.createErrorHtml(tagExpression, reason);
+      })
+      // finally replace contents
+      .then((html) => {
+        // create pattern from escaped html
+        const tempHtmlRegexp = new RegExp(this.regexpEscape(tempHtml), 'g');
+        // create replaced content
+        const orgContent = this.crowiForJquery.getRevisionBodyContent();
+        const replacedContent = orgContent.replace(tempHtmlRegexp, html);
+        // replace Element.html()
         this.crowiForJquery.replaceRevisionBodyContent(replacedContent);
         this.crowiForJquery.replaceRevisionBodyContent(replacedContent);
       });
       });
   }
   }
@@ -57,9 +60,9 @@ export class LsxPreProcessor {
         + `<span class="lsx-blink">${tagExpression}</span>`;
         + `<span class="lsx-blink">${tagExpression}</span>`;
   }
   }
 
 
-  createErrorHtml(tagExpression, error) {
+  createErrorHtml(tagExpression, message) {
     return `<i class="fa fa-exclamation-triangle fa-fw"></i>`
     return `<i class="fa fa-exclamation-triangle fa-fw"></i>`
-        + `${tagExpression} (-> <small>${error}</small>)`;
+        + `${tagExpression} (-> <small>${message}</small>)`;
   }
   }
 
 
   regexpEscape(s) {
   regexpEscape(s) {