LsxBeforeRenderPageInterceptor.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. const stringReplaceAsync = require('string-replace-async');
  2. const BasicInterceptor = require('./BasicInterceptor');
  3. const Lsx = require('./Lsx');
  4. /**
  5. * The interceptor for lsx Server Side Rendering
  6. *
  7. * replace lsx tag to HTML codes
  8. * when contextName is 'beforeRenderPage'
  9. */
  10. class LsxBeforeRenderPageInterceptor extends BasicInterceptor {
  11. constructor(crowi, app) {
  12. super(crowi, app);
  13. this.lsx = new Lsx(crowi, app);
  14. }
  15. /**
  16. * @inheritdoc
  17. */
  18. isInterceptWhen(contextName) {
  19. return contextName === 'beforeRenderPage';
  20. }
  21. /**
  22. * @inheritdoc
  23. */
  24. process(contextName, ...args) {
  25. const req = args[0];
  26. const res = args[1];
  27. const renderVars = args[2];
  28. let promises = [];
  29. return stringReplaceAsync(
  30. renderVars.page.revision.body,
  31. /\$lsx\((.*)\)/g, // see: https://regex101.com/r/NQq3s9/2
  32. (all, group1) => {
  33. return this.lsx.renderHtml(req.user, req.path, group1);
  34. }
  35. ).then((results) => {
  36. // replace body
  37. renderVars.page.revision.body = results;
  38. return Promise.resolve(...args);
  39. });
  40. }
  41. }
  42. module.exports = LsxBeforeRenderPageInterceptor;