Browse Source

Merge pull request #855 from weseek/fix/850-remove-trailing-slash-when-moving-2

Fix/850 remove trailing slash when moving 2
Yuki Takei 7 years ago
parent
commit
5ffdad02c3
3 changed files with 23 additions and 8 deletions
  1. 7 3
      src/lib/util/path-utils.js
  2. 6 5
      src/server/routes/page.js
  3. 10 0
      src/test/util/path-utils.test.js

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

@@ -19,8 +19,8 @@ function encodePagePath(path) {
 }
 
 function matchSlashes(path) {
-  // https://regex101.com/r/Z21fEd/3
-  return path.match(/^((\/)?(.+?))(\/)?$$/);
+  // https://regex101.com/r/Z21fEd/5
+  return path.match(/^((\/+)?(.+?))(\/+)?$/);
 }
 
 function hasHeadingSlash(path) {
@@ -65,7 +65,11 @@ function removeTrailingSlash(path) {
 }
 
 function normalizePath(path) {
-  return this.addHeadingSlash(this.removeTrailingSlash(path));
+  const match = matchSlashes(path);
+  if (match == null) {
+    return '/';
+  }
+  return `/${match[3]}`;
 }
 
 module.exports = {

+ 6 - 5
src/server/routes/page.js

@@ -48,8 +48,7 @@ module.exports = function(crowi, app) {
   }
 
   function getPathFromRequest(req) {
-    const path = '/' + (req.params[0] || '');
-    return path.replace(/\.md$/, '');
+    return pathUtils.normalizePath(req.params[0] || '');
   }
 
   function isUserPage(path) {
@@ -186,7 +185,7 @@ module.exports = function(crowi, app) {
   }
 
   async function showPageListForCrowiBehavior(req, res, next) {
-    const portalPath = Page.addSlashOfEnd(getPathFromRequest(req));
+    const portalPath = pathUtils.addTrailingSlash(getPathFromRequest(req));
     const revisionId = req.query.revision;
 
     // check whether this page has portal page
@@ -329,7 +328,7 @@ module.exports = function(crowi, app) {
 
     // check whether this page has portal page
     if (!behaviorType || 'crowi' === behaviorType) {
-      const portalPagePath = Page.addSlashOfEnd(getPathFromRequest(req));
+      const portalPagePath = pathUtils.addTrailingSlash(getPathFromRequest(req));
       let hasPortalPage = await Page.count({ path: portalPagePath }) > 0;
 
       if (hasPortalPage) {
@@ -391,10 +390,12 @@ module.exports = function(crowi, app) {
   actions.notFound = async function(req, res) {
     const path = getPathFromRequest(req);
 
+    const isCreatable = Page.isCreatableName(path);
+
     let view;
     const renderVars = { path };
 
-    if (req.isForbidden) {
+    if (!isCreatable || req.isForbidden) {
       view = 'customlayout-selector/forbidden';
     }
     else {

+ 10 - 0
src/test/util/path-utils.test.js

@@ -9,6 +9,11 @@ const pathUtils = require('@commons/util/path-utils');
 describe('page-utils', () => {
 
   describe('.normalizePath', () => {
+    it('should rurn root path with empty string', done => {
+      expect(pathUtils.normalizePath('')).to.equal('/');
+      done();
+    });
+
     it('should add heading slash', done => {
       expect(pathUtils.normalizePath('hoge/fuga')).to.equal('/hoge/fuga');
       done();
@@ -18,5 +23,10 @@ describe('page-utils', () => {
       expect(pathUtils.normalizePath('/hoge/fuga/')).to.equal('/hoge/fuga');
       done();
     });
+
+    it('should remove unnecessary slashes', done => {
+      expect(pathUtils.normalizePath('//hoge/fuga//')).to.equal('/hoge/fuga');
+      done();
+    });
   });
 });