Steven Fukase 4 лет назад
Родитель
Сommit
d559793ac6
2 измененных файлов с 36 добавлено и 12 удалено
  1. 11 9
      src/client/js/components/PageCreateModal.jsx
  2. 25 3
      src/lib/util/path-utils.js

+ 11 - 9
src/client/js/components/PageCreateModal.jsx

@@ -6,15 +6,17 @@ import { Modal, ModalHeader, ModalBody } from 'reactstrap';
 
 import { withTranslation } from 'react-i18next';
 import { format } from 'date-fns';
-import urljoin from 'url-join';
+// import urljoin from 'url-join';
 
-import { userPageRoot, isCreatablePage, generateEditorPath } from '@commons/util/path-utils';
+import {
+  userPageRoot, isCreatablePage, generateEditorPath,
+} from '@commons/util/path-utils';
 import { pathUtils } from 'growi-commons';
 
 import AppContainer from '../services/AppContainer';
 import NavigationContainer from '../services/NavigationContainer';
 import { withUnstatedContainers } from './UnstatedUtils';
-import { toastError } from '../util/apiNotification';
+// import { toastError } from '../util/apiNotification';
 
 import PagePathAutoComplete from './PagePathAutoComplete';
 
@@ -76,12 +78,12 @@ const PageCreateModal = (props) => {
    * @param {string} paths
    */
   function joinCheckRedirect(...paths) {
-    const joinedUrl = encodeURI(urljoin(...paths));
-    if (!isCreatablePage(joinedUrl)) {
-      toastError(new Error('Invalid characters found on new page path'));
-      return;
-    }
-    window.location.href = generateEditorPath(joinedUrl);
+    // const joinedUrl = encodeURI(urljoin(...paths));
+    // if (!isCreatablePage(joinedUrl)) {
+    //   toastError(new Error('Invalid characters found on new page path'));
+    //   return;
+    // }
+    window.location.href = generateEditorPath(...paths);
   }
 
   /**

+ 25 - 3
src/lib/util/path-utils.js

@@ -1,4 +1,6 @@
-const urljoin = require('url-join');
+// const nodePath = require('path');
+// const nodeUrl = require('url');
+// const urljoin = require('url-join');
 const escapeStringRegexp = require('escape-string-regexp');
 
 /**
@@ -70,13 +72,33 @@ const isCreatablePage = (path) => {
   return isCreatable;
 };
 
+/**
+ * join url
+ * @param {string} paths
+ * @returns {string}
+ */
+function joinUrl(...paths) {
+
+  const pathArray = [...paths]
+    .map(str => str.replace(/^\/+|\/+$|\s/g, ''));
+
+  const queries = new Set(pathArray.filter(str => /^\?/.test(str)));
+  const hash = new Set(pathArray.filter(str => /^#/.test(str)));
+
+  const newPathArray = pathArray.filter((item) => {
+    return !queries.has(item) || !hash.has(item);
+  });
+  const url = new URL(newPathArray, 'http://dummy');
+  console.log(url);
+}
+
 /**
  * return path to editor
  * @param {string} path
  * @returns {string}
  */
-function generateEditorPath(path) {
-  return urljoin(path, '#edit');
+function generateEditorPath(...paths) {
+  return joinUrl(...paths, '#edit');
 }
 
 /**