Răsfoiți Sursa

impl parsing arguments

Yuki Takei 9 ani în urmă
părinte
comite
ed06e1c37d

+ 24 - 4
packages/growi-plugin-lsx/src/lib/routes/lsx.js

@@ -1,16 +1,36 @@
 module.exports = function(crowi, app) {
   var debug = require('debug')('crowi-plugin:lsx:routes:lsx')
+    , path = require('path')
     , Page = crowi.model('Page')
     , actions = {};
 
   actions.renderHtml = function(req, res) {
-    debug(`rendering html for '${path}'`);
+    var currentPath = req.query.currentPath;
+    var args = req.query.args;
 
-    var path = '/Level2/';
-    var queryOptions = {};
+    debug(`rendering html for currentPath='${currentPath}', args='${args}'`);
+
+    // TODO try-catch
+
+    // create {Key: Value} Objects
+    var splittedArgs = args.split(',');
+    var lsxOptions = {};
+    splittedArgs.forEach(function(arg) {
+      // see https://regex101.com/r/pYHcOM/1
+      var match = arg.match(/([^=]+)=?(.+)?/);
+      var value = match[2] || true;
+      lsxOptions[match[1]] = value;
+    });
+
+    // determine prefix
+    // 'prefix=foo' pattern or the first argument
+    var lsxPrefix = lsxOptions.prefix || splittedArgs[0];
+    // resolve path
+    var pagePath = path.resolve(currentPath, lsxPrefix);
+    var queryOptions = {}
 
     // find pages
-    Page.findListByStartWith(path, req.user, queryOptions)
+    Page.findListByStartWith(pagePath, req.user, queryOptions)
       .then(function(pages) {
         var renderVars = {};
         renderVars.pages = pages;

+ 20 - 2
packages/growi-plugin-lsx/src/resource/js/util/PreProcessor/LsxPreProcessor.js

@@ -1,9 +1,27 @@
+// TODO change to PostProcessor
 export class LsxPreProcessor {
   process(markdown) {
+    // get current path from window.location
+    let currentPath = window.location.pathname;
+
     return markdown
       // see: https://regex101.com/r/NQq3s9/2
-      .replace(/\$lsx\((.*)\)/g, function(all, group1) {
-        return `$lsx tag called!! (option='${group1}')`;
+      .replace(/\$lsx\((.*)\)/g, (all, group1) => {
+        // TODO create hash
+        let replacementId = 'key_created_by_currentPath_and_args';
+
+        /*
+         * TODO regist post processor
+         *
+        $.get('/_api/plugins/lsx', {currentPath: currentPath, args: group1},
+          (res) => {
+            // TODO
+          })
+          .fail((data) => {
+            console.error(data);
+          });
+         */
+        return `$$crowi-plugin-lsx$${replacementId}`;
       });
   }
 }