Просмотр исходного кода

add /_hackmd/load-styles route

Yuki Takei 7 лет назад
Родитель
Сommit
f155ac3f60
3 измененных файлов с 21 добавлено и 15 удалено
  1. 20 2
      lib/routes/hackmd.js
  2. 1 0
      lib/routes/index.js
  3. 0 13
      resource/js/agent-for-hackmd.js

+ 20 - 2
lib/routes/hackmd.js

@@ -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');
 
@@ -31,12 +32,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 +44,24 @@ 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) {
+    const styleFilePath = path.join(crowi.publicDir, manifest['styles/style-hackmd.css']);
+
+    const styles = fs.readFileSync(styleFilePath);
+
+    res.set('Content-Type', 'text/css');
+    res.send(styles);
+  };
+
   const validateForApi = async function(req, res, next) {
     // validate process.env.HACKMD_URI
     const hackmdUri = process.env.HACKMD_URI;
@@ -145,6 +162,7 @@ module.exports = function(crowi, app) {
 
   return {
     loadAgent,
+    loadStyles,
     validateForApi,
     integrate,
     saveOnHackmd,

+ 1 - 0
lib/routes/index.js

@@ -214,6 +214,7 @@ module.exports = function(crowi, app) {
   app.get('/trash/*/$'               , loginRequired(crowi, app, false) , page.deletedPageListShowWrapper);
 
   app.get('/_hackmd/load-agent'        , hackmd.loadAgent);
+  app.get('/_hackmd/load-styles'       , hackmd.loadStyles);
   app.post('/_api/hackmd.integrate'    , accessTokenParser , loginRequired(crowi, app) , csrf, hackmd.validateForApi, hackmd.integrate);
   app.post('/_api/hackmd.saveOnHackmd' , accessTokenParser , loginRequired(crowi, app) , csrf, hackmd.validateForApi, hackmd.saveOnHackmd);
 

+ 0 - 13
resource/js/agent-for-hackmd.js

@@ -17,19 +17,8 @@ import { debounce } from 'throttle-debounce';
 /* eslint-disable no-console  */
 
 const allowedOrigin = '{{origin}}';         // will be replaced by swig
-const styleFilePath = '{{styleFilePath}}';  // will be replaced by swig
 
 
-/**
- * Insert link tag to load style file
- */
-function insertStyle() {
-  const element = document.createElement('link');
-  element.href = styleFilePath;
-  element.rel = 'stylesheet';
-  document.getElementsByTagName('head')[0].appendChild(element);
-}
-
 /**
  * return the value of CodeMirror
  */
@@ -154,8 +143,6 @@ function connectToParentWithPenpal() {
 
   console.log('[HackMD] Loading GROWI agent for HackMD...');
 
-  insertStyle();
-
   window.addEventListener('load', (event) => {
     console.log('loaded');
     addEventListenersToCodemirror();