hackmd.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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. const origin = `${req.protocol}://${req.get('host')}`;
  18. const styleFilePath = origin + manifest['styles/style-hackmd.css'];
  19. // generate definitions to replace
  20. const definitions = {
  21. origin,
  22. styleFilePath,
  23. };
  24. // inject
  25. const script = agentScriptContentTpl(definitions);
  26. res.set('Content-Type', 'application/javascript');
  27. res.send(script);
  28. };
  29. return {
  30. loadAgent,
  31. };
  32. };