LsxPageListRenderer.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. const debug = require('debug')('crowi-plugin:lsx:util:LsxPageListRenderer');
  2. const path = require('path');
  3. class LsxPageListRenderer {
  4. constructor(crowi, app) {
  5. this.crowi = crowi;
  6. this.app = app;
  7. }
  8. renderHtml(user, fromPagePath, args) {
  9. debug(`rendering html for fromPagePath='${fromPagePath}', args='${args}'`);
  10. // TODO try-catch
  11. // initialize
  12. let lsxPrefix = args || fromPagePath;
  13. let lsxOptions = {};
  14. // if args is a String like 'key1=value1, key2=value2, ...'
  15. const splittedArgs = args.split(',');
  16. if (splittedArgs.length > 1) {
  17. splittedArgs.forEach((arg) => {
  18. arg = arg.trim();
  19. // see https://regex101.com/r/pYHcOM/1
  20. const match = arg.match(/([^=]+)=?(.+)?/);
  21. const value = match[2] || true;
  22. lsxOptions[match[1]] = value;
  23. });
  24. // determine prefix
  25. // 'prefix=foo' pattern or the first argument
  26. lsxPrefix = lsxOptions.prefix || splittedArgs[0];
  27. }
  28. // resolve path
  29. const pagePath = path.resolve(fromPath, lsxPrefix);
  30. const queryOptions = {}
  31. // find pages
  32. const Page = this.crowi.model('Page');
  33. return Page.findListByStartWith(pagePath, user, queryOptions)
  34. .then((pages) => {
  35. let renderVars = {};
  36. renderVars.pages = pages;
  37. renderVars.pager = false
  38. renderVars.viewConfig = {};
  39. // app.render widget
  40. return new Promise((resolve, reject) => {
  41. this.app.render('widget/page_list', renderVars, (err, html) => {
  42. if (err) {
  43. reject(err);
  44. }
  45. resolve(html);
  46. });
  47. });
  48. });
  49. }
  50. }
  51. module.exports = LsxPageListRenderer;