|
|
@@ -1,5 +1,6 @@
|
|
|
const logger = require('@alias/logger')('growi:routes:hackmd');
|
|
|
const path = require('path');
|
|
|
+const fs = require('graceful-fs');
|
|
|
const swig = require('swig-templates');
|
|
|
const axios = require('axios');
|
|
|
|
|
|
@@ -10,9 +11,11 @@ module.exports = function(crowi, app) {
|
|
|
|
|
|
// load GROWI agent script for HackMD
|
|
|
const manifest = require(path.join(crowi.publicDir, 'manifest.json'));
|
|
|
- const agentScriptPath = path.join(crowi.publicDir, manifest['js/agent-for-hackmd.js']);
|
|
|
+ const agentScriptPath = path.join(crowi.publicDir, manifest['js/hackmd-agent.js']);
|
|
|
+ const stylesScriptPath = path.join(crowi.publicDir, manifest['js/hackmd-styles.js']);
|
|
|
// generate swig template
|
|
|
let agentScriptContentTpl = undefined;
|
|
|
+ let stylesScriptContentTpl = undefined;
|
|
|
|
|
|
|
|
|
/**
|
|
|
@@ -31,12 +34,10 @@ module.exports = function(crowi, app) {
|
|
|
}
|
|
|
|
|
|
const origin = `${req.protocol}://${req.get('host')}`;
|
|
|
- const styleFilePath = origin + manifest['styles/style-hackmd.css'];
|
|
|
|
|
|
// generate definitions to replace
|
|
|
const definitions = {
|
|
|
origin,
|
|
|
- styleFilePath,
|
|
|
};
|
|
|
// inject
|
|
|
const script = agentScriptContentTpl(definitions);
|
|
|
@@ -45,6 +46,35 @@ module.exports = function(crowi, app) {
|
|
|
res.send(script);
|
|
|
};
|
|
|
|
|
|
+ /**
|
|
|
+ * GET /_hackmd/load-styles
|
|
|
+ *
|
|
|
+ * loadStyles action
|
|
|
+ * This should be access from HackMD and send script to insert styles
|
|
|
+ *
|
|
|
+ * @param {object} req
|
|
|
+ * @param {object} res
|
|
|
+ */
|
|
|
+ const loadStyles = function(req, res) {
|
|
|
+ // generate swig template
|
|
|
+ if (stylesScriptContentTpl == null) {
|
|
|
+ stylesScriptContentTpl = swig.compileFile(stylesScriptPath);
|
|
|
+ }
|
|
|
+
|
|
|
+ const styleFilePath = path.join(crowi.publicDir, manifest['styles/style-hackmd.css']);
|
|
|
+ const styles = fs.readFileSync(styleFilePath).toString().replace(/\s+/g, ' ');
|
|
|
+
|
|
|
+ // generate definitions to replace
|
|
|
+ const definitions = {
|
|
|
+ styles,
|
|
|
+ };
|
|
|
+ // inject
|
|
|
+ const script = stylesScriptContentTpl(definitions);
|
|
|
+
|
|
|
+ res.set('Content-Type', 'application/javascript');
|
|
|
+ res.send(script);
|
|
|
+ };
|
|
|
+
|
|
|
const validateForApi = async function(req, res, next) {
|
|
|
// validate process.env.HACKMD_URI
|
|
|
const hackmdUri = process.env.HACKMD_URI;
|
|
|
@@ -145,6 +175,7 @@ module.exports = function(crowi, app) {
|
|
|
|
|
|
return {
|
|
|
loadAgent,
|
|
|
+ loadStyles,
|
|
|
validateForApi,
|
|
|
integrate,
|
|
|
saveOnHackmd,
|