hackmd.js 919 B

1234567891011121314151617181920212223242526272829303132333435
  1. module.exports = function(crowi, app) {
  2. const path = require('path');
  3. const swig = require('swig-templates');
  4. // load GROWI agent script for HackMD
  5. const manifest = require(path.join(crowi.publicDir, 'manifest.json'));
  6. const agentScriptPath = path.join(crowi.publicDir, manifest['js/agent-for-hackmd.js']);
  7. // generate swig template
  8. const agentScriptContentTpl = swig.compileFile(agentScriptPath);
  9. /**
  10. * loadAgent action
  11. * This should be access from HackMD and send agent script
  12. *
  13. * @param {object} req
  14. * @param {object} res
  15. */
  16. const loadAgent = function(req, res) {
  17. // generate definitions to replace
  18. const definitions = {
  19. origin: `${req.protocol}://${req.get('host')}`
  20. };
  21. // inject
  22. const script = agentScriptContentTpl(definitions);
  23. res.set('Content-Type', 'application/javascript');
  24. res.send(script);
  25. };
  26. return {
  27. loadAgent,
  28. };
  29. };